继我的
我的makefile之路-1
之后,今天有机会在此基础上在写一篇比较深处的文字。现列出稍微负责的makefile
CC=g++ OUTPUTLIB=./lib SRCDYNDIR=./dyn SRCSTCDIR=./stc _OBJSDYN=add.o sub.o mult.o OBJSDYN= $(patsubst %, $(SRCDYNDIR)/%, $(_OBJSDYN)) _OBJSSTC=output.o OBJSSTC= $(patsubst %, $(SRCSTCDIR)/%, $(_OBJSSTC)) DYN = $(OUTPUTLIB)/libdyn.so.1 STC = $(OUTPUTLIB)/libstc.a.1 OBJ = $(DYN) $(STC) main.o $(SRCDYNDIR)/*.o EXEC = main CLEANFILE = $(OBJ) $(EXEC) $(EXEC): $(STC) $(DYN) main.o $(CC) -o $@ $^ main.o: main.cpp $(CC) -Wall -c $^ ############################### # dynmic library ############################## $(SRCDYNDIR)/%.o: $(SRCDYNDIR)/%.cpp $(CC) -Wall -fPIC -c $^ -o $@ $(DYN): $(OBJSDYN) $(CC) -shared $^ -o $@ ############################### # static library ############################## $(SRCSTCDIR)/%.o: $(SRCSTCDIR)/%.cpp $(CC) -Wall -c $^ -o $@ $(STC): $(OBJSSTC) ar rvs $@ $^ .PHONY: clean clean: rm -f $(CLEANFILE)
工程结构:
/—
—– main.cpp
—– Makefile
—– dyn
—-libdyn.h
—-add.cpp
—-sub.cpp
—-mult.cpp
—– stc
— output.cpp
—– lib
目的,dyn文件夹下面的代码生成一个动态链接库,stc下面的代码生成一个静态库,生成的库均放在lib这个文件夹中,然后main.cpp中引用这2个库导出的函数,最后生成main可执行程序:
main.cpp
#include <iostream> #include "dyn/libdyn.h" #include "stc/libstc.h" using namespace std; int main( int argc, char** argv ) { cout<<"call function from shared object"<<endl; cout<<"5-2="<<math_sub( 5, 2 )<<endl; cout<<"5+2="<<math_add( 5, 2 )<<endl; cout<<"5*2="<<math_mult( 5, 2 )<<endl; //str_output(); return 0; }
dyn/add.cpp
#include <iostream> using namespace std; int math_add( int a, int b ) { return a + b; }
dyn/sub.cpp
#include <iostream> using namespace std; int math_sub( int a, int b ) { return a - b; }
dyn/multi.cpp
int math_mult( int a, int b ) { return a * b; }
dyn/libdyn.h
#ifndef _LIBTEST_H_ #define _LIBTEST_H_ extern int math_sub( int a, int b ); extern int math_add( int a, int b ); extern int math_mult( int a, int b ); #endif
stc/libstc.h
#ifndef _LIBSTC_H_ #define _LIBSTC_H_ extern void str_output( void ); #endif
stc/output.cpp
#include <iostream> void str_output( void ) { std::cout<<"output from static lib"<<std::endl; }
今天现贴上这些,改天在详细解释。
版权所有,禁止转载. 如需转载,请先征得博主的同意,并且表明文章出处,否则按侵权处理.