CMake快速入门

https://www.cnblogs.com/douzujun/p/10761176.html


参考: https://www.hahack.com/codes/cmake/

1. 单目标文件

main.c

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4.  
  5.  
  6. double power(double base, int exponent)
  7. {
  8. int result = base;
  9. int i;
  10. if (exponent == 0) {
  11. return 1;
  12. }
  13. for(i = 1; i < exponent; ++i){
  14. result = result * base;
  15. }
  16. return result;
  17. }
  18.  
  19. int main(int argc, char *argv[])
  20. {
  21. if (argc < 3){
  22. printf("Usage: %s base exponent ", argv[0]);
  23. return 1;
  24. }
  25.  
  26. double base = atof(argv[1]);
  27. int exponent = atoi(argv[2]);
  28. double result = power(base, exponent);
  29. printf("%g ^ %d is %g ", base, exponent, result);
  30. return 0;
  31. }

CMakeLists.txt

  1. cmake_minimum_required (VERSION 2.8) # 指定最低的版本号
  2.  
  3. # 项目信息
  4. project (Demo01_cmake)
  5.  
  6. # 指定生成目标
  7. add_executable(Demo_exe main.c) # Demo_exe为生成的可执行文件 main.c为依赖文件

然后 

  1. cmake . # 生成makeFile文件
    make # 执行make

2. 同一个目录,多个源文件

calc_power.h

  1. double power(double a, int b);

calc_power.c

  1. #include "calc_power.h"
  2.  
  3. double power(double base, int exponent)
  4. {
  5. int result = base;
  6. int i;
  7.  
  8. if (exponent == 0) {
  9. return 1;
  10. }
  11. for(i = 1; i < exponent; ++i){
  12. result = result * base;
  13. }
  14. return result;
  15. }

main.cpp

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include "calc_power.h"
  4. int main(int argc, char *argv[])
  5. {
  6. if (argc < 3){
  7. printf("Usage: %s base exponent ", argv[0]);
  8. return 1;
  9. }
  10. double base = atof(argv[1]);
  11. int exponent = atoi(argv[2]);
  12. double result = power(base, exponent);
  13. printf("%g ^ %d is %g ", base, exponent, result);
  14. return 0;
  15. }

CMakeLists.txt

  1. cmake_minimum_required (VERSION 2.8)
  2.  
  3. # 项目信息
  4. project (Demo2_cmake)
  5.  
  6. # 指定生成目标
  7. # add_executable(Demo2_cmake main.c calc_power.c) # 如果源文件多,这样写麻烦
  8.  
  9. # 该命令会查找指定目录下的所有源文件, 然后将结果存到指定变量名
  10. # 如下: 查找当前目录下的所有源文件, 并将名称报错到 DIR_SRCS 变量
  11. aux_source_directory(. DIR_SRCS)
  12.  
  13. # 指定生成目标
  14. # CMake 会将当前目录所有源文件名赋值给变量 DIR_SRCS
  15. # 再指示变量 DIR_SRCS 中源文件 编译成一个 Demo2_exe的可执行文件
  16. add_executable(Demo2_exe ${DIR_SRCS})

再执行

  1. cmake .
  2. make

 3. 多目录,多文件

main.c

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include "./mymath/calc_power.h"
  4. int main(int argc, char *argv[])
  5. {
  6. if (argc < 3){
  7. printf("Usage: %s base exponent ", argv[0]);
  8. return 1;
  9. }
  10. double base = atof(argv[1]);
  11. int exponent = atoi(argv[2]);
  12. double result = power(base, exponent);
  13. printf("%g ^ %d is %g ", base, exponent, result);
  14. return 0;
  15. }

mymath的目录下CMakeLists.txt

  1. # 查找当前目录下的所有源文件
  2. # 并将名称保存到 DIR_LIB_SRCS 变量
  3. aux_source_directory(. DIR_LIB_SRCS)
  4.  
  5. # 生成链接库
  6. # 使用add_library 将src目录中的源文件编译成静态链接库
  7. add_library(calc_power $(DIR_LIB_SRCS))

main.c所在的目录下的CMakeLists.txt

  1. # Make 最低版本号要求
  2. cmake_minimum_required (VERSION 2.8)
  3. # 项目信息
  4. project (Demo3_cmake)
  5. # 查找当前目录下的所有源文件
  6. # 并将名称保存到 DIR_SRCS 变量
  7. aux_source_directory(. DIR_SRCS)
  8. # 添加 math 子目录, 这样mymath目录下的 CMakeLists.txt文件和源代码也会被处理
  9. add_subdirectory(mymath)
  10. # 指定生成目标
  11. add_executable(Demo3_exe main.c)
  12. # 添加链接库, 指明可执行文件Demo3_exe 需要连接一个名为 calc_power的链接库
  13. target_link_libraries(Demo3_exe calc_power)

然后:

  1. cmake .
  2. make
原文地址:https://www.cnblogs.com/okgogo2000/p/12461593.html