CMake 构建管理器






4.87/5 (21投票s)
CMake 是一款跨平台开源构建工具,可以生成 Visual Studio 6、7 和 7.1 IDE 项目文件。它还可以为 NMake、Borland、Mac OSX、Linux 和大多数 UNIX 系统生成 makefile 文件。
下载 Unix 二进制文件:http://www.cmake.org/HTML/Download.html
引言
CMake 是一款跨平台构建管理器。它的开发目的是支持一个输入,描述一个在所有平台上都可行的构建过程。然而,它不是接管构建过程,而是简单地为原生构建工具生成输入。CMake 可以生成 Microsoft 项目文件和各种用于 UNIX 和 Windows 的 makefile 格式。由于 Visual Studio 6、7 和 7.1 都具有不同的项目格式,因此可以使用 CMake 来维护对所有三个版本在您的项目中的支持。除了移植构建系统之外,CMake 还可以用于测试编译器对受支持的 C++ 特性的支持。CMake 支持类似于 UNIX 上 autoconf 的完整 try/compile try/run 系统。这将允许您的代码利用语言支持的可用特性。
背景
CMake 是一个开源项目,已经开发了三年。有关 CMake 的更多信息,请参阅 http://www.cmake.org/。CMake 的完整源代码也可以从 CMake 主页下载。
Using the Code
以下示例演示了 CMake 的一些关键思想。
涉及三个目录。顶层目录有两个子目录,分别称为 ./Demo 和 ./Hello。在 ./Hello 目录中,构建一个库。在 ./Demo 目录中,通过链接到该库来构建一个可执行文件。创建总共三个 CMakeLists.txt 文件:每个目录一个。
第一个,顶层目录包含以下 CMakeLists.txt 文件。
# The name of our project is "HELLO". CMakeLists files in this project can # refer to the root source directory of the project as ${HELLO_SOURCE_DIR} and # to the root binary directory of the project as ${HELLO_BINARY_DIR}. PROJECT(HELLO) # Recurse into the "Hello" and "Demo" subdirectories. This does not actually # cause another cmake executable to run. The same process will walk through # the project's entire directory structure. SUBDIRS(Hello Demo)然后,对于 SUBDIRS 命令中列出的每个子目录,都会创建 CMakeLists.txt 文件。在 ./Hello 目录中,创建以下 CMakeLists.txt 文件
# Create a library called "Hello" which includes the source file "hello.cxx". # Any number of sources could be listed here. ADD_LIBRARY(Hello hello.cxx)最后,在 ./Demo 目录中,创建第三个也是最后一个 CMakeLists.txt 文件
# Make sure the compiler can find include files from our Hello library. INCLUDE_DIRECTORIES(${HELLO_SOURCE_DIR}/Hello) # Make sure the linker can find the Hello library once it is built. LINK_DIRECTORIES(${HELLO_BINARY_DIR}/Hello) # Add executable called "helloDemo" that is built from the source files # "demo.cxx" and "demo_b.cxx". ADD_EXECUTABLE(helloDemo demo.cxx demo_b.cxx) # Link the executable to the Hello library. TARGET_LINK_LIBRARIES(helloDemo Hello)
CMake 在顶层目录中执行时,将处理 CMakeLists.txt 文件,然后下降到列出的子目录中。变量、包含路径、库路径等将被继承。根据系统,将构建 makefile(Unix)或工作区/项目(MSVC)。然后可以使用它们以通常的方式构建代码。
历史
- 2003年11月1日 - 更新下载