cmake命令行生成32位和64位项目 - mohist - 博客园

概述

  • 本文演示环境: win10 + VS2017

1.指定变量值

咱们常用命令【cmake ..】在build目录下配置生成项目和解决方案。 其实,这个命令还有其他用法。
指定CMakeLists.txt中的变量值。比如 , fmt 库 的CMakeLists.txt中有一个变量 【BUILD_SHARED_LIBS】,可以在命令行指定其值:

  1. cmake -DBUILD_SHARED_LIBS=TRUE ..

对比可知,多了中间指定变量的命令, 其他没有变化。

2.指定编译器版本

使用命令: cmake -G 可知当前cmake支持的编译器

  1. Generators
  2. Visual Studio 17 2022 = Generates Visual Studio 2022 project files.
  3. Use -A option to specify architecture.
  4. * Visual Studio 16 2019 = Generates Visual Studio 2019 project files.
  5. Use -A option to specify architecture.
  6. Visual Studio 15 2017 [arch] = Generates Visual Studio 2017 project files.
  7. Optional [arch] can be "Win64" or "ARM".
  8. Visual Studio 14 2015 [arch] = Generates Visual Studio 2015 project files.
  9. Optional [arch] can be "Win64" or "ARM".
  10. Visual Studio 12 2013 [arch] = Generates Visual Studio 2013 project files.
  11. Optional [arch] can be "Win64" or "ARM".
  12. Visual Studio 11 2012 [arch] = Generates Visual Studio 2012 project files.
  13. Optional [arch] can be "Win64" or "ARM".
  14. Visual Studio 10 2010 [arch] = Deprecated. Generates Visual Studio 2010
  15. project files. Optional [arch] can be
  16. "Win64" or "IA64".
  17. Visual Studio 9 2008 [arch] = Generates Visual Studio 2008 project files.
  18. Optional [arch] can be "Win64" or "IA64".
  19. Borland Makefiles = Generates Borland makefiles.
  20. NMake Makefiles = Generates NMake makefiles.
  21. NMake Makefiles JOM = Generates JOM makefiles.
  22. MSYS Makefiles = Generates MSYS makefiles.
  23. MinGW Makefiles = Generates a make file for use with
  24. mingw32-make.
  25. Green Hills MULTI = Generates Green Hills MULTI files
  26. (experimental, work-in-progress).
  27. Unix Makefiles = Generates standard UNIX makefiles.
  28. Ninja = Generates build.ninja files.
  29. Ninja Multi-Config = Generates build-<Config>.ninja files.
  30. Watcom WMake = Generates Watcom WMake makefiles.
  31. CodeBlocks - MinGW Makefiles = Generates CodeBlocks project files.
  32. CodeBlocks - NMake Makefiles = Generates CodeBlocks project files.
  33. CodeBlocks - NMake Makefiles JOM
  34. = Generates CodeBlocks project files.
  35. CodeBlocks - Ninja = Generates CodeBlocks project files.
  36. CodeBlocks - Unix Makefiles = Generates CodeBlocks project files.
  37. CodeLite - MinGW Makefiles = Generates CodeLite project files.
  38. CodeLite - NMake Makefiles = Generates CodeLite project files.
  39. CodeLite - Ninja = Generates CodeLite project files.
  40. CodeLite - Unix Makefiles = Generates CodeLite project files.
  41. Eclipse CDT4 - NMake Makefiles
  42. = Generates Eclipse CDT 4.0 project files.
  43. Eclipse CDT4 - MinGW Makefiles
  44. = Generates Eclipse CDT 4.0 project files.
  45. Eclipse CDT4 - Ninja = Generates Eclipse CDT 4.0 project files.
  46. Eclipse CDT4 - Unix Makefiles= Generates Eclipse CDT 4.0 project files.
  47. Kate - MinGW Makefiles = Generates Kate project files.
  48. Kate - NMake Makefiles = Generates Kate project files.
  49. Kate - Ninja = Generates Kate project files.
  50. Kate - Unix Makefiles = Generates Kate project files.
  51. Sublime Text 2 - MinGW Makefiles
  52. = Generates Sublime Text 2 project files.
  53. Sublime Text 2 - NMake Makefiles
  54. = Generates Sublime Text 2 project files.
  55. Sublime Text 2 - Ninja = Generates Sublime Text 2 project files.
  56. Sublime Text 2 - Unix Makefiles
  57. = Generates Sublime Text 2 project files.

前面带星号*表示当前安装的编译器。

给cmake 传递参数 -G 指定编译器的例子如下(指定VS2017为例):

  1. cmake -G "Visual Studio 15 2017 " ..

该命令将默认生成32位程序
  注意: “..”前面有空格

【扩展】Linux/Unix cmake指定clang为默认编译器

3.指定指定生成64位的配置命令

上面显示Vs的版本已经说明了参数格式: Visual Studio 9 2008 [arch] 。 这里的 [arch] 就是需要指定的平台目标。可以是”Win64” or “ARM”。

还是以VS2017为例,编译生成64位,命令如下:

  1. cmake -G "Visual Studio 15 2017 Win64" ..

对比 【2】中的命令多了 “Win64”

4.指定目录下的CMakeLists.txt

习惯上,我更喜欢在和top目录下新建build 目录, 将输出的文件放到这里。【cmake ..】 命令中的【..】其实就是指定 top目录下的CMakeLists.txt文件。
原型:

  1. cmake to/path/cmake_lists_txt_dir

其中【 to/path/cmake_lists_txt_dir 】则指向的是CMakeLists.txt所在目录。