您现在的位置: 万盛学电脑网 >> 程序编程 >> 网络编程 >> 编程语言综合 >> 正文

CMake安装使用教程

作者:佚名    责任编辑:admin    更新时间:2022-06-22

   CMake是一个比make更高级的编译配置工具,它可以根据不同平台、不同的编译器,生成相应的Makefile或者vcproj项目。

  通过编写CMakeLists.txt,可以控制生成的Makefile,从而控制编译过程。CMake自动生成的Makefile不仅可以通过make命令构建项目生成目标文件,还支持安装(make install)、测试安装的程序是否能正确执行(make test,或者ctest)、生成当前平台的安装包(make package)、生成源码包(make package_source)、产生Dashboard显示数据并上传等高级功能,只要在CMakeLists.txt中简单配置,就可以完成很多复杂的功能,包括写测试用例。

  如果有嵌套目录,子目录下可以有自己的CMakeLists.txt。

  总之,CMake是一个非常强大的编译自动配置工具,支持各种平台,KDE也是用它编译的,感兴趣的可以试用一下。

  准备活动:

  (1)安装cmake。

  (2)运行cmake的方法。(GUI、命令行)

  
       CMake使用步骤:

  运行GUI的cmake界面:

  cmake-2.8.1-win32-x86bincmake-gui.exe

CMake安装使用教程 三联

  执行Configure:

  运行之后,生成了如下文件:

  生成Makefile:

  执行Generate之后生成如下文件:

  运行make进行编译:

  编译完成后,在build目录生成Tutorial.exe,运行Tutorial.exe 25就可以看到运行结果:

  运行make install安装程序:

  运行make test进行测试:

  通过cmake tutorial学习CMake配置方法 http://www.cmake.org/cmake/help/cmake_tutorial.html

  可以在源代码的Tests/Turorial目录中找到这个手册对应的代码。

  1、Step1。

  (如果不知道如何使用cmake,以及如何使用编译产生的Turorial.exe,可先看下前面“CMake使用步骤”的说明,它以Step4为例详细介绍了使用过程,Step1的配置可能不够完全,比如无法运行make install,无法运行make test,但可以参考。)

  简单的程序编译。

  (1)运行GUI的cmake,指定要编译的源代码路径和二进制文件路径(会自动创建)。

  (2)点击Configure,配置成功后,再点击Generate。

  配置需要选择合适的编译器,虽然我安装了VC2008,但没有配置成功;选择Unix Makefiles,配置成功,它自动找到了DevC++下的gcc.exe等编译器。

  (3)在build3目录执行make,就能够编译生成Turorial.exe了。

  D:ProjectsLabtestngppcmake-2.8.1TestsTutorialStep1build3>makeLinking CXX executable Tutorial.exe

  [100%] Built target Tutorial

  可以运行一下Turorial.exe:

  D:ProjectsLabtestngppcmake-2.8.1TestsTutorialStep1build3>Tutorial.exe

  Tutorial.exe Version 1.0

  Usage: Tutorial.exe number

  D:ProjectsLabtestngppcmake-2.8.1TestsTutorialStep1build3>Tutorial.exe 4

  The square root of 4 is 2

  2、Step2 把子目录编译为库,并且链接到最终的可执行文件。

  include_directories ("${PROJECT_SOURCE_DIR}/MathFunctions")

  add_subdirectory (MathFunctions) # 使得子目录MathFunctions也能被编译

  # add the executable

  add_executable (Tutorial tutorial.cxx)

  target_link_libraries (Tutorial MathFunctions)

  产生makefile:

  在GUI上点击Configure,之后Generate还是灰色,再次点击Configure,Generate就可以点击了。

  编译:

  在build目录运行make,即可开始编译,但是开始会报告sh.exe运行异常,应该是Tools下的UnxUtils的sh.exe与Win7不兼容,发现有如下make文件,估计是它导致的,于是把它重命名,不使用UnxUtils下的make,就OK乐。

  D:ToolsCMDUnxUtilsusrlocalwbinmake.exe

  编译过程:

  D:ProjectsLabtestngppcmake-2.8.1TestsTutorialStep2build>make

  [ 50%] Building CXX object MathFunctions/CMakeFiles/MathFunctions.dir/mysqrt.cxx

  .obj

  Linking CXX static library libMathFunctions.a

  [ 50%] Built target MathFunctions

  Linking CXX executable Tutorial.exe

  [100%] Built target Tutorial

  3、Step3

  支持make install把程序安装到系统指定目录,并且运行一些测试检查它是否能够正常工作。

  a、安装时使用的基础目录,由CMAKE_INSTALL_PREFIX指定。

  b、可以通过一个很简单的用例检查程序是否运行起来,没有出现异常。(TurotialRuns只是一个用例名字)

  add_test (TutorialRuns Tutorial 25)

  c、macro方式进行多组数据的测试是非常简洁方便的。

  #define a macro to simplify adding tests, then use it

  macro (do_test arg result)

  add_test (TutorialComp${arg} Tutorial ${arg})

  set_tests_properties (TutorialComp${arg}

  PROPERTIES PASS_REGULAR_EXPRESSION ${result})

  endmacro (do_test)

  # do a bunch of result based tests

  do_test (25 "25 is 5")

  do_test (-25 "-25 is 0")

  执行make install:

  D:ProjectsLabtestngppcmake-2.8.1TestsTutorialStep3build>make install

  [ 50%] "Built target MathFunctions"

  [100%] "Built target Tutorial"

  Install the project...

  -- Install configuration: ""

  -- Installing: C:/Program Files/Tutorial/bin/Tutorial.exe

  -- Installing: C:/Program Files/Tutorial/include/TutorialConfig.h

  -- Installing: C:/Program Files/Tutorial/bin/libMathFunctions.a

  -- Installing: C:/Program Files/Tutorial/include/MathFunctions.h

  安装结果: C:Program FilesTutorial>tree /f

  C:.

  ├─bin

  │ libMathFunctions.a

  │ Tutorial.exe

  │

  └─include

  MathFunctions.h

  TutorialConfig.h

  执行make test:

  D:ProjectsLabtestngppcmake-2.8.1TestsTutorialStep3build>make test

  Running tests...

  Test project D:/Projects/Lab/testngpp/cmake-2.8.1/Tests/Tutorial/Step3/build

  Start 1: TutorialRuns

  1/5 Test #1: TutorialRuns ..................... Passed 0.01 sec

  Start 2: TutorialComp25

  2/5 Test #2: TutorialComp25 ................... Passed 0.01 sec

  Start 3: TutorialNegative

  3/5 Test #3: TutorialNegative ................. Passed 0.01 sec

  Start 4: TutorialSmall

  4/5 Test #4: TutorialSmall .................... Passed 0.00 sec

  Start 5: TutorialUsage

  5/5 T