CMake Tutorial

短命女 2021-10-29 15:52 440阅读 0赞

1.最简实例

  使用cmake的最简实例是由一个源程序文件生成一个可执行文件。例如由下述C++源程序文件生成可执行文件tutorial。

  main.cpp

  1. #include<iostream>
  2. using namespace std;
  3. int main(){
  4. cout<<"hello world"<<endl;
  5. }

  需要编辑CMakeLists.txt文件如下:

  1. cmake_minimum_required(VERSION 2.6)
  2. project (tutorial)
  3. add_executable(tutorial main.cpp)

  其中cmake_minimum_required指定了cmake最低版本限制,project指定了项目名称,add_executable指定了生成的可执行文件名称为tutorial,源程序文件为main.cpp。

  需要生成项目可以cd到此目录,然后依次执行下述命令:

  1. cmake .
  2. make

  可见,cmake帮助我们快速生成了项目的makefile文件,从而可以通过make命令直接生成项目的可执行文件。此时,在该路径下就生成了可执行文件tutorial,执行该程序,有:

689069-20160504154647997-1475357473.png

  使用cmake就是如此简单快捷!

2.设置版本号和配置头文件

  如果在cmake中指定版本号这样更加灵活,我们可以这么操作:通过CMakeLists.txt文件指定版本号,然后通过CMake链接到CMakeLists.txt 文件生成含有该版本号的头文件,之后就可以引用该头文件中的版本号了。

  例如:我们在CMakeLists.txt文件中指定了下述两个版本号:

  1. cmake_minimum_required (VERSION 2.6)
  2. project (Tutorial)
  3. # The version number.
  4. set (Tutorial_VERSION_MAJOR 1)
  5. set (Tutorial_VERSION_MINOR 0)
  6. # configure a header file to pass some of the CMake settings
  7. # to the source code
  8. configure_file (
  9. "${PROJECT_SOURCE_DIR}/config.h.in"
  10. "${PROJECT_BINARY_DIR}/config.h"
  11. )
  12. # add the binary tree to the search path for include files
  13. # so that we will find config.h
  14. include_directories("${PROJECT_BINARY_DIR}")
  15. # add the executable
  16. add_executable(Tutorial main.cpp)

  config.h.in 中引用了CMakeLists.txt中定义的两个版本号,如下所示:

  1. #define Tutorial_VERSION_MAJOR @Tutorial_VERSION_MAJOR@
  2. #define Tutorial_VERSION_MINOR @Tutorial_VERSION_MINOR@

  main.cpp文件直接引用config.h头文件中定义的版本号,如下所示:

  1. #include "config.h"
  2. #include<iostream>
  3. using namespace std;
  4. int main(){
  5. cout<<"Tutorial_VERSION_MAJOR: "<<Tutorial_VERSION_MAJOR<<endl;
  6. cout<<"Tutorial_VERSION_MINOR: "<<Tutorial_VERSION_MINOR<<endl;
  7. cout<<"hello world"<<endl;
  8. return 0;
  9. }

  此时,执行命令cmake不仅生成了makefile文件,还链接生成了config.h文件,如下所示:

  1. #define Tutorial_VERSION_MAJOR 1
  2. #define Tutorial_VERSION_MINOR 0

  执行命令make,然后顺利生成了可执行文件Tutorial,执行该文件有:

689069-20160504161646529-1426369663.png

  完美!

