本节demo地址:https://github.com/ttroy50/cmake-examples/tree/master/01-basic/B-hello-headers
1 简介
还是一个hello cmake示例,将头文件和源文件放在不同的文件夹来组织工程文件目录。
B-hello-headers$ tree.├── CMakeLists.txt├── include│ └── Hello.h└── src ├── Hello.cpp └── main.cpp
CMakeList.txt:
# Set the minimum version of CMake that can be used# To find the cmake version run# $ cmake --versioncmake_minimum_required(VERSION 3.5)# Set the project nameproject (hello_headers)# Create a sources variable with a link to all cpp files to compileset(SOURCES src/Hello.cpp src/main.cpp)# Add an executable with the above sourcesadd_executable(hello_headers ${SOURCES})# Set the directories that should be included in the build command for this target# when running g++ these will be included as -I/directory/path/target_include_directories(hello_headers PRIVATE ${PROJECT_SOURCE_DIR}/include)
2 Concepts
这个地方的Concept翻译成概念觉得不妥,Concept的英英解释:an abstract or general idea inferred or derived from specific instances。应该取general idea inferred or derived from specific instances。根据内容把这个地方翻译成什么比较合适嘞,希望大家在评论里各抒己见。
2.1 目录路径(Directory Paths)
CMake语法指定了一些变量,这些变量可以用来帮助您在项目或源树中找到有用的目录。常用的如下所示:
- CMAKE_SOURCE_DIR 源文件根目录
- CMAKE_CURRENT_SOURCE_DIR 如果使用子项目和目录,则使用当前源目录
- PROJECT_SOURCE_DIR 当前cmake项目的源目录
- CMAKE_BINARY_DIR 根二进制/构建目录,运行cmake命令的目录
- CMAKE_CURRENT_BINARY_DIR 当前所在的生成目录
- PROJECT_BINARY_DIR 当前项目的生成目录
看上边的解释比较杂乱,后边用到这些变量的时候可以对照一下。
2.2 源文件变量(Source Files Variable)
创建一个包含源文件的变量可以让您更清楚地了解这些文件,并轻松地将它们添加到多个命令中,例如add_executable()函数
# Create a sources variable with a link to all cpp files to compileset(SOURCES src/Hello.cpp src/main.cpp )add_executable(${PROJECT_NAME} ${SOURCES})
Note: 在SOURCES变量中设置特定文件名的另一种方法是使用GLOB命令使用通配符模式匹配查找文件。
file(GLOB_SOURCES "src/*.cpp")
Tip: 对于现代CMake,不建议对源使用变量。相反,典型的做法是直接在add_xxx函数中声明源。这对于glob命令尤其重要,如果您添加一个新源文件,glob命令可能并不总是显示正确的结果。
2.3 包含目录(Include Directory)
当有不同的include文件夹时,可以使用target_include_directories()函数让的编译器知道它们。当编译这个目标时,将使用-I标志将这些目录添加到编译器中,例如-I/directory/path。
target_include_directories(target PRIVATE ${PROJECT_SOURCE_DIR}/include)
PRIVATE标识符指定包含的范围。这对于库来说很重要,在下一个例子中会解释。关于这个功能的更多细节可以在这里(https://cmake.org/cmake/help/v3.0/command/target_include_directories.html)找到。
上面的代码中出现了PROJECT_SOURCE_DIR,在我的电脑上值得就是:
3 构建示例
3.1 标准输出(Standard Output)
$ mkdir build$ cd build$ cmake ..-- The C compiler identification is GNU 4.8.4-- The CXX compiler identification is GNU 4.8.4-- Check for working C compiler: /usr/bin/cc-- Check for working C compiler: /usr/bin/cc -- works-- Detecting C compiler ABI info-- Detecting C compiler ABI info - done-- Check for working CXX compiler: /usr/bin/c++-- Check for working CXX compiler: /usr/bin/c++ -- works-- Detecting CXX compiler ABI info-- Detecting CXX compiler ABI info - done-- Configuring done-- Generating done-- Build files have been written to: /home/matrim/workspace/cmake-examples/01-basic/hello_headers/build$ makeScanning dependencies of target hello_headers[ 50%] Building CXX object CMakeFiles/hello_headers.dir/src/Hello.cpp.o[100%] Building CXX object CMakeFiles/hello_headers.dir/src/main.cpp.oLinking CXX executable hello_headers[100%] Built target hello_headers$ ./hello_headersHello Headers!
3.2 详细输出
在前面的示例中,当运行make命令时,输出仅显示构建的状态。为了查看调试目的的完整输出,您可以在运行make时添加VERBOSE=1标志。
详细的输出如下所示,对输出的检查显示了正在添加到c++编译器命令中的include目录。
$ make clean$ make VERBOSE=1/usr/bin/cmake -H/home/matrim/workspace/cmake-examples/01-basic/hello_headers -B/home/matrim/workspace/cmake-examples/01-basic/hello_headers/build --check-build-system CMakeFiles/Makefile.cmake 0/usr/bin/cmake -E cmake_progress_start /home/matrim/workspace/cmake-examples/01-basic/hello_headers/build/CMakeFiles /home/matrim/workspace/cmake-examples/01-basic/hello_headers/build/CMakeFiles/progress.marksmake -f CMakeFiles/Makefile2 allmake[1]: Entering directory `/home/matrim/workspace/cmake-examples/01-basic/hello_headers/build'make -f CMakeFiles/hello_headers.dir/build.make CMakeFiles/hello_headers.dir/dependmake[2]: Entering directory `/home/matrim/workspace/cmake-examples/01-basic/hello_headers/build'cd /home/matrim/workspace/cmake-examples/01-basic/hello_headers/build && /usr/bin/cmake -E cmake_depends "Unix Makefiles" /home/matrim/workspace/cmake-examples/01-basic/hello_headers /home/matrim/workspace/cmake-examples/01-basic/hello_headers /home/matrim/workspace/cmake-examples/01-basic/hello_headers/build /home/matrim/workspace/cmake-examples/01-basic/hello_headers/build /home/matrim/workspace/cmake-examples/01-basic/hello_headers/build/CMakeFiles/hello_headers.dir/DependInfo.cmake --color=make[2]: Leaving directory `/home/matrim/workspace/cmake-examples/01-basic/hello_headers/build'make -f CMakeFiles/hello_headers.dir/build.make CMakeFiles/hello_headers.dir/buildmake[2]: Entering directory `/home/matrim/workspace/cmake-examples/01-basic/hello_headers/build'/usr/bin/cmake -E cmake_progress_report /home/matrim/workspace/cmake-examples/01-basic/hello_headers/build/CMakeFiles 1[ 50%] Building CXX object CMakeFiles/hello_headers.dir/src/Hello.cpp.o/usr/bin/c++ -I/home/matrim/workspace/cmake-examples/01-basic/hello_headers/include -o CMakeFiles/hello_headers.dir/src/Hello.cpp.o -c /home/matrim/workspace/cmake-examples/01-basic/hello_headers/src/Hello.cpp/usr/bin/cmake -E cmake_progress_report /home/matrim/workspace/cmake-examples/01-basic/hello_headers/build/CMakeFiles 2[100%] Building CXX object CMakeFiles/hello_headers.dir/src/main.cpp.o/usr/bin/c++ -I/home/matrim/workspace/cmake-examples/01-basic/hello_headers/include -o CMakeFiles/hello_headers.dir/src/main.cpp.o -c /home/matrim/workspace/cmake-examples/01-basic/hello_headers/src/main.cppLinking CXX executable hello_headers/usr/bin/cmake -E cmake_link_script CMakeFiles/hello_headers.dir/link.txt --verbose=1/usr/bin/c++ CMakeFiles/hello_headers.dir/src/Hello.cpp.o CMakeFiles/hello_headers.dir/src/main.cpp.o -o hello_headers -rdynamicmake[2]: Leaving directory `/home/matrim/workspace/cmake-examples/01-basic/hello_headers/build'/usr/bin/cmake -E cmake_progress_report /home/matrim/workspace/cmake-examples/01-basic/hello_headers/build/CMakeFiles 1 2[100%] Built target hello_headersmake[1]: Leaving directory `/home/matrim/workspace/cmake-examples/01-basic/hello_headers/build'/usr/bin/cmake -E cmake_progress_start /home/matrim/workspace/cmake-examples/01-basic/hello_headers/build/CMakeFiles 0
总结:这一节主要就是把头文件和源文件分别放到incllude文件夹和src文件夹,如何写CMakeList.txt文件。结合上一节的内容,Windows环境,与CMakeLists.txt同级,新建一个build文件夹,指定好相应的路径:
generate后的build文件夹如下,至此,用CMake组织好的hello_headers就结束了,打开工程文件hello_header.sln进行生成就可以了。
1.1 CMake基础-hello_cmake
注:封面图片来自CMake官网。