Makefile学习之路 – 2

Makefile学习之路 – 2

继我的

Makefile学习之路 – 1

之后,今天有机会在此基础上在写一篇比较深处的文字。现列出稍微负责的makefile

[plain][/plain] view plaincopy

 

  1. CC=g++
  2. OUTPUTLIB=./lib
  3. SRCDYNDIR=./dyn
  4. SRCSTCDIR=./stc
  5. _OBJSDYN=add.o sub.o mult.o
  6. OBJSDYN= $(patsubst %, $(SRCDYNDIR)/%, $(_OBJSDYN))
  7. _OBJSSTC=output.o
  8. OBJSSTC= $(patsubst %, $(SRCSTCDIR)/%, $(_OBJSSTC))
  9. DYN = $(OUTPUTLIB)/libdyn.so.1
  10. STC = $(OUTPUTLIB)/libstc.a.1
  11. OBJ = $(DYN) $(STC) main.o $(SRCDYNDIR)/*.o
  12. EXEC = main
  13. CLEANFILE = $(OBJ) $(EXEC)
  14.  
  15. $(EXEC): $(STC) $(DYN) main.o
  16.     $(CC)  -o $@ $^
  17.  
  18. main.o: main.cpp
  19.     $(CC) -Wall -c $^
  20.  
  21. ###############################
  22. # dynmic library
  23. ##############################
  24. $(SRCDYNDIR)/%.o: $(SRCDYNDIR)/%.cpp
  25.     $(CC) -Wall -fPIC -c $^ -o $@
  26.  
  27. $(DYN): $(OBJSDYN)
  28.     $(CC) -shared $^ -o $@
  29.  
  30. ###############################
  31. # static library
  32. ##############################
  33. $(SRCSTCDIR)/%.o: $(SRCSTCDIR)/%.cpp
  34.     $(CC) -Wall -c $^ -o $@
  35.  
  36. $(STC): $(OBJSSTC)
  37.     ar rvs $@ $^
  38.  
  39.  
  40.  
  41. .PHONY: clean
  42. clean:
  43.     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

[cpp][/cpp] view plaincopy

 

  1. #include <iostream>
  2. #include "dyn/libdyn.h"
  3. #include "stc/libstc.h"
  4.  
  5. using namespace std;
  6.  
  7. int main( int argc, char** argv )
  8. {
  9.     cout<<"call function from shared object"<<endl;
  10.     cout<<"5-2="<<math_sub( 5, 2 )<<endl;
  11.     cout<<"5+2="<<math_add( 5, 2 )<<endl;
  12.     cout<<"5*2="<<math_mult( 5, 2 )<<endl;
  13.     //str_output();
  14.     return 0;
  15. }

dyn/add.cpp

[cpp][/cpp] view plaincopy

 

  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. int math_add( int a, int b )
  6. {
  7.     return a + b;
  8. }

 

dyn/sub.cpp

[cpp][/cpp] view plaincopy

 

  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. int math_sub( int a, int b )
  6. {
  7.     return a – b;
  8. }

 

dyn/multi.cpp

[cpp][/cpp] view plaincopy

 

  1. int math_mult( int a, int b )
  2. {
  3.     return a * b;
  4. }

 

dyn/libdyn.h

[cpp][/cpp] view plaincopy

 

  1. #ifndef _LIBTEST_H_
  2. #define _LIBTEST_H_
  3.  
  4. extern int math_sub( int a, int b );
  5. extern int math_add( int a, int b );
  6. extern int math_mult( int a, int b );
  7.  
  8. #endif

stc/libstc.h

[cpp][/cpp] view plaincopy

 

  1. #ifndef _LIBSTC_H_
  2. #define _LIBSTC_H_
  3.  
  4. extern void str_output( void );
  5.  
  6. #endif

stc/output.cpp

[cpp][/cpp] view plaincopy

 

  1. #include <iostream>
  2.  
  3. void str_output( void )
  4. {
  5.     std::cout<<"output from static lib"<<std::endl;
  6. }

今天现贴上这些,改天在详细解释。

版权所有,禁止转载. 如需转载,请先征得博主的同意,并且表明文章出处,否则按侵权处理.

    分享到:

2 Replies to “Makefile学习之路 – 2”

留言给游客 取消

你的邮箱是保密的 必填的信息用*表示