3.在项目中添加可选库

  在大型项目中,我们需要根据实际情况决定到底引用哪个库中的函数实现。我们可以借助CMake便捷的实现库的可选性。

  如下,设置CMakeLists.txt文件如下所示:

  1. # CMake 最低版本号要求
  2. cmake_minimum_required (VERSION 2.8)
  3. # 项目信息
  4. project (Tutorial)
  5. # 是否使用自己的 MathFunctions 库 这是可选的,ON使用,OFF不使用
  6. option (USE_MYMATH "Use provided math implementation" ON)
  7. # 加入一个配置头文件,用于处理 CMake 对源码的设置,与option相对应
  8. configure_file (
  9. "${PROJECT_SOURCE_DIR}/config.h.in"
  10. "${PROJECT_BINARY_DIR}/config.h"
  11. )
  12. # 是否加入 MathFunctions 库
  13. if (USE_MYMATH)
  14. include_directories ("${PROJECT_SOURCE_DIR}/math")
  15. add_subdirectory (math)
  16. set (EXTRA_LIBS ${EXTRA_LIBS} MathFunctions )
  17. endif (USE_MYMATH)
  18. # 查找当前目录下的所有源文件
  19. # 并将名称保存到 DIR_SRCS 变量
  20. aux_source_directory(. DIR_SRCS)
  21. # 指定生成目标
  22. add_executable (Tutorial ${DIR_SRCS})
  23. target_link_libraries (Tutorial ${EXTRA_LIBS})

  相对应的,为了使得CMake能够自动的根据option自动生成相对应的头文件config.h,我们编辑config.h.in文件如下:

  1. #cmakedefine USE_MYMATH

  当我们设置option为ON的时候,CMake链接生成的config.h头文件如下所示:

  1. #define USE_MYMATH

  所以,我们可以在主程序源文件中根据USE_MYMATH是否定义来决策是否使用自定义库。主源程序文件main.cpp如下所示:

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include "config.h"
  4. #ifdef USE_MYMATH
  5. #include "math/MathFunctions.h"
  6. #else
  7. #include <math.h>
  8. #endif
  9. int main(int argc, char *argv[])
  10. {
  11. double base = 2.0;
  12. int exponent = 4;
  13. #ifdef USE_MYMATH
  14. printf("Now we use our own Math library. \n");
  15. double result = power(base, exponent);
  16. #else
  17. printf("Now we use the standard library. \n");
  18. double result = pow(base, exponent);
  19. #endif
  20. printf("%g ^ %d is %g\n", base, exponent, result);
  21. return 0;
  22. }

  我们自行定义的库在子文件夹math中,其中主要有两个文件,分别是头文件和源程序文件,如下所示:

  MathFunctions.h

  1. #ifndef POWER_H
  2. #define POWER_H
  3. extern double power(double base, int exponent);
  4. #endif

  MathFunctions.cpp

  1. /**
  2. * power - Calculate the power of number.
  3. * @param base: Base value.
  4. * @param exponent: Exponent value.
  5. *
  6. * @return base raised to the power exponent.
  7. */
  8. double power(double base, int exponent)
  9. {
  10. int result = base;
  11. int i;
  12. for(i = 1; i < exponent; ++i){
  13. result = result * base;
  14. }
  15. return result;
  16. }

  为了打包生成库文件,也需要在库文件夹中定义CMakeLists.txt文件,如下所示:

  1. # 查找当前目录下的所有源文件
  2. # 并将名称保存到 DIR_LIB_SRCS 变量
  3. aux_source_directory(. DIR_LIB_SRCS)
  4. # 指定生成 MathFunctions 链接库
  5. add_library (MathFunctions ${DIR_LIB_SRCS})

  最后,测试一下我们的项目,执行命令:

  1. cmake .
  2. make

  项目顺利编译通过,执行生成的可执行文件Tutorial,有:

689069-20160504183531341-1639989664.png

  pass!

  

转载于:https://www.cnblogs.com/zhoudayang/p/5458861.html

发表评论

表情:
评论列表 (有 0 条评论,440人围观)

还没有评论,来说两句吧...

相关阅读

    相关 《ParaView Tutorial

    看到这篇博客的小伙伴大家好,在这里主要是想发布一则信息: > [《ParaView Tutorial》][ParaView Tutorial]系列图文教程上线了。 整理本

    相关 cmakecmake策略

    cmake在添加新特性之后可能不会完全兼容旧的CMake版本,这就导致了在新版本的CMake使用旧的 CMakeLists 文件时可能会存在一些问题。策略大的引入就是帮助用户和

    相关 cmakecmake-language

    组织 CMake输入文件以CMake语言写在名为CMakeLists.txt的源文件中,或者以`.CMake`扩展名结尾的文件中 项目中的CMake语言源文件被组织为:

    相关 2.25-CMake Tutorial

    指令是大小写无关的,参数和变量是大小写相关的,但推荐全部使用大写指令。? CMake编码灵活性比较大,注意形成统一的风格 较为简单的是内部构建,但是强烈推荐外

    相关 CMake Tutorial

    1.最简实例   使用cmake的最简实例是由一个源程序文件生成一个可执行文件。例如由下述C++源程序文件生成可执行文件tutorial。   main.cpp