An operating system: Linux/Windows/macOS/Unix Development toolkit: GCC/Clang/MSVC CMake: Latest Version make: legacy build system ninja: Recommended to replace the make command. Not necessarily needed.
CMake suite maintained and supported by Kitware (kitware.com/cmake). PS D:\> cl 用于 x64 的 Microsoft (R) C/C++ 优化编译器 19.29.30147 版 版权所有(C) Microsoft Corporation。保留所有权利。
PS D:\Projects\CMake-tutorials\build> cmake -G Ninja .. -- The C compiler identification is GNU 10.3.0 -- The CXX compiler identification is GNU 10.3.0 -- Detecting C compiler ABI info -- Detecting C compiler ABI info - done -- Check for working C compiler: D:/opt/TDM-GCC-64/bin/gcc.exe - skipped -- Detecting C compile features -- Detecting C compile features - done -- Detecting CXX compiler ABI info -- Detecting CXX compiler ABI info - done -- Check for working CXX compiler: D:/opt/TDM-GCC-64/bin/c++.exe - skipped -- Detecting CXX compile features -- Detecting CXX compile features - done -- Configuring done -- Generating done -- Build files have been written to: D:/Projects/CMake-tutorials/build PS D:\Projects\CMake-tutorials\build> ninja [1/2] Building CXX object CMakeFiles/hello_cmake.dir/main.cpp.obj D:/Projects/CMake-tutorials/main.cpp:3:5: warning: second argument of 'int main(int, char***)' should be 'char **' [-Wmain] 3 | int main(int argc, char **argv[]) { | ^~~~ [2/2] Linking CXX executable hello_cmake.exe
此处,咱们使用了 ninja 进行构建,如果你没有安装,建议大家安装一个~
如果使用的是 make 进行构建,则运行下面的命令:
Unix/macOS/Linux
1 2 3 4
mkdir build cd build cmake -G "Unix Makefiles" .. make
Windows
1 2 3 4
mkdir build cd build cmake -G "MinGW Makefiles" .. mingw32-make
或者你使用Visual Studio来编译~
1 2 3 4
mkdir build cd build cmake -G "Visual Studio 16 2019" .. msbuild -consoleloggerparameters:Verbosity=minimal ALL_BUILD.vcxproj
PS D:\Projects\CMake-tutorials\build> cmake -G "Visual Studio 16 2019" .. -- Selecting Windows SDK version 10.0.19041.0 to target Windows 10.0.22621. -- The C compiler identification is MSVC 19.29.30147.0 -- The CXX compiler identification is MSVC 19.29.30147.0 -- Detecting C compiler ABI info -- Detecting C compiler ABI info - done -- Check for working C compiler: D:/Program Files (x86)/Microsoft Visual Studio/2019/Community/VC/Tools/MSVC/14.29.30133/bin/Hostx64/x64/cl.exe - skipped -- Detecting C compile features -- Detecting C compile features - done -- Detecting CXX compiler ABI info -- Detecting CXX compiler ABI info - done -- Check for working CXX compiler: D:/Program Files (x86)/Microsoft Visual Studio/2019/Community/VC/Tools/MSVC/14.29.30133/bin/Hostx64/x64/cl.exe - skipped -- Detecting CXX compile features -- Detecting CXX compile features - done -- Configuring done -- Generating done -- Build files have been written to: D:/Projects/CMake-tutorials/build PS D:\Projects\CMake-tutorials\build> msbuild -consoleloggerparameters:Verbosity=minimal ALL_BUILD.vcxproj 用于 .NET Framework 的 Microsoft (R) 生成引擎版本 16.11.2+f32259642 版权所有(C) Microsoft Corporation。保留所有权利。
Checking Build System Building Custom Rule D:/Projects/CMake-tutorials/CMakeLists.txt main.cpp hello_cmake.vcxproj -> D:\Projects\CMake-tutorials\build\Debug\hello_cmake.exe Building Custom Rule D:/Projects/CMake-tutorials/CMakeLists.txt
// the configured options and settings for Tutorial #define Tutorial_VERSION_MAJOR @hello_cmake_VERSION_MAJOR@ #define Tutorial_VERSION_MINOR @hello_cmake_VERSION_MINOR@
double mysqrt(double num) { double i = 0; double small; while (i * i < num) i += 0.01; small = i - 1; return (i*i - num > num - small * small ? small : i); }
// convert input to double constdouble inputValue = std::stod(argv[1]);
// calculate square root constdouble outputValue = mysqrt(inputValue); std::cout << "The square root of " << inputValue << " is " << outputValue << std::endl; return0; }
修改 根目录下载的 CMakeLists.txt ,添加如下内容:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
# add the MathFunctions library add_subdirectory(MathFunctions)
# add the executable add_executable(${PROJECT_NAME} tutorial.cpp)
target_link_libraries(${PROJECT_NAME} PUBLIC MathFunctions)
# add the binary tree to the search path for include files # so that we will find TutorialConfig.h target_include_directories(${PROJECT_NAME} PUBLIC ${PROJECT_BINARY_DIR} ${PROJECT_SOURCE_DIR}/MathFunctions )
此时,项目应该可以正常编译使用啦~
1 2
PS D:\Projects\CMake-tutorials\build> ."D:/Projects/CMake-tutorials/build/hello_cmake.exe" 123 The square root of 123 is 11.1
# add executable files to the project add_executable(${PROJECT_NAME} main.cpp) target_include_directories(${PROJECT_NAME} PUBLIC ${PROJECT_BINARY_DIR} ${EXTRA_INCLUDES} )
target_link_libraries(${PROJECT_NAME} PUBLIC ${EXTRA_LIBS})
在 if 块中,有 add_subdirectory 命令和 list 命令,APPEND 表示将元素 MathFunctions 追加到列表EXTRA_LIBS 中,将元素 ${PROJECT_SOURCE_DIR}/MathFunctions 追加到列表 EXTRA_INCLUDES 中。EXTRA_LIBS 存储动态/静态链接库,EXTRA_INCLUDES 存储头文件。
PS > cmake -G Ninja .. -- The C compiler identification is GNU 10.3.0 -- The CXX compiler identification is GNU 10.3.0 -- Detecting C compiler ABI info -- Detecting C compiler ABI info - done -- Check for working C compiler: D:/opt/TDM-GCC-64/bin/gcc.exe - skipped -- Detecting C compile features -- Detecting C compile features - done -- Detecting CXX compiler ABI info -- Detecting CXX compiler ABI info - done -- Check for working CXX compiler: D:/opt/TDM-GCC-64/bin/c++.exe - skipped -- Detecting CXX compile features -- Detecting CXX compile features - done -- Configuring done -- Generating done -- Build files have been written to: D:/Projects/CMake-tutorials/build PS > cmake --build . [4/4] Linking CXX executable hello_cmake.exe PS > ./hello_cmake.exe 8 The square root of 8 is 2.8285
# add the binary tree to the search path for include files # so that we will find TutorialConfig.h target_include_directories(${PROJECT_NAME} PUBLIC ${PROJECT_BINARY_DIR} ${EXTRA_INCLUDES} # 删除此行 )
$ cmake .. -- The C compiler identification is GNU 11.3.0 -- The CXX compiler identification is GNU 11.3.0 -- Detecting C compiler ABI info -- Detecting C compiler ABI info - done -- Check for working C compiler: /usr/bin/cc - skipped -- Detecting C compile features -- Detecting C compile features - done -- Detecting CXX compiler ABI info -- Detecting CXX compiler ABI info - done -- Check for working CXX compiler: /usr/bin/c++.exe - skipped -- Detecting CXX compile features -- Detecting CXX compile features - done -- Configuring done -- Generating done -- Build files have been written to: /cygdrive/d/Projects/CMake-tutorials/build $ make [ 16%] Building CXX object MathFunctions/CMakeFiles/MathFunctions.dir/mysqrt.cpp.o [ 33%] Linking CXX shared library cygMathFunctions-1.dll [ 33%] Built target MathFunctions [ 50%] Building CXX object CMakeFiles/hello_cmake.dir/main.cpp.o [ 66%] Linking CXX executable hello_cmake.exe [ 66%] Built target hello_cmake [ 83%] Building CXX object MathFunctions/CMakeFiles/MathFunctions_static.dir/mysqrt.cpp.o [100%] Linking CXX static library libMathFunctions.a [100%] Built target MathFunctions_static $ ls CMakeFiles Makefile cmake_install.cmake cygMathFunctions-1.dll libMathFunctions.a libMathFunctions.dll.a
project(myProject C CXX):该命令会影响 PROJECT_SOURCE_DIR、PROJECT_BINARY_DIR、PROJECT_NAME等变量。另外要注意的是,对于多个project嵌套的情况,CMAKE_PROJECT_NAME是当前CMakeLists.txt文件回溯至最顶层CMakeLists.txt文件中所在位置之前所定义的最后一个project的名字。