When running the CMake configuration command, it’s recommended to explicitly specify the desired build file generator with the -G option. MSYS2 provided CMake defaults to Ninja (but this is not the default in upstream CMake, so it’s safest to explicitly specify it).
Thus, to configure and build a CMake based project, you can run the following commands:
If building by invoking cmake --build, the same command works for all generator choices. Alternatively, to build by directly invoking the build tool, you can call ninja, make or mingw32-make respectively for those three alternatives.
lyf@DESKTOP-GV2QHKN MINGW64 /f/vscode/cpp_projects/cmake-examples/01-basic/A-hello-cmake $ which cmake /mingw64/bin/cmake
lyf@DESKTOP-GV2QHKN MINGW64 /f/vscode/cpp_projects/cmake-examples/01-basic/A-hello-cmake $ which make /usr/bin/make
lyf@DESKTOP-GV2QHKN MINGW64 /f/vscode/cpp_projects/cmake-examples/01-basic/A-hello-cmake $ ls CMakeLists.txt main.cpp
lyf@DESKTOP-GV2QHKN MINGW64 /f/vscode/cpp_projects/cmake-examples/01-basic/A-hello-cmake $ cmake . -G "MSYS Makefiles" -- The C compiler identification is GNU 12.1.0 -- The CXX compiler identification is GNU 12.1.0 -- Detecting C compiler ABI info -- Detecting C compiler ABI info - done -- Check for working C compiler: D:/lyf_computer_language/msys64/mingw64/bin/cc.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:/lyf_computer_language/msys64/mingw64/bin/c++.exe - skipped -- Detecting CXX compile features -- Detecting CXX compile features - done -- Configuring done -- Generating done -- Build files have been written to: F:/vscode/cpp_projects/cmake-examples/01-basic/A-hello-cmake
lyf@DESKTOP-GV2QHKN MINGW64 /f/vscode/cpp_projects/cmake-examples/01-basic/A-hello-cmake $ ls build CMakeLists.txt main.cpp
lyf@DESKTOP-GV2QHKN MINGW64 /f/vscode/cpp_projects/cmake-examples/01-basic/A-hello-cmake $ cd build/
lyf@DESKTOP-GV2QHKN MINGW64 /f/vscode/cpp_projects/cmake-examples/01-basic/A-hello-cmake/build $ ls
lyf@DESKTOP-GV2QHKN MINGW64 /f/vscode/cpp_projects/cmake-examples/01-basic/A-hello-cmake/build $ cmake .. -- Building for: Ninja -- The C compiler identification is GNU 12.1.0 -- The CXX compiler identification is GNU 12.1.0 -- Detecting C compiler ABI info -- Detecting C compiler ABI info - done -- Check for working C compiler: D:/lyf_computer_language/msys64/mingw64/bin/cc.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:/lyf_computer_language/msys64/mingw64/bin/c++.exe - skipped -- Detecting CXX compile features -- Detecting CXX compile features - done -- Configuring done -- Generating done -- Build files have been written to: F:/vscode/cpp_projects/cmake-examples/01-basic/A-hello-cmake/build
# Set the minimum version of CMake that can be used # To find the cmake version run # $ cmake --version cmake_minimum_required(VERSION 3.5)
# Set the project name project(hello_headers)
# Create a sources variable with a link to all cpp files to compile file(GLOB SOURCES "src/*.cpp")
# Add an executable with the above sources add_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 )
lyf@DESKTOP-GV2QHKN MINGW64 /f/vscode/cpp_projects/cmake-examples/01-basic/B-hello-headers $ cd build/
lyf@DESKTOP-GV2QHKN MINGW64 /f/vscode/cpp_projects/cmake-examples/01-basic/B-hello-headers/build $ cmake .. -G "MSYS Makefiles" -- The C compiler identification is GNU 12.1.0 -- The CXX compiler identification is GNU 12.1.0 -- Detecting C compiler ABI info -- Detecting C compiler ABI info - done -- Check for working C compiler: D:/lyf_computer_language/msys64/mingw64/bin/cc.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:/lyf_computer_language/msys64/mingw64/bin/c++.exe - skipped -- Detecting CXX compile features -- Detecting CXX compile features - done -- Configuring done -- Generating done -- Build files have been written to: F:/vscode/cpp_projects/cmake-examples/01-basic/B-hello-headers/build
lyf@DESKTOP-GV2QHKN MINGW64 /f/vscode/cpp_projects/cmake-examples/01-basic/B-hello-headers/build $ make [ 33%] Building CXX object CMakeFiles/hello_headers.dir/src/Hello.cpp.obj [ 66%] Building CXX object CMakeFiles/hello_headers.dir/src/main.cpp.obj [100%] Linking CXX executable hello_headers.exe [100%] Built target hello_headers
lyf@DESKTOP-GV2QHKN MINGW64 /f/vscode/cpp_projects/cmake-examples/01-basic/B-hello-headers/build $ ls cmake_install.cmake CMakeFiles Makefile CMakeCache.txt hello_headers.exe
在此示例中,我们使用作用域设置为 PUBLIC 的 target_include_directories() 函数在库中包含目录。
1 2 3 4
target_include_directories(hello_library PUBLIC ${PROJECT_SOURCE_DIR}/include )
This will cause the included directory used in the following places:
When compiling the library.
When compiling any additional target that links the library.
The meaning of scopes are:
PRIVATE - the directory is added to this target’s include directories
INTERFACE - the directory is added to the include directories for any targets that link this library.
PUBLIC - As above, it is included in this library and also any targets that link this library.
For public headers it is often a good idea to have your include folder be “namespaced” with sub-directories.
The directory passed to target_include_directories will be the root of your include directory tree and your C++ files should include the path from there to your header.
For this example you can see that we do it as follows:
1
#include"static/Hello.h"
Using this method means that there is less chance of header filename clashes when you use multiple libraries in your project.
This tells CMake to link the hello_library against the hello_binary executable during link time. It will also propagate any include directories with PUBLIC or INTERFACE scope from the linked library target.
lyf@DESKTOP-GV2QHKN MINGW64 /f/vscode/cpp_projects/cmake-examples/01-basic/C-static-library $ cd build/
lyf@DESKTOP-GV2QHKN MINGW64 /f/vscode/cpp_projects/cmake-examples/01-basic/C-static-library/build $ cmake .. -G "MSYS Makefiles" -- The C compiler identification is GNU 12.1.0 -- The CXX compiler identification is GNU 12.1.0 -- Detecting C compiler ABI info -- Detecting C compiler ABI info - done -- Check for working C compiler: D:/lyf_computer_language/msys64/mingw64/bin/cc.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:/lyf_computer_language/msys64/mingw64/bin/c++.exe - skipped -- Detecting CXX compile features -- Detecting CXX compile features - done -- Configuring done -- Generating done -- Build files have been written to: F:/vscode/cpp_projects/cmake-examples/01-basic/C-static-library/build
lyf@DESKTOP-GV2QHKN MINGW64 /f/vscode/cpp_projects/cmake-examples/01-basic/C-static-library/build $ make [ 25%] Building CXX object CMakeFiles/hello_library.dir/src/Hello.cpp.obj [ 50%] Linking CXX static library libhello_library.a [ 50%] Built target hello_library [ 75%] Building CXX object CMakeFiles/hello_binary.dir/src/main.cpp.obj [100%] Linking CXX executable hello_binary.exe [100%] Built target hello_binary
lyf@DESKTOP-GV2QHKN MINGW64 /f/vscode/cpp_projects/cmake-examples/01-basic/D-shared-library $ cd build/
lyf@DESKTOP-GV2QHKN MINGW64 /f/vscode/cpp_projects/cmake-examples/01-basic/D-shared-library/build $ cmake .. -G "MSYS Makefiles" -- The C compiler identification is GNU 12.1.0 -- The CXX compiler identification is GNU 12.1.0 -- Detecting C compiler ABI info -- Detecting C compiler ABI info - done -- Check for working C compiler: D:/lyf_computer_language/msys64/mingw64/bin/cc.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:/lyf_computer_language/msys64/mingw64/bin/c++.exe - skipped -- Detecting CXX compile features -- Detecting CXX compile features - done -- Configuring done -- Generating done -- Build files have been written to: F:/vscode/cpp_projects/cmake-examples/01-basic/D-shared-library/build
lyf@DESKTOP-GV2QHKN MINGW64 /f/vscode/cpp_projects/cmake-examples/01-basic/D-shared-library/build $ make [ 25%] Building CXX object CMakeFiles/hello_library.dir/src/Hello.cpp.obj [ 50%] Linking CXX shared library libhello_library.dll [ 50%] Built target hello_library [ 75%] Building CXX object CMakeFiles/hello_binary.dir/src/main.cpp.obj [100%] Linking CXX executable hello_binary.exe [100%] Built target hello_binary
lyf@DESKTOP-GV2QHKN MINGW64 /f/vscode/cpp_projects/cmake-examples/01-basic/E-installing $ cd build/
lyf@DESKTOP-GV2QHKN MINGW64 /f/vscode/cpp_projects/cmake-examples/01-basic/E-installing/build $ cmake .. -G "MSYS Makefiles" -- The C compiler identification is GNU 12.1.0 -- The CXX compiler identification is GNU 12.1.0 -- Detecting C compiler ABI info -- Detecting C compiler ABI info - done -- Check for working C compiler: D:/lyf_computer_language/msys64/mingw64/bin/cc.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:/lyf_computer_language/msys64/mingw64/bin/c++.exe - skipped -- Detecting CXX compile features -- Detecting CXX compile features - done -- Configuring done -- Generating done -- Build files have been written to: F:/vscode/cpp_projects/cmake-examples/01-basic/E-installing/build
lyf@DESKTOP-GV2QHKN MINGW64 /f/vscode/cpp_projects/cmake-examples/01-basic/E-installing/build $ make [ 25%] Building CXX object CMakeFiles/cmake_examples_inst.dir/src/Hello.cpp.obj [ 50%] Linking CXX shared library libcmake_examples_inst.dll [ 50%] Built target cmake_examples_inst [ 75%] Building CXX object CMakeFiles/cmake_examples_inst_bin.dir/src/main.cpp.obj [100%] Linking CXX executable cmake_examples_inst_bin.exe [100%] Built target cmake_examples_inst_bin
The CMake variable CMAKE_INSTALL_PREFIX is used to determine the root of where the files will be installed. If using the cmake --install command, the installation prefix can be overridden via the --prefix argument. For example:
# Set the minimum version of CMake that can be used # To find the cmake version run # $ cmake --version cmake_minimum_required(VERSION 3.5)
# Set a default build type if none was specified if(NOT CMAKE_BUILD_TYPE ANDNOT CMAKE_CONFIGURATION_TYPES) message("Setting build type to 'RelWithDebInfo' as none was specified.") set(CMAKE_BUILD_TYPE RelWithDebInfo CACHE STRING"Choose the type of build." FORCE) # Set the possible values of build type for cmake-gui set_property(CACHE CMAKE_BUILD_TYPE PROPERTY STRINGS "Debug""Release""MinSizeRel""RelWithDebInfo") endif()
# Set the project name project(build_type)
# Add an executable add_executable(cmake_examples_build_type main.cpp)
if(NOT CMAKE_BUILD_TYPE ANDNOT CMAKE_CONFIGURATION_TYPES) message("Setting build type to 'RelWithDebInfo' as none was specified.") set(CMAKE_BUILD_TYPE RelWithDebInfo CACHE STRING"Choose the type of build." FORCE) # Set the possible values of build type for cmake-gui set_property(CACHE CMAKE_BUILD_TYPE PROPERTY STRINGS "Debug""Release" "MinSizeRel""RelWithDebInfo") endif()
lyf@DESKTOP-GV2QHKN MINGW64 /f/vscode/cpp_projects/cmake-examples/01-basic/F-build-type $ cd build/
lyf@DESKTOP-GV2QHKN MINGW64 /f/vscode/cpp_projects/cmake-examples/01-basic/F-build-type/build $ cmake .. -G "MSYS Makefiles" Setting build type to 'RelWithDebInfo' as none was specified. -- The C compiler identification is GNU 12.1.0 -- The CXX compiler identification is GNU 12.1.0 -- Detecting C compiler ABI info -- Detecting C compiler ABI info - done -- Check for working C compiler: D:/lyf_computer_language/msys64/mingw64/bin/cc.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:/lyf_computer_language/msys64/mingw64/bin/c++.exe - skipped -- Detecting CXX compile features -- Detecting CXX compile features - done -- Configuring done -- Generating done -- Build files have been written to: F:/vscode/cpp_projects/cmake-examples/01-basic/F-build-type/build
如果目标是一个库,并且已选择作用域 PUBLIC 或 INTERFACE,则该定义也将包含在链接此目标的任何可执行文件中。
对于编译器选项,您还可以使用 target_compile_options() 函数。
Set Default C++ Flags
CMAKE_CXX_FLAGS 的默认值为空或包含生成类型的适当标志。
若要设置额外的默认编译标志,可以将以下内容添加到顶级 CMakeLists.txt。
1
set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DEX2" CACHE STRING"Set C++ Compiler Flags" FORCE)
与 CMAKE_CXX_FLAGS 类似,其他选项包括:
使用 CMAKE_C_FLAGS 设置 C 编译器标志。
使用 CMAKE_LINKER_FLAGS 设置链接器标志。
The values CACHE STRING "Set C++ Compiler Flags" FORCE from the above command are used to force this variable to be set in the CMakeCache.txt file.
Once set the CMAKE_C_FLAGS and CMAKE_CXX_FLAGS will set a compiler flag / definition globally for all targets in this directory or any included sub-directories. This method is not recommended for general usage now and the target_compile_definitions function is preferred.
Set CMake Flags
Similar to the build type a global C++ compiler flag can be set using the following methods.
lyf@DESKTOP-GV2QHKN MINGW64 /f/vscode/cpp_projects/cmake-examples/01-basic/G-compile-flags $ cd build/
lyf@DESKTOP-GV2QHKN MINGW64 /f/vscode/cpp_projects/cmake-examples/01-basic/G-compile-flags/build $ cmake .. -G "MSYS Makefiles" -- The C compiler identification is GNU 12.1.0 -- The CXX compiler identification is GNU 12.1.0 -- Detecting C compiler ABI info -- Detecting C compiler ABI info - done -- Check for working C compiler: D:/lyf_computer_language/msys64/mingw64/bin/cc.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:/lyf_computer_language/msys64/mingw64/bin/c++.exe - skipped -- Detecting CXX compile features -- Detecting CXX compile features - done -- Configuring done -- Generating done -- Build files have been written to: F:/vscode/cpp_projects/cmake-examples/01-basic/G-compile-flags/build
lyf@DESKTOP-GV2QHKN MINGW64 /f/vscode/cpp_projects/cmake-examples/01-basic/H-third-party-library $ tree . ├── CMakeLists.txt └── main.cpp
0 directories, 2 files
CMakeLists.txt: CMake 的配置文件。
main.cpp: main 文件。
Requirements
此示例要求 boost 安装在默认系统位置。
1
pacman -S mingw-w64-x86_64-boost
Concepts
Finding a Package
As mentioned above the find_package() function will search for CMake modules in the formant “FindXXX.cmake” from the list of folders in CMAKE_MODULE_PATH. The exact format of the arguments to find_package will depend on the module you are looking for. This is typically documented at the top of the FindXXX.cmake file.
After a package is found it will often export variables which can inform the user where to find the library, header, or executable files. Similar to the XXX_FOUND variable, these are package specific and are typically documented at the top of the FindXXX.cmake file.
The variables exported in this example include:
Boost_INCLUDE_DIRS - The path to the boost header files.
In some cases you can also check these variables by examining the cache using ccmake or cmake-gui.
Alias / Imported targets
Most modern CMake libraries export ALIAS targets in their module files. The benefit of imported targets are that they can also populate include directories and linked libraries.
For example, starting from v3.5+ of CMake, the Boost module supports this. Similar to using your own ALIAS target for libraires, an ALIAS in a module can make referencing found targets easier.
In the case of Boost, all targets are exported using the Boost:: identifier and then the name of the subsystem. For example you can use:
Boost::boost for header only libraries
Boost::system for the boost system library.
Boost::filesystem for filesystem library.
As with your own targets, these targets include their dependencies, so linking against Boost::filesystem will automatically add Boost::boost and Boost::system dependencies.
To link against an imported target you can use the following:
While most modern libraries use imported targets, not all modules have been updated. In the case where a library hasn’t been updated you will often find the following variables available:
xxx_INCLUDE_DIRS - A variable pointing to the include directory for the library.
xxx_LIBRARY - A variable pointing to the library path.
These can then be added to your target_include_directories and target_link_libraries as:
1 2 3 4 5 6 7 8 9 10 11
# Include the boost headers target_include_directories( third_party_include PRIVATE ${Boost_INCLUDE_DIRS} )
# link against the boost libraries target_link_libraries( third_party_include PRIVATE ${Boost_SYSTEM_LIBRARY} ${Boost_FILESYSTEM_LIBRARY} )
lyf@DESKTOP-GV2QHKN MINGW64 /f/vscode/cpp_projects/cmake-examples/01-basic/H-third-party-library $ cd build/
lyf@DESKTOP-GV2QHKN MINGW64 /f/vscode/cpp_projects/cmake-examples/01-basic/H-third-party-library/build $ cmake .. -G "MSYS Makefiles" -- The C compiler identification is GNU 12.1.0 -- The CXX compiler identification is GNU 12.1.0 -- Detecting C compiler ABI info -- Detecting C compiler ABI info - done -- Check for working C compiler: D:/lyf_computer_language/msys64/mingw64/bin/cc.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:/lyf_computer_language/msys64/mingw64/bin/c++.exe - skipped -- Detecting CXX compile features -- Detecting CXX compile features - done -- Found Boost: D:/lyf_computer_language/msys64/mingw64/include (found suitable version "1.79.0", minimum required is "1.46.1") found components: filesystem system boost found -- Configuring done -- Generating done -- Build files have been written to: F:/vscode/cpp_projects/cmake-examples/01-basic/H-third-party-library/build
lyf@DESKTOP-GV2QHKN MINGW64 /f/vscode/cpp_projects/cmake-examples/01-basic/H-third-party-library/build $ make [ 50%] Building CXX object CMakeFiles/third_party_include.dir/main.cpp.obj [100%] Linking CXX executable third_party_include.exe [100%] Built target third_party_include
lyf@DESKTOP-GV2QHKN MINGW64 /f/vscode/cpp_projects/cmake-examples/01-basic/H-third-party-library/build $ ls cmake_install.cmake CMakeFiles third_party_include.exe CMakeCache.txt Makefile
lyf@DESKTOP-GV2QHKN MINGW64 /f/vscode/cpp_projects/cmake-examples/01-basic/H-third-party-library/build $ ./third_party_include.exe Hello Third Party Include! Path is relative
lyf@DESKTOP-GV2QHKN CLANG64 /f/vscode/cpp_projects/cmake-examples/01-basic/I-compiling-with-clang $ cd build.clang/
lyf@DESKTOP-GV2QHKN CLANG64 /f/vscode/cpp_projects/cmake-examples/01-basic/I-compiling-with-clang/build.clang $ cmake .. -DCMAKE_C_COMPILER=clang -DCMAKE_CXX_COMPILER=clang++ -- The C compiler identification is Clang 14.0.4 -- The CXX compiler identification is Clang 14.0.4 System is unknown to cmake, create: Platform/MINGW64_NT-10.0-19044 to use this system, please post your config file on discourse.cmake.org so it can be added to cmake -- Detecting C compiler ABI info System is unknown to cmake, create: Platform/MINGW64_NT-10.0-19044 to use this system, please post your config file on discourse.cmake.org so it can be added to cmake -- Detecting C compiler ABI info - done -- Check for working C compiler: /clang64/bin/clang.exe - skipped -- Detecting C compile features -- Detecting C compile features - done -- Detecting CXX compiler ABI info System is unknown to cmake, create: Platform/MINGW64_NT-10.0-19044 to use this system, please post your config file on discourse.cmake.org so it can be added to cmake -- Detecting CXX compiler ABI info - done -- Check for working CXX compiler: /clang64/bin/clang++.exe - skipped -- Detecting CXX compile features -- Detecting CXX compile features - done -- Configuring done -- Generating done -- Build files have been written to: /f/vscode/cpp_projects/cmake-examples/01-basic/I-compiling-with-clang/build.clang
lyf@DESKTOP-GV2QHKN CLANG64 /f/vscode/cpp_projects/cmake-examples/01-basic/I-compiling-with-clang/build.clang $ make [ 50%] Building CXX object CMakeFiles/hello_cmake.dir/main.cpp.obj [100%] Linking CXX executable hello_cmake [100%] Built target hello_cmake
lyf@DESKTOP-GV2QHKN CLANG64 /f/vscode/cpp_projects/cmake-examples/01-basic/I-compiling-with-clang/build.clang $ ls cmake_install.cmake CMakeCache.txt CMakeFiles hello_cmake.exe Makefile
As mentioned, CMake is a meta-build system that can be used to create the build files for many other build tools. This example shows how to have CMake use the ninja build tool.
The files in this tutorial are below:
1 2 3 4 5 6 7
lyf@DESKTOP-GV2QHKN MINGW64 /f/vscode/cpp_projects/cmake-examples/01-basic/J-building-with-ninja $ tree . ├── CMakeLists.txt └── main.cpp
0 directories, 2 files
CMakeLists.txt - Contains the CMake commands you wish to run.
main.cpp - A simple “Hello World” cpp file.
Concepts
Generators
CMake generators are responsible for writing the input files (e.g. Makefiles) for the underlying build system. Running cmake --help will show the generators available.
Specify a source directory to (re-)generate a build system for it in the current working directory. Specify an existing build directory to re-generate its build system.
Options -S <path-to-source> = Explicitly specify a source directory. -B <path-to-build> = Explicitly specify a build directory. -C <initial-cache> = Pre-load a script to populate the cache. -D <var>[:<type>]=<value> = Create or update a cmake cache entry. -U <globbing_expr> = Remove matching entries from CMake cache. -G <generator-name> = Specify a build system generator. -T <toolset-name> = Specify toolset name if supported by generator. -A <platform-name> = Specify platform name if supported by generator. --toolchain <file> = Specify toolchain file [CMAKE_TOOLCHAIN_FILE]. --install-prefix <directory> = Specify install directory [CMAKE_INSTALL_PREFIX]. -Wdev = Enable developer warnings. -Wno-dev = Suppress developer warnings. -Werror=dev = Make developer warnings errors. -Wno-error=dev = Make developer warnings not errors. -Wdeprecated = Enable deprecation warnings. -Wno-deprecated = Suppress deprecation warnings. -Werror=deprecated = Make deprecated macro and function warnings errors. -Wno-error=deprecated = Make deprecated macro and function warnings not errors. --preset <preset>,--preset=<preset> = Specify a configure preset. --list-presets = List available presets. -E = CMake command mode. -L[A][H] = List non-advanced cached variables. --build <dir> = Build a CMake-generated project binary tree.
--install <dir> = Install a CMake-generated project binary tree. --open <dir> = Open generated project in the associated application. -N = View mode only. -P <file> = Process script mode. --find-package = Legacy pkg-config like mode. Do not use. --graphviz=[file] = Generate graphviz of dependencies, see CMakeGraphVizOptions.cmake for more. --system-information [file] = Dump information about this system. --log-level=<ERROR|WARNING|NOTICE|STATUS|VERBOSE|DEBUG|TRACE> = Set the verbosity of messages from CMake files. --loglevel is also accepted for backward compatibility reasons. --log-context = Prepend log messages with context, if given --debug-trycompile = Do not delete the try_compile build tree. Only useful on one try_compile at a time. --debug-output = Put cmake in a debug mode. --debug-find = Put cmake find in a debug mode. --debug-find-pkg=<pkg-name>[,...] = Limit cmake debug-find to the comma-separated list of packages --debug-find-var=<var-name>[,...] = Limit cmake debug-find to the comma-separated list of result variables --trace = Put cmake in trace mode. --trace-expand = Put cmake in trace mode with variable expansion. --trace-format=<human|json-v1> = Set the output format of the trace. --trace-source=<file> = Trace only this CMake file/module. Multiple
options allowed. --trace-redirect=<file> = Redirect trace output to a file instead of stderr. --warn-uninitialized = Warn about uninitialized values. --no-warn-unused-cli = Don't warn about command line options. --check-system-vars = Find problems with variable usage in system files. --profiling-format=<fmt> = Output data for profiling CMake scripts. Supported formats: google-trace --profiling-output=<file> = Select an output path for the profiling data
enabled through --profiling-format. --help,-help,-usage,-h,-H,/? = Print usage information and exit. --version,-version,/V [<f>] = Print version number and exit. --help-full [<f>] = Print all help manuals and exit. --help-manual <man> [<f>] = Print one help manual and exit. --help-manual-list [<f>] = List help manuals available and exit. --help-command <cmd> [<f>] = Print help for one command and exit. --help-command-list [<f>] = List commands with help available and exit. --help-commands [<f>] = Print cmake-commands manual and exit. --help-module <mod> [<f>] = Print help for one module and exit. --help-module-list [<f>] = List modules with help available and exit. --help-modules [<f>] = Print cmake-modules manual and exit. --help-policy <cmp> [<f>] = Print help for one policy and exit. --help-policy-list [<f>] = List policies with help available and exit. --help-policies [<f>] = Print cmake-policies manual and exit. --help-property <prop> [<f>] = Print help for one property and exit. --help-property-list [<f>] = List properties with help available and exit. --help-properties [<f>] = Print cmake-properties manual and exit. --help-variable var [<f>] = Print help for one variable and exit. --help-variable-list [<f>] = List variables with help available and exit.
--help-variables [<f>] = Print cmake-variables manual and exit.
Generators
The following generators are available on this platform (* marks default): Visual Studio 17 2022 = Generates Visual Studio 2022 project files. Use -A option to specify architecture. Visual Studio 16 2019 = Generates Visual Studio 2019 project files. Use -A option to specify architecture. Visual Studio 15 2017 [arch] = Generates Visual Studio 2017 project files. Optional [arch] can be "Win64" or "ARM". Visual Studio 14 2015 [arch] = Generates Visual Studio 2015 project files. Optional [arch] can be "Win64" or "ARM". Visual Studio 12 2013 [arch] = Generates Visual Studio 2013 project files. Optional [arch] can be "Win64" or "ARM". Visual Studio 11 2012 [arch] = Generates Visual Studio 2012 project files. Optional [arch] can be "Win64" or "ARM". Visual Studio 10 2010 [arch] = Deprecated. Generates Visual Studio 2010 project files. Optional [arch] can be "Win64" or "IA64". Visual Studio 9 2008 [arch] = Generates Visual Studio 2008 project files. Optional [arch] can be "Win64" or "IA64". Borland Makefiles = Generates Borland makefiles. NMake Makefiles = Generates NMake makefiles. NMake Makefiles JOM = Generates JOM makefiles. MSYS Makefiles = Generates MSYS makefiles. MinGW Makefiles = Generates a make file for use with mingw32-make. Green Hills MULTI = Generates Green Hills MULTI files (experimental, work-in-progress). Unix Makefiles = Generates standard UNIX makefiles. * Ninja = Generates build.ninja files. Ninja Multi-Config = Generates build-<Config>.ninja files. Watcom WMake = Generates Watcom WMake makefiles. CodeBlocks - MinGW Makefiles = Generates CodeBlocks project files. CodeBlocks - NMake Makefiles = Generates CodeBlocks project files. CodeBlocks - NMake Makefiles JOM = Generates CodeBlocks project files. CodeBlocks - Ninja = Generates CodeBlocks project files. CodeBlocks - Unix Makefiles = Generates CodeBlocks project files. CodeLite - MinGW Makefiles = Generates CodeLite project files. CodeLite - NMake Makefiles = Generates CodeLite project files. CodeLite - Ninja = Generates CodeLite project files. CodeLite - Unix Makefiles = Generates CodeLite project files. Eclipse CDT4 - NMake Makefiles = Generates Eclipse CDT 4.0 project files. Eclipse CDT4 - MinGW Makefiles = Generates Eclipse CDT 4.0 project files. Eclipse CDT4 - Ninja = Generates Eclipse CDT 4.0 project files. Eclipse CDT4 - Unix Makefiles= Generates Eclipse CDT 4.0 project files. Kate - MinGW Makefiles = Generates Kate project files. Kate - NMake Makefiles = Generates Kate project files. Kate - Ninja = Generates Kate project files. Kate - Unix Makefiles = Generates Kate project files. Sublime Text 2 - MinGW Makefiles = Generates Sublime Text 2 project files. Sublime Text 2 - NMake Makefiles = Generates Sublime Text 2 project files. Sublime Text 2 - Ninja = Generates Sublime Text 2 project files. Sublime Text 2 - Unix Makefiles = Generates Sublime Text 2 project files.
lyf@DESKTOP-GV2QHKN MINGW64 /f/vscode/cpp_projects/cmake-examples/01-basic/J-building-with-ninja $ cd build.ninja/
lyf@DESKTOP-GV2QHKN MINGW64 /f/vscode/cpp_projects/cmake-examples/01-basic/J-building-with-ninja/build.ninja $ cmake .. -G Ninja -- The C compiler identification is GNU 12.1.0 -- The CXX compiler identification is GNU 12.1.0 -- Detecting C compiler ABI info -- Detecting C compiler ABI info - done -- Check for working C compiler: D:/lyf_computer_language/msys64/mingw64/bin/cc.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:/lyf_computer_language/msys64/mingw64/bin/c++.exe - skipped -- Detecting CXX compile features -- Detecting CXX compile features - done -- Configuring done -- Generating done -- Build files have been written to: F:/vscode/cpp_projects/cmake-examples/01-basic/J-building-with-ninja/build.ninja
lyf@DESKTOP-GV2QHKN MINGW64 /f/vscode/cpp_projects/cmake-examples/01-basic/K-imported-targets $ cd build/
lyf@DESKTOP-GV2QHKN MINGW64 /f/vscode/cpp_projects/cmake-examples/01-basic/K-imported-targets/build $ cmake .. -G "MSYS Makefiles" -- The C compiler identification is GNU 12.1.0 -- The CXX compiler identification is GNU 12.1.0 -- Detecting C compiler ABI info -- Detecting C compiler ABI info - done -- Check for working C compiler: D:/lyf_computer_language/msys64/mingw64/bin/cc.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:/lyf_computer_language/msys64/mingw64/bin/c++.exe - skipped -- Detecting CXX compile features -- Detecting CXX compile features - done -- Found Boost: D:/lyf_computer_language/msys64/mingw64/include (found suitable version "1.79.0", minimum required is "1.46.1") found components: filesystem system boost found -- Configuring done -- Generating done -- Build files have been written to: F:/vscode/cpp_projects/cmake-examples/01-basic/K-imported-targets/build
lyf@DESKTOP-GV2QHKN MINGW64 /f/vscode/cpp_projects/cmake-examples/01-basic/K-imported-targets/build $ make [ 50%] Building CXX object CMakeFiles/imported_targets.dir/main.cpp.obj [100%] Linking CXX executable imported_targets.exe [100%] Built target imported_targets
lyf@DESKTOP-GV2QHKN MINGW64 /f/vscode/cpp_projects/cmake-examples/01-basic/K-imported-targets/build $ ls cmake_install.cmake CMakeFiles Makefile CMakeCache.txt imported_targets.exe
lyf@DESKTOP-GV2QHKN MINGW64 /f/vscode/cpp_projects/cmake-examples/01-basic/K-imported-targets/build $ ./imported_targets.exe Hello Third Party Include! Path is relative
Since the release of C++11 and C++14 a common use case is to invoke the compiler to use these standards. As CMake has evolved, it has added features to make this easier and new versions of CMake have changed how this is achieved.
The following examples show different methods of setting the C++ standard and what versions of CMake they are available from.
The examples include:
common-method - A simple version that should work with most versions of CMake.
cxx-standard - Using the CMAKE_CXX_STANDARD variable introduced in CMake v3.1.
compile-features - Using the target_compile_features function introduced in CMake v3.1.
i-common-method
Files
运行开始菜单的 “MSYS2 MinGW x64”,运行下面命令构建项目目录。
1 2 3 4 5
cd /f/vscode/cpp_projects/cmake-examples/01-basic/ mkdir L-cpp-standard cd L-cpp-standard mkdir i-common-method cd i-common-method
# check results and add flag if(COMPILER_SUPPORTS_CXX11) set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11") elseif(COMPILER_SUPPORTS_CXX0X) set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++0x") else() message(STATUS "The compiler ${CMAKE_CXX_COMPILER} has no C++11 support. Please use a different C++ compiler.") endif()
# Add an executable add_executable(hello_cpp11 main.cpp)
This example shows a common method to set the C++ Standard. This can be used with most versions of CMake. However, if you are targeting a recent version of CMake there are more convenient methods available.
The files in this tutorial are below:
1 2 3 4 5 6 7
lyf@DESKTOP-GV2QHKN MINGW64 /f/vscode/cpp_projects/cmake-examples/01-basic/L-cpp-standard/i-common-method $ tree . ├── CMakeLists.txt └── main.cpp
0 directories, 2 files
CMakeLists.txt - Contains the CMake commands you wish to run.
main.cpp - A simple “Hello World” cpp file targeting C++11.
Concepts
Checking Compile flags
CMake has support for attempting to compile a program with any flags you pass into the function CMAKE_CXX_COMPILER_FLAG. The result is then stored in a variable that you pass in.
This example will attempt to compile a program with the flag -std=c++11 and store the result in the variable COMPILER_SUPPORTS_CXX11.
The line include(CheckCXXCompilerFlag) tells CMake to include this function to make it available for use.
Adding the flag
Once you have determined if the compile supports a flag, you can then use the standard cmake methods to add this flag to a target. In this example we use the CMAKE_CXX_FLAGS to propegate the flag to all targets.
1 2 3 4 5 6 7
if(COMPILER_SUPPORTS_CXX11)# set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11") elseif(COMPILER_SUPPORTS_CXX0X)# set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++0x") else() message(STATUS "The compiler ${CMAKE_CXX_COMPILER} has no C++11 support. Please use a different C++ compiler.") endif()
The above example only checks for the gcc version of the compile flags and supports fallback from C++11 to the pre-standardisation C+\+0x flag. In real usage you may want to check for C14, or add support for different methods of setting the compile, e.g. -std=gnu11.
Building the Examples
Below is sample output from building this example.
lyf@DESKTOP-GV2QHKN MINGW64 /f/vscode/cpp_projects/cmake-examples/01-basic/L-cpp-standard/i-common-method $ cd build/
lyf@DESKTOP-GV2QHKN MINGW64 /f/vscode/cpp_projects/cmake-examples/01-basic/L-cpp-standard/i-common-method/build $ cmake .. -G "MSYS Makefiles" CMake Deprecation Warning at CMakeLists.txt:4 (cmake_minimum_required): Compatibility with CMake < 2.8.12 will be removed from a future version of CMake.
Update the VERSION argument <min> value or use a ...<max> suffix to tell CMake that the project does not need compatibility with older versions.
-- The C compiler identification is GNU 12.1.0 -- The CXX compiler identification is GNU 12.1.0 -- Detecting C compiler ABI info -- Detecting C compiler ABI info - done -- Check for working C compiler: D:/lyf_computer_language/msys64/mingw64/bin/cc.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:/lyf_computer_language/msys64/mingw64/bin/c++.exe - skipped -- Detecting CXX compile features -- Detecting CXX compile features - done -- Performing Test COMPILER_SUPPORTS_CXX11 -- Performing Test COMPILER_SUPPORTS_CXX11 - Success -- Performing Test COMPILER_SUPPORTS_CXX0X -- Performing Test COMPILER_SUPPORTS_CXX0X - Success -- Configuring done -- Generating done -- Build files have been written to: F:/vscode/cpp_projects/cmake-examples/01-basic/L-cpp-standard/i-common-method/build
This example shows how to set the C++ standard using the CMAKE_CXX_STANDARD variable. This is available since CMake v3.1.
The files in this tutorial are below:
1 2 3 4 5 6 7
lyf@DESKTOP-GV2QHKN MINGW64 /f/vscode/cpp_projects/cmake-examples/01-basic/L-cpp-standard/ii-cxx-standard $ tree . ├── CMakeLists.txt └── main.cpp
0 directories, 2 files
CMakeLists.txt - Contains the CMake commands you wish to run.
main.cpp - A simple “Hello World” cpp file targeting C++11.
Concepts
Using CXX_STANDARD property
Setting the CMAKE_CXX_STANDARD variable causes the CXX_STANDARD property on all targets. This causes CMake to set the appropriate flag at compille time.
The CMAKE_CXX_STANDARD variable falls back to the closest appropriate standard without a failure. For example, if you request -std=gnu11 you may end up with -std=gnu0x.
This can result in an unexpected failure at compile time.
Building the Examples
Below is sample output from building this example.
lyf@DESKTOP-GV2QHKN MINGW64 /f/vscode/cpp_projects/cmake-examples/01-basic/L-cpp-standard/ii-cxx-standard $ cd build/
lyf@DESKTOP-GV2QHKN MINGW64 /f/vscode/cpp_projects/cmake-examples/01-basic/L-cpp-standard/ii-cxx-standard/build $ cmake .. -G "MSYS Makefiles" -- The C compiler identification is GNU 12.1.0 -- The CXX compiler identification is GNU 12.1.0 -- Detecting C compiler ABI info -- Detecting C compiler ABI info - done -- Check for working C compiler: D:/lyf_computer_language/msys64/mingw64/bin/cc.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:/lyf_computer_language/msys64/mingw64/bin/c++.exe - skipped -- Detecting CXX compile features -- Detecting CXX compile features - done -- Configuring done -- Generating done -- Build files have been written to: F:/vscode/cpp_projects/cmake-examples/01-basic/L-cpp-standard/ii-cxx-standard/build
This example shows how to set the C++ standard using the target_compile_features function. This is available since CMake v3.1.
The files in this tutorial are below:
1 2 3 4 5 6 7
lyf@DESKTOP-GV2QHKN MINGW64 /f/vscode/cpp_projects/cmake-examples/01-basic/L-cpp-standard/iii-compile-features $ tree . ├── CMakeLists.txt └── main.cpp
0 directories, 2 files
CMakeLists.txt - Contains the CMake commands you wish to run.
main.cpp - A simple “Hello World” cpp file targeting C++11.
Concepts
Using target_compile_features
Calling the target_compile_features function on a target will look at the passed in feature and determine the correct compiler flag to use for your target.
1
target_compile_features(hello_cpp11 PUBLIC cxx_auto_type)
As with other target_* functions, you can specify the scope of the feature for the selected target. This populates the INTERFACE_COMPILE_FEATURES property for the target.
The list of available features can be found from the CMAKE_CXX_COMPILE_FEATURES variable. You can obtain a list of the available features using the following code:
1
message("List of compile features: ${CMAKE_CXX_COMPILE_FEATURES}")
Building the Examples
Below is sample output from building this example.
lyf@DESKTOP-GV2QHKN MINGW64 /f/vscode/cpp_projects/cmake-examples/01-basic/L-cpp-standard/iii-compile-features $ cd build/
lyf@DESKTOP-GV2QHKN MINGW64 /f/vscode/cpp_projects/cmake-examples/01-basic/L-cpp-standard/iii-compile-features/build $ cmake .. -G "MSYS Makefiles" -- The C compiler identification is GNU 12.1.0 -- The CXX compiler identification is GNU 12.1.0 -- Detecting C compiler ABI info -- Detecting C compiler ABI info - done -- Check for working C compiler: D:/lyf_computer_language/msys64/mingw64/bin/cc.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:/lyf_computer_language/msys64/mingw64/bin/c++.exe - skipped -- Detecting CXX compile features -- Detecting CXX compile features - done List of compile features: cxx_std_98;cxx_template_template_parameters;cxx_std_11;cxx_alias_templates;cxx_alignas;cxx_alignof;cxx_attributes;cxx_auto_type;cxx_constexpr;cxx_decltype;cxx_decltype_incomplete_return_types;cxx_default_function_template_args;cxx_defaulted_functions;cxx_defaulted_move_initializers;cxx_delegating_constructors;cxx_deleted_functions;cxx_enum_forward_declarations;cxx_explicit_conversions;cxx_extended_friend_declarations;cxx_extern_templates;cxx_final;cxx_func_identifier;cxx_generalized_initializers;cxx_inheriting_constructors;cxx_inline_namespaces;cxx_lambdas;cxx_local_type_template_args;cxx_long_long_type;cxx_noexcept;cxx_nonstatic_member_init;cxx_nullptr;cxx_override;cxx_range_for;cxx_raw_string_literals;cxx_reference_qualified_functions;cxx_right_angle_brackets;cxx_rvalue_references;cxx_sizeof_member;cxx_static_assert;cxx_strong_enums;cxx_thread_local;cxx_trailing_return_types;cxx_unicode_literals;cxx_uniform_initialization;cxx_unrestricted_unions;cxx_user_literals;cxx_variadic_macros;cxx_variadic_templates;cxx_std_14;cxx_aggregate_default_initializers;cxx_attribute_deprecated;cxx_binary_literals;cxx_contextual_conversions;cxx_decltype_auto;cxx_digit_separators;cxx_generic_lambdas;cxx_lambda_init_captures;cxx_relaxed_constexpr;cxx_return_type_deduction;cxx_variable_templates;cxx_std_17;cxx_std_20;cxx_std_23 -- Configuring done -- Generating done -- Build files have been written to: F:/vscode/cpp_projects/cmake-examples/01-basic/L-cpp-standard/iii-compile-features/build
Many large projects are made up of different libraries and binaries. These can be organised into multiple folders and sub-projects to ease development.
The examples included are
basic - This basic example includes a static library, a header only library and an executable.
A-basic
Files
运行开始菜单的 “MSYS2 MinGW x64”,运行下面命令构建项目目录。
1 2 3 4 5
cd /f/vscode/cpp_projects/cmake-examples/ mkdir 02-sub-projects cd 02-sub-projects mkdir A-basic cd A-basic
创建 CMakeLists.txt 文件,粘贴下面代码。
1 2 3 4 5 6 7 8
cmake_minimum_required(VERSION 3.5)
project(subprojects)
# Add sub directories add_subdirectory(sublibrary1) add_subdirectory(sublibrary2) add_subdirectory(subbinary)
创建 subbinary/CMakeLists.txt 文件,粘贴下面代码。
1 2 3 4 5 6 7 8 9 10 11 12
project(subbinary)
# Create the executable add_executable(${PROJECT_NAME} main.cpp)
# Link the static library from subproject1 using its alias sub::lib1 # Link the header only library from subproject2 using its alias sub::lib2 # This will cause the include directories for that target to be added to this project target_link_libraries(${PROJECT_NAME} sub::lib1 sub::lib2 )
This example shows how to setup a CMake project that includes sub-projects. The top level CMakeLists.txt calls the CMakeLists.txt in the sub directories to create the following:
sublibrary1/CMakeLists.txt - to make a static library
sublibrary1/include/sublib1/sublib1.h - The header file of the static library
sublibrary1/src/sublib1.cpp - The source file of the static library
sublibrary2/CMakeLists.txt - to setup header only library
sublibrary2/include/sublib2/sublib2.h - The header file of the header only library
In this example I have moved the header files to a subfolder under each projects include directory, while leaving the target include as the root include folder. This is a good idea to prevent filename clashes because you have to include a file like below:
1
#include"sublib1/sublib1.h"
This also means that if you install your library for other users the default install location would be /usr/local/include/sublib1/sublib1.h.
Concepts
Adding a Sub-Directory
A CMakeLists.txt file can include and call sub-directories which include a CMakeLists.txt files.
When a project is created using the project() command, CMake will automatically create a number of variables which can be used to reference details about the project. These variables can then be used by other sub-projects or the main project. For example, to reference the source directory for a different project you can use.
The name of the project set by the current project().
CMAKE_PROJECT_NAME
the name of the first project set by the project() command, i.e. the top level project.
PROJECT_SOURCE_DIR
The source directory of the current project.
PROJECT_BINARY_DIR
The build directory for the current project.
name_SOURCE_DIR
The source directory of the project called “name”. In this example the source directories created would be sublibrary1_SOURCE_DIR, sublibrary2_SOURCE_DIR, and subbinary_SOURCE_DIR.
name_BINARY_DIR
The binary directory of the project called “name”. In this example the binary directories created would be sublibrary1_BINARY_DIR, sublibrary2_BINARY_DIR, and subbinary_BINARY_DIR.
Header only Libraries
If you have a library that is created as a header only library, cmake supports the INTERFACE target to allow creating a target without any build output.
1
add_library(${PROJECT_NAME} INTERFACE)
When creating the target you can also include directories for that target using the INTERFACE scope. The INTERFACE scope is use to make target requirements that are used in any Libraries that link this target but not in the compilation of the target itself.
If a sub-project creates a library, it can be referenced by other projects by calling the name of the target in the target_link_libraries() command. This means that you don’t have to reference the full path of the new library and it is added as a dependency.
1 2 3 4
target_link_libraries(subbinary PUBLIC sublibrary1 )
Alternatively, you can create an alias target which allows you to reference the target in read only contexts.
To create an alias target run:
1 2
add_library(sublibrary2) add_library(sub::lib2 ALIAS sublibrary2)
To reference the alias, just it as follows:
1 2 3
target_link_libraries(subbinary sub::lib2 )
Include directories from sub-projects
When adding the libraries from the sub-projects, starting from cmake v3, there is no need to add the projects include directories in the include directories of the binary using them.
This is controlled by the scope in the target_include_directories() command when creating the libraries. In this example because the subbinary executable links the sublibrary1 and sublibrary2 libraries it will automatically include the ${sublibrary1_SOURCE_DIR}/include and ${sublibrary2_SOURCE_DIR}/include folders as they are exported with the PUBLIC and INTERFACE scopes of the libraries.
lyf@DESKTOP-GV2QHKN MINGW64 /f/vscode/cpp_projects/cmake-examples/02-sub-projects/A-basic $ cd build/
lyf@DESKTOP-GV2QHKN MINGW64 /f/vscode/cpp_projects/cmake-examples/02-sub-projects/A-basic/build $ cmake .. -G "MSYS Makefiles" -- The C compiler identification is GNU 12.1.0 -- The CXX compiler identification is GNU 12.1.0 -- Detecting C compiler ABI info -- Detecting C compiler ABI info - done -- Check for working C compiler: D:/lyf_computer_language/msys64/mingw64/bin/cc.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:/lyf_computer_language/msys64/mingw64/bin/c++.exe - skipped -- Detecting CXX compile features -- Detecting CXX compile features - done -- Configuring done -- Generating done -- Build files have been written to: F:/vscode/cpp_projects/cmake-examples/02-sub-projects/A-basic/build
lyf@DESKTOP-GV2QHKN MINGW64 /f/vscode/cpp_projects/cmake-examples/02-sub-projects/A-basic/build $ make [ 25%] Building CXX object sublibrary1/CMakeFiles/sublibrary1.dir/src/sublib1.cpp.obj [ 50%] Linking CXX static library libsublibrary1.a [ 50%] Built target sublibrary1 [ 75%] Building CXX object subbinary/CMakeFiles/subbinary.dir/main.cpp.obj [100%] Linking CXX executable subbinary.exe [100%] Built target subbinary
Code generation can be useful to create source code in different languages from a common description file. This can reduce the amount of manual code to write and increase interoperability.
Examples showing code generation using variables from CMake and also using some common tools.
configure-file - Using the CMakeconfigure_file function to inject CMake variables.
Protocol Buffers - Using Google Protocol Buffers to generate C++ source.
configure-files
Files
运行开始菜单的 “MSYS2 MinGW x64”,运行下面命令构建项目目录。
1 2 3 4 5
cd /f/vscode/cpp_projects/cmake-examples/ mkdir 03-code-generation cd 03-code-generation/ mkdir configure-files cd configure-files/
# Set a project version set(cf_example_VERSION_MAJOR 0) set(cf_example_VERSION_MINOR 2) set(cf_example_VERSION_PATCH 1) set(cf_example_VERSION "${cf_example_VERSION_MAJOR}.${cf_example_VERSION_MINOR}.${cf_example_VERSION_PATCH}")
# Call configure files on ver.h.in to set the version. # Uses the standard ${VARIABLE} syntax in the file configure_file(ver.h.in ${PROJECT_BINARY_DIR}/ver.h)
# configure the path.h.in file. # This file can only use the @VARIABLE@ syntax in the file configure_file(path.h.in ${PROJECT_BINARY_DIR}/path.h @ONLY)
# Add an executable add_executable(cf_example main.cpp )
# include the directory with the new files target_include_directories(cf_example PUBLIC ${CMAKE_BINARY_DIR} )
intmain(int argc, char *argv[]) { std::cout << "Hello Version " << ver << "!" << std::endl; std::cout << "Path is " << path << std::endl; return0; }
创建 path.h.in 文件,粘贴下面代码。
1 2 3 4 5 6 7 8
#ifndef __PATH_H__ #define __PATH_H__
// version variable that will be substituted by cmake // This shows an example using the @ variable type constchar* path = "@CMAKE_SOURCE_DIR@";
#endif
创建 ver.h.in 文件,粘贴下面代码。
1 2 3 4 5 6 7 8
#ifndef __VER_H__ #define __VER_H__
// version variable that will be substituted by cmake // This shows an example using the $ variable type constchar* ver = "${cf_example_VERSION}";
#endif
Introduction
During the call to cmake it is possible to create files that use variables from the CMakeLists.txt and cmake cache. During CMake generation the file is copied to a new location and any cmake variables are replaced.
CMakeLists.txt - Contains the CMake commands you wish to run
main.cpp - The source file with main
path.h.in - File to contain a path to the build directory
ver.h.in - File to contain the version of the project
Concepts
Configure Files
To do variable substitution in a file you can use the configure_file() function in CMake. This core arguments for this function are source file and destination file.
The first example above, allows the variable to be defined like a CMake variables using the ${} syntax or an @@ in the ver.h.in file. After generation a new file ver.h will be available in the PROJECT_BINARY_DIR.
1
constchar* ver = "${cf_example_VERSION}";
The second example, only allows variables to be defined using the @@ syntax in the path.h.in file. After generation a new file path.h will be available in the PROJECT_BINARY_DIR.
lyf@DESKTOP-GV2QHKN MINGW64 /f/vscode/cpp_projects/cmake-examples/03-code-generation/configure-files $ cd build/
lyf@DESKTOP-GV2QHKN MINGW64 /f/vscode/cpp_projects/cmake-examples/03-code-generation/configure-files/build $ cmake .. -G "MSYS Makefiles" -- The C compiler identification is GNU 12.1.0 -- The CXX compiler identification is GNU 12.1.0 -- Detecting C compiler ABI info -- Detecting C compiler ABI info - done -- Check for working C compiler: D:/lyf_computer_language/msys64/mingw64/bin/cc.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:/lyf_computer_language/msys64/mingw64/bin/c++.exe - skipped -- Detecting CXX compile features -- Detecting CXX compile features - done -- Configuring done -- Generating done -- Build files have been written to: F:/vscode/cpp_projects/cmake-examples/03-code-generation/configure-files/build
// version variable that will be substituted by cmake // This shows an example using the @ variable type const char* path = "F:/vscode/cpp_projects/cmake-examples/03-code-generation/configure-files"; #endif
// This function fills in a Person message based on user input. voidPromptForAddress(tutorial::Person* person){ cout << "Enter person ID number: "; int id; cin >> id; person->set_id(id); cin.ignore(256, '\n');
cout << "Is this a mobile, home, or work phone? "; string type; getline(cin, type); if (type == "mobile") { phone_number->set_type(tutorial::Person::MOBILE); } elseif (type == "home") { phone_number->set_type(tutorial::Person::HOME); } elseif (type == "work") { phone_number->set_type(tutorial::Person::WORK); } else { cout << "Unknown phone type. Using default." << endl; } } }
// Main function: Reads the entire address book from a file, // adds one person based on user input, then writes it back out to the same // file. intmain(int argc, char* argv[]){ // Verify that the version of the library that we linked against is // compatible with the version of the headers we compiled against. GOOGLE_PROTOBUF_VERIFY_VERSION;
{ // Read the existing address book. fstream input(argv[1], ios::in | ios::binary); if (!input) { cout << argv[1] << ": File not found. Creating a new file." << endl; } elseif (!address_book.ParseFromIstream(&input)) { cerr << "Failed to parse address book." << endl; return-1; } }
// Add an address. PromptForAddress(address_book.add_person());
{ // Write the new address book back to disk. fstream output(argv[1], ios::out | ios::trunc | ios::binary); if (!address_book.SerializeToOstream(&output)) { cerr << "Failed to write address book." << endl; return-1; } }
// Optional: Delete all global objects allocated by libprotobuf. google::protobuf::ShutdownProtobufLibrary();
return0; }
Introduction
This example shows how to generate source files using protobuf. Protocol Buffers is a data serialization format from Google. A user provides a .proto file with a description of the data. Then using the protobuf compiler, the proto file can be translated into source code in a number of languages including C++.
lyf@DESKTOP-GV2QHKN MSYS ~ $ pacman -Ss protobuf mingw32/mingw-w64-i686-protobuf 3.16.0-3 Protocol Buffers - Google's data interchange format (mingw-w64) mingw32/mingw-w64-i686-protobuf-c 1.3.3-2 Protocol Buffers implementation in C (mingw-w64) mingw32/mingw-w64-i686-python-protobuf 3.16.0-3 Protocol Buffers (mingw-w64) mingw64/mingw-w64-x86_64-protobuf 3.16.0-3 [已安装] Protocol Buffers - Google's data interchange format (mingw-w64) mingw64/mingw-w64-x86_64-protobuf-c 1.3.3-2 [已安装] Protocol Buffers implementation in C (mingw-w64) mingw64/mingw-w64-x86_64-python-protobuf 3.16.0-3 Protocol Buffers (mingw-w64) ucrt64/mingw-w64-ucrt-x86_64-protobuf 3.16.0-3 Protocol Buffers - Google's data interchange format (mingw-w64) ucrt64/mingw-w64-ucrt-x86_64-protobuf-c 1.3.3-2 Protocol Buffers implementation in C (mingw-w64) ucrt64/mingw-w64-ucrt-x86_64-python-protobuf 3.16.0-3 Protocol Buffers (mingw-w64) clang32/mingw-w64-clang-i686-protobuf 3.16.0-3 Protocol Buffers - Google's data interchange format (mingw-w64) clang32/mingw-w64-clang-i686-protobuf-c 1.3.3-2 Protocol Buffers implementation in C (mingw-w64) clang32/mingw-w64-clang-i686-python-protobuf 3.16.0-3 Protocol Buffers (mingw-w64) clang64/mingw-w64-clang-x86_64-protobuf 3.16.0-3 Protocol Buffers - Google's data interchange format (mingw-w64) clang64/mingw-w64-clang-x86_64-protobuf-c 1.3.3-2 Protocol Buffers implementation in C (mingw-w64) clang64/mingw-w64-clang-x86_64-python-protobuf 3.16.0-3 Protocol Buffers (mingw-w64) msys/protobuf 3.16.0-1 (libraries) [已安装] Protocol Buffers - Google's data interchange format msys/protobuf-devel 3.16.0-1 (development) [已安装] Protobuf headers and libraries
lyf@DESKTOP-GV2QHKN MSYS ~ $
Concepts
Exported Variables
The variables exported by the CMake protobuf package and used in this example include:
PROTOBUF_FOUND - If Protocol Buffers is installed
PROTOBUF_INCLUDE_DIRS - The protobuf header files
PROTOBUF_LIBRARIES - The protobuf library
More variables are defined and can be found by examining the documentation at the top of your FindProtobuf.cmake file.
Generating Source
The protobuf CMake package includes a number of helper functions to make the code generation easier. In this example we are generating C++ source and use the following code:
PROTO_SRCS - Name of the variable that will store the .pb.cc files.
PROTO_HDRS - Name of the variable that will store the .pb.h files.
AddressBook.proto - The .proto file to generate code from.
Generated Files
After the PROTOBUF_GENERATE_CPP function is called, you will have the above mentioned variables available. These will be marked as the output to a custom command which calls the protobuf compiler binary to generate them.
To then have the files generated you should add them to a library or executable. For example:
This will cause the protobuf compiler to be called when you call make on that executables target.
When changes are made to the .proto file, the associated source files will be autogenerated again. However, if no changes are made to the .proto file and you re-run make, then nothing will be done.
lyf@DESKTOP-GV2QHKN MINGW64 /f/vscode/cpp_projects/cmake-examples/03-code-generation/protobuf $ cd build/
lyf@DESKTOP-GV2QHKN MINGW64 /f/vscode/cpp_projects/cmake-examples/03-code-generation/protobuf/build $ cmake .. -G "MSYS Makefiles" -- The C compiler identification is GNU 12.1.0 -- The CXX compiler identification is GNU 12.1.0 -- Detecting C compiler ABI info -- Detecting C compiler ABI info - done -- Check for working C compiler: D:/lyf_computer_language/msys64/mingw64/bin/cc.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:/lyf_computer_language/msys64/mingw64/bin/c++.exe - skipped -- Detecting CXX compile features -- Detecting CXX compile features - done -- Found Protobuf: D:/lyf_computer_language/msys64/mingw64/lib/libprotobuf.dll.a (found version "3.16.0") protobuf found PROTO_SRCS = F:/vscode/cpp_projects/cmake-examples/03-code-generation/protobuf/build/AddressBook.pb.cc PROTO_HDRS = F:/vscode/cpp_projects/cmake-examples/03-code-generation/protobuf/build/AddressBook.pb.h -- Configuring done -- Generating done -- Build files have been written to: F:/vscode/cpp_projects/cmake-examples/03-code-generation/protobuf/build
lyf@DESKTOP-GV2QHKN MINGW64 /f/vscode/cpp_projects/cmake-examples/03-code-generation/protobuf/build $ ls cmake_install.cmake CMakeCache.txt CMakeFiles Makefile
lyf@DESKTOP-GV2QHKN MINGW64 /f/vscode/cpp_projects/cmake-examples/03-code-generation/protobuf/build $ make VERBOSE=1 /D/lyf_computer_language/msys64/mingw64/bin/cmake.exe -S/F/vscode/cpp_projects/cmake-examples/03-code-generation/protobuf -B/F/vscode/cpp_projects/cmake-examples/03-code-generation/protobuf/build --check-build-system CMakeFiles/Makefile.cmake 0 /D/lyf_computer_language/msys64/mingw64/bin/cmake.exe -E cmake_progress_start /F/vscode/cpp_projects/cmake-examples/03-code-generation/protobuf/build/CMakeFiles /F/vscode/cpp_projects/cmake-examples/03-code-generation/protobuf/build//CMakeFiles/progress.marks make -f CMakeFiles/Makefile2 all make[1]: 进入目录“/f/vscode/cpp_projects/cmake-examples/03-code-generation/protobuf/build” make -f CMakeFiles/protobuf_example.dir/build.make CMakeFiles/protobuf_example.dir/depend make[2]: 进入目录“/f/vscode/cpp_projects/cmake-examples/03-code-generation/protobuf/build” [ 25%] Running cpp protocol buffer compiler on AddressBook.proto /D/lyf_computer_language/msys64/mingw64/bin/protoc.exe --cpp_out F:/vscode/cpp_projects/cmake-examples/03-code-generation/protobuf/build -I F:/vscode/cpp_projects/cmake-examples/03-code-generation/protobuf F:/vscode/cpp_projects/cmake-examples/03-code-generation/protobuf/AddressBook.proto [libprotobuf WARNING C:/_/M/mingw-w64-protobuf/src/build-x86_64-shared/src/google/protobuf/compiler/parser.cc:651] No syntax specified for the proto file: AddressBook.proto. Please use 'syntax = "proto2";' or 'syntax = "proto3";' to specify a syntax version. (Defaulted to proto2 syntax.) /D/lyf_computer_language/msys64/mingw64/bin/cmake.exe -E cmake_depends "MSYS Makefiles" /F/vscode/cpp_projects/cmake-examples/03-code-generation/protobuf /F/vscode/cpp_projects/cmake-examples/03-code-generation/protobuf /F/vscode/cpp_projects/cmake-examples/03-code-generation/protobuf/build /F/vscode/cpp_projects/cmake-examples/03-code-generation/protobuf/build /F/vscode/cpp_projects/cmake-examples/03-code-generation/protobuf/build/CMakeFiles/protobuf_example.dir/DependInfo.cmake --color= make[2]: 离开目录“/f/vscode/cpp_projects/cmake-examples/03-code-generation/protobuf/build” make -f CMakeFiles/protobuf_example.dir/build.make CMakeFiles/protobuf_example.dir/build make[2]: 进入目录“/f/vscode/cpp_projects/cmake-examples/03-code-generation/protobuf/build” [ 50%] Building CXX object CMakeFiles/protobuf_example.dir/main.cpp.obj /D/lyf_computer_language/msys64/mingw64/bin/c++.exe -I/F/vscode/cpp_projects/cmake-examples/03-code-generation/protobuf/build -MD -MT CMakeFiles/protobuf_example.dir/main.cpp.obj -MF CMakeFiles/protobuf_example.dir/main.cpp.obj.d -o CMakeFiles/protobuf_example.dir/main.cpp.obj -c /F/vscode/cpp_projects/cmake-examples/03-code-generation/protobuf/main.cpp [ 75%] Building CXX object CMakeFiles/protobuf_example.dir/AddressBook.pb.cc.obj /D/lyf_computer_language/msys64/mingw64/bin/c++.exe -I/F/vscode/cpp_projects/cmake-examples/03-code-generation/protobuf/build -MD -MT CMakeFiles/protobuf_example.dir/AddressBook.pb.cc.obj -MF CMakeFiles/protobuf_example.dir/AddressBook.pb.cc.obj.d -o CMakeFiles/protobuf_example.dir/AddressBook.pb.cc.obj -c /F/vscode/cpp_projects/cmake-examples/03-code-generation/protobuf/build/AddressBook.pb.cc [100%] Linking CXX executable protobuf_example.exe /D/lyf_computer_language/msys64/mingw64/bin/cmake.exe -E rm -f CMakeFiles/protobuf_example.dir/objects.a /D/lyf_computer_language/msys64/mingw64/bin/ar.exe qc CMakeFiles/protobuf_example.dir/objects.a "CMakeFiles/protobuf_example.dir/main.cpp.obj" "CMakeFiles/protobuf_example.dir/AddressBook.pb.cc.obj" /D/lyf_computer_language/msys64/mingw64/bin/c++.exe -Wl,--whole-archive CMakeFiles/protobuf_example.dir/objects.a -Wl,--no-whole-archive -o protobuf_example.exe -Wl,--out-implib,libprotobuf_example.dll.a -Wl,--major-image-version,0,--minor-image-version,0 /D/lyf_computer_language/msys64/mingw64/lib/libprotobuf.dll.a -lkernel32 -luser32 -lgdi32 -lwinspool -lshell32 -lole32 -loleaut32 -luuid -lcomdlg32 -ladvapi32 make[2]: 离开目录“/f/vscode/cpp_projects/cmake-examples/03-code-generation/protobuf/build” [100%] Built target protobuf_example make[1]: 离开目录“/f/vscode/cpp_projects/cmake-examples/03-code-generation/protobuf/build” /D/lyf_computer_language/msys64/mingw64/bin/cmake.exe -E cmake_progress_start /F/vscode/cpp_projects/cmake-examples/03-code-generation/protobuf/build/CMakeFiles 0
lyf@DESKTOP-GV2QHKN MINGW64 /f/vscode/cpp_projects/cmake-examples/03-code-generation/protobuf/build $ ./protobuf_example.exe test.db test.db: File not found. Creating a new file. Enter person ID number: 1 Enter name: lyf Enter email address (blank for none): xxx@qq.com Enter a phone number (or leave blank to finish): 12345678901 Is this a mobile, home, or work phone? home Enter a phone number (or leave blank to finish):
12345678901 lyf@DESKTOP-GV2QHKN MINGW64 /f/vscode/cpp_projects/cmake-examples/03-code-generation/protobuf/build $ ./protobuf_example.exe test.db Enter person ID number: 2 Enter name: zhy Enter email address (blank for none): xxx@qq.com Enter a phone number (or leave blank to finish): 12345678902 Is this a mobile, home, or work phone? mobile Enter a phone number (or leave blank to finish): 99999999999 Is this a mobile, home, or work phone? work Enter a phone number (or leave blank to finish):
Static analysis is the analysis of code without executing it. It can be used to find common programming errors and enforce coding guidelines. Examples of errors that can be found using static analysis tools include:
Out of bounds errors
Memory leaks
Usage of uninitialized variables
Use of unsafe functions
Analysis tools can detect errors early and are becoming a standard tool in most build chains. Some build tools such as Clang include a build in static analysis tool. However standalone tools also exist.
The examples here include using the following tools:
lyf@DESKTOP-GV2QHKN CLANG64 /f/vscode/cpp_projects/cmake-examples/04-static-analysis/clang-analyzer $ which scan-build /clang64/bin/scan-build
Concepts
scan-build
To run clang static analyzer you can use the tool scan-build to run the analyzer when you also run the compiler. This overrides the CC and CXX environment variables and replaces them with it’s own tools. To run it you can do
1 2
$ scan-build-3.6 cmake .. $ scan-build-3.6 make
By default this will run the standard compiler for your platform, i.e. gcc on linux. However, if you want to override this you can change the command to:
1
$ scan-build-3.6 --use-cc=clang-3.6 --use-c++=clang++-3.6 -o ./scanbuildout/ make
scan-build output
scan-build will only output warnings during compile time and will also generate a list of HTML files which contain detailed analysis of the error.
1 2 3 4 5 6 7 8 9 10
$ cd scanbuildout/ $ tree . └── 2017-07-03-213514-3653-1 ├── index.html ├── report-42eba1.html ├── scanview.css └── sorttable.js
1 directory, 4 files
By default these are output to /tmp/scanbuildout/{run folder}. You can change this using scan-build -o /output/folder.
lyf@DESKTOP-GV2QHKN CLANG64 /f/vscode/cpp_projects/cmake-examples/04-static-analysis/clang-analyzer $ cd build/
lyf@DESKTOP-GV2QHKN CLANG64 /f/vscode/cpp_projects/cmake-examples/04-static-analysis/clang-analyzer/build $ scan-build -o ./scanbuildout cmake .. scan-build: Using '/clang64/bin/clang' for static analysis -- The C compiler identification is Clang 14.0.4 -- The CXX compiler identification is Clang 14.0.4 System is unknown to cmake, create: Platform/MINGW64_NT-10.0-19044 to use this system, please post your config file on discourse.cmake.org so it can be added to cmake -- Detecting C compiler ABI info System is unknown to cmake, create: Platform/MINGW64_NT-10.0-19044 to use this system, please post your config file on discourse.cmake.org so it can be added to cmake -- Detecting C compiler ABI info - done -- Check for working C compiler: /clang64/libexec/ccc-analyzer - skipped -- Detecting C compile features -- Detecting C compile features - done -- Detecting CXX compiler ABI info System is unknown to cmake, create: Platform/MINGW64_NT-10.0-19044 to use this system, please post your config file on discourse.cmake.org so it can be added to cmake -- Detecting CXX compiler ABI info - done -- Check for working CXX compiler: /clang64/libexec/c++-analyzer - skipped -- Detecting CXX compile features -- Detecting CXX compile features - done -- Configuring done -- Generating done -- Build files have been written to: /f/vscode/cpp_projects/cmake-examples/04-static-analysis/clang-analyzer/build scan-build: Analysis run complete. scan-build: Removing directory '/f/vscode/cpp_projects/cmake-examples/04-static-analysis/clang-analyzer/build/scanbuildout/2022-08-12-144922-1147-1' because it contains no reports. scan-build: No bugs found.
lyf@DESKTOP-GV2QHKN CLANG64 /f/vscode/cpp_projects/cmake-examples/04-static-analysis/clang-analyzer/build $ scan-build -o ./scanbuildout make scan-build: Using '/clang64/bin/clang' for static analysis [ 25%] Building CXX object subproject1/CMakeFiles/subproject1.dir/main1.cpp.obj [ 50%] Linking CXX executable subproject1 [ 50%] Built target subproject1 [ 75%] Building CXX object subproject2/CMakeFiles/subproject2.dir/main2.cpp.obj [100%] Linking CXX executable subproject2 [100%] Built target subproject2 scan-build: Analysis run complete. scan-build: Removing directory '/f/vscode/cpp_projects/cmake-examples/04-static-analysis/clang-analyzer/build/scanbuildout/2022-08-12-144942-1200-1' because it contains no reports. scan-build: No bugs found.
CMake has the ability to create installers for multiple platforms using a program called CPack. CPack includes the ability to create LinuxRPM, deb and gzip distributions of both binaries and source code. It also includes the ability to create NSIS files for Microsoft Windows.
deb
Files
运行开始菜单的 “MSYS2 MinGW x64”,运行下面命令构建项目目录。
1 2 3 4 5
cd /f/vscode/cpp_projects/cmake-examples/ mkdir 06-installer cd 06-installer/ mkdir deb cd deb/
创建 cmake-examples.conf 文件,粘贴下面代码。
1
# Sample configuration file that could be installed
# set a project version set(deb_example_VERSION_MAJOR 0) set(deb_example_VERSION_MINOR 2) set(deb_example_VERSION_PATCH 2) set(deb_example_VERSION "${deb_example_VERSION_MAJOR}.${deb_example_VERSION_MINOR}.${deb_example_VERSION_PATCH}")
####################################### # Create a library #######################################
# Generate the shared library from the library sources add_library(cmake_examples_zip SHARED src/Hello.cpp)
target_include_directories(cmake_examples_zip PUBLIC ${PROJECT_SOURCE_DIR}/include )
######################################## # Create an executable ########################################
# Add an executable with the above sources add_executable(cmake_examples_zip_bin src/main.cpp)
# link the new hello_library target with the hello_binary target target_link_libraries(cmake_examples_zip_bin PUBLIC cmake_examples_zip )
lyf@DESKTOP-GV2QHKN MINGW64 /f/vscode/cpp_projects/cmake-examples/06-installer/deb $ cd build/
lyf@DESKTOP-GV2QHKN MINGW64 /f/vscode/cpp_projects/cmake-examples/06-installer/deb/build $ cmake .. -G "MSYS Makefiles" -- The C compiler identification is GNU 12.1.0 -- The CXX compiler identification is GNU 12.1.0 -- Detecting C compiler ABI info -- Detecting C compiler ABI info - done -- Check for working C compiler: D:/lyf_computer_language/msys64/mingw64/bin/cc.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:/lyf_computer_language/msys64/mingw64/bin/c++.exe - skipped -- Detecting CXX compile features -- Detecting CXX compile features - done -- Configuring done -- Generating done -- Build files have been written to: F:/vscode/cpp_projects/cmake-examples/06-installer/deb/build
lyf@DESKTOP-GV2QHKN MINGW64 /f/vscode/cpp_projects/cmake-examples/06-installer/deb/build $ make help The following are some of the valid targets for this Makefile: ... all (the default if no target is provided) ... clean ... depend ... edit_cache ... install ... install/local ... install/strip ... list_install_components ... package ... package_source ... rebuild_cache ... cmake_examples_zip ... cmake_examples_zip_bin ... src/Hello.obj ... src/Hello.i ... src/Hello.s ... src/main.obj ... src/main.i ... src/main.s
lyf@DESKTOP-GV2QHKN MINGW64 /f/vscode/cpp_projects/cmake-examples/06-installer/deb/build $ make package [ 25%] Building CXX object CMakeFiles/cmake_examples_zip.dir/src/Hello.cpp.obj [ 50%] Linking CXX shared library libcmake_examples_zip.dll [ 50%] Built target cmake_examples_zip [ 75%] Building CXX object CMakeFiles/cmake_examples_zip_bin.dir/src/main.cpp.obj [100%] Linking CXX executable cmake_examples_zip_bin.exe [100%] Built target cmake_examples_zip_bin Run CPack packaging tool... CPack: Create package using ZIP CPack: Install projects CPack: - Run preinstall target for: cmake_examples_zip CPack: - Install project: cmake_examples_zip [] CPack: Create package CPack: - package: F:/vscode/cpp_projects/cmake-examples/06-installer/deb/build/cmake_examples_zip-0.2.2-win64.zip generated.