cmake编译工程实例3__适合自己参考实验,待实验模型)

cmake编译工程实例http://blog.csdn.net/rjp609206160/article/details/51579909

麻雀虽小,五脏俱全:

       工程目录:

       

下面是具体文件内容:

1、根目录下得CMakeLists.txt

       

      

      

[cpp]  view plain  copy
  1. cmake_minimum_required(VERSION 2.5)  
  2. project(MyTest)  
  3. set(top_dir ${PROJECT_SOURCE_DIR})  
  4. set(head_dir ${top_dir}/include)  
  5. set(source_dir ${top_dir}/src)  
  6. set(lib_dir ${top_dir}/lib)  
  7. set(EXECUTABLE_OUTPUT_PATH ${top_dir}/bin)  
  8. set(LIBRARY_OUTPUT_PATH ${top_dir}/lib)  
  9. link_directories(${lib_dir})  
  10. add_subdirectory(src)  
  11. add_subdirectory(lib)  

2、库目录lib

       

       CMakeLists.txt文件

       

      
[cpp]  view plain  copy
  1. add_subdirectory(LOG_C)  
  2. add_subdirectory(LOG_CPP)  
  3. add_subdirectory(LOG_SHARED)  

       2.1、LOG_C目录,不带头文件的源文件生成静态库

       

       CMakeLists.txt文件

       

       2.1.1、LOG_C的源文件目录

       

       CMakeLists.txt文件

       

       LOG_C.cpp文件

       

      

[cpp]  view plain  copy
  1. #include <stdio.h>  
  2. void show()  
  3. {  
  4.     printf("Link Lib LOG_C success !\n");  
  5. }  

       2.2、LOG_CPP目录,带头文件的源文件生成静态库

       

       CMakeLists.txt文件

       

       2.2.1、LOG_CPP的头文件目录

       

       LOG_CPP.h文件

       

      

[cpp]  view plain  copy
  1. #ifndef __LOG_CPP__H__  
  2. #define __LOG_CPP__H__  
  3.   
  4. #include <stdio.h>  
  5. class LOG_CPP  
  6. {  
  7. public:  
  8.     void show();  
  9. };  
  10.   
  11. #endif//__LOG_CPP__H__  

       2.2.2、LOG_CPP的源文件目录

       

       CMakeLists.txt文件

       

      

[cpp]  view plain  copy
  1. include_directories(../include)  
  2. aux_source_directory(. source_list)  
  3. add_library(LOG_CPP ${source_list})  
      

       LOG_CPP.cpp文件

       

      

[cpp]  view plain  copy
  1. #include "LOG_CPP.h"  
  2. void LOG_CPP::show()  
  3. {  
  4.     printf("Link Lib LOG_CPP success !\n");  
  5. }  
       2.3、LOG_SHARED目录,带头文件的源文件生成动态库

       

       CMakeLists.txt文件

       

       2.3.1、LOG_SHARED的头文件目录

       

       LOG_SHARED.h文件

       

      

[cpp]  view plain  copy
  1. #ifndef __LOG_SHARED__H__  
  2. #define __LOG_SHARED__H__  
  3.   
  4. #include <stdio.h>  
  5. class LOG_SHARED  
  6. {  
  7. public:  
  8.     void show();  
  9. };  
  10.   
  11. #endif//__LOG_SHARED__H__  
       2.3.2、LOG_SHARED的源文件目录

       

       CMakeLists.txt文件

       

      

[cpp]  view plain  copy
  1. include_directories(../include)  
  2. aux_source_directory(. source_list)  
  3. add_library(LOG_SHARED SHARED ${source_list})  

       LOG_SHARED.cpp

       

      

[cpp]  view plain  copy
  1. #include "LOG_SHARED.h"  
  2. void LOG_SHARED::show()  
  3. {  
  4.     printf("Link Lib LOG_SHARED success !\n");  
  5. }  

3、工程源文件目录src

       

       CMakeLists.txt文件

       

      

[cpp]  view plain  copy
  1. set(targetName Test)  
  2. aux_source_directory(${source_dir} source_list)  
  3. add_executable(${targetName} ${source_list})  
  4. include_directories(${head_dir})  
  5. include_directories(${lib_dir}/LOG_CPP/include)  
  6. include_directories(${lib_dir}/LOG_SHARED/include)  
  7. target_link_libraries(${targetName} LOG_C LOG_CPP LOG_SHARED)  

       

      

[cpp]  view plain  copy
  1. #include "CTest.h"  
  2. void CTest::show()  
  3. {  
  4.     printf("Test is running ......\n");  
  5. }  

       

      

[cpp]  view plain  copy
  1. #include "CTest.h"  
  2. #include "LOG_CPP.h"  
  3. #include "LOG_SHARED.h"  
  4. extern void show();  
  5. int main()  
  6. {  
  7.     CTest test;  
  8.     test.show();  
  9.   
  10.     show();  
  11.   
  12.     LOG_CPP log1;  
  13.     log1.show();  
  14.   
  15.     LOG_SHARED log2;  
  16.     log2.show();  
  17.   
  18.     return 0;  
  19. }  

4、最后进入build目录,运行命令:

      cmake ..

      make

      ./bin/Test

      运行结果,这里直接通过脚本顺序执行:

      


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值