CMake基础 第7节 编译标志
引言#
CMake支持以多種不同方式設置編譯標志:
- 使用target_compile_definitions()函數
- 使用CMAKE_C_FLAGS和CMAKE_CXX_FLAGS變量。
本教程中的文件如下:
$ tree . ├── CMakeLists.txt ├── main.cpp-
[CMakeLists.txt] - 包含要運行的CMake命令
cmake_minimum_required(VERSION 3.5)# Set a default C++ compile flag set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DEX2" CACHE STRING "Set C++ Compiler Flags" FORCE)# Set the project name project (compile_flags)# Add an executable add_executable(cmake_examples_compile_flags main.cpp)target_compile_definitions(cmake_examples_compile_flags PRIVATE EX3 ) -
[main.cpp] - 具有main的源文件
#include <iostream>int main(int argc, char *argv[]) {std::cout << "Hello Compile Flags!" << std::endl;// only print if compile flag set #ifdef EX2std::cout << "Hello Compile Flag EX2!" << std::endl; #endif#ifdef EX3std::cout << "Hello Compile Flag EX3!" << std::endl; #endifreturn 0; }
概念#
設置每個目標的C++標志#
在現代CMake中設置C++標志的推薦方式是使用每個目標的標志,這些標志可以通過target_compile_definitions()函數的作用域(或者說接口范圍)遞到其他目標(INTERFACE或PUBLIC)。這將填充庫的INTERFACE_COMPILE_DEFINITIONS,并根據作用域將定義傳遞到鏈接的目標。
target_compile_definitions(cmake_examples_compile_flagsPRIVATE EX3 )這將導致編譯器在編譯目標時添加定義 -DEX3。
如果目標是庫,并且已經選擇了作用域PUBLIC或者INTERFACE,則該定義也將包含在鏈接該目標的任何可執行文件中。
對于編譯器選項,你還可以使用target_compile_options()函數。
設置默認C++標志#
CMAKE_CXX_FLAGS的默認值為空或包含生成類型的相應標志。
要設置其他默認編譯標志,可以將以下內容添加到頂級CMakeLists.txt。
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設置鏈接器標志
一旦設置,CMAKE_C_FLAGS和CMAKE_CXX_FLAGS將為該目錄或任何包含的子目錄中的所有目標全局設置編譯器標志/定義。現在不建議將此方法用于一般用途,最好使用target_compile_definitions函數。
設置CMake標志#
與構建類型類似,可以使用以下方法設置全局C++編譯器標志。
-
使用GUI工具,如ccmake/cmake-gui
-
傳遞到cmake
構建示例#
$ 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/G-compile-flags/build$ make VERBOSE=1 /usr/bin/cmake -H/home/matrim/workspace/cmake-examples/01-basic/G-compile-flags -B/home/matrim/workspace/cmake-examples/01-basic/G-compile-flags/build --check-build-system CMakeFiles/Makefile.cmake 0 /usr/bin/cmake -E cmake_progress_start /home/matrim/workspace/cmake-examples/01-basic/G-compile-flags/build/CMakeFiles /home/matrim/workspace/cmake-examples/01-basic/G-compile-flags/build/CMakeFiles/progress.marks make -f CMakeFiles/Makefile2 all make[1]: Entering directory `/home/matrim/workspace/cmake-examples/01-basic/G-compile-flags/build' make -f CMakeFiles/cmake_examples_compile_flags.dir/build.make CMakeFiles/cmake_examples_compile_flags.dir/depend make[2]: Entering directory `/home/matrim/workspace/cmake-examples/01-basic/G-compile-flags/build' cd /home/matrim/workspace/cmake-examples/01-basic/G-compile-flags/build && /usr/bin/cmake -E cmake_depends "Unix Makefiles" /home/matrim/workspace/cmake-examples/01-basic/G-compile-flags /home/matrim/workspace/cmake-examples/01-basic/G-compile-flags /home/matrim/workspace/cmake-examples/01-basic/G-compile-flags/build /home/matrim/workspace/cmake-examples/01-basic/G-compile-flags/build /home/matrim/workspace/cmake-examples/01-basic/G-compile-flags/build/CMakeFiles/cmake_examples_compile_flags.dir/DependInfo.cmake --color= Dependee "/home/matrim/workspace/cmake-examples/01-basic/G-compile-flags/build/CMakeFiles/cmake_examples_compile_flags.dir/DependInfo.cmake" is newer than depender "/home/matrim/workspace/cmake-examples/01-basic/G-compile-flags/build/CMakeFiles/cmake_examples_compile_flags.dir/depend.internal". Dependee "/home/matrim/workspace/cmake-examples/01-basic/G-compile-flags/build/CMakeFiles/CMakeDirectoryInformation.cmake" is newer than depender "/home/matrim/workspace/cmake-examples/01-basic/G-compile-flags/build/CMakeFiles/cmake_examples_compile_flags.dir/depend.internal". Scanning dependencies of target cmake_examples_compile_flags make[2]: Leaving directory `/home/matrim/workspace/cmake-examples/01-basic/G-compile-flags/build' make -f CMakeFiles/cmake_examples_compile_flags.dir/build.make CMakeFiles/cmake_examples_compile_flags.dir/build make[2]: Entering directory `/home/matrim/workspace/cmake-examples/01-basic/G-compile-flags/build' /usr/bin/cmake -E cmake_progress_report /home/matrim/workspace/cmake-examples/01-basic/G-compile-flags/build/CMakeFiles 1 [100%] Building CXX object CMakeFiles/cmake_examples_compile_flags.dir/main.cpp.o /usr/bin/c++ -DEX2 -o CMakeFiles/cmake_examples_compile_flags.dir/main.cpp.o -c /home/matrim/workspace/cmake-examples/01-basic/G-compile-flags/main.cpp Linking CXX executable cmake_examples_compile_flags /usr/bin/cmake -E cmake_link_script CMakeFiles/cmake_examples_compile_flags.dir/link.txt --verbose=1 /usr/bin/c++ -DEX2 CMakeFiles/cmake_examples_compile_flags.dir/main.cpp.o -o cmake_examples_compile_flags -rdynamic make[2]: Leaving directory `/home/matrim/workspace/cmake-examples/01-basic/G-compile-flags/build' /usr/bin/cmake -E cmake_progress_report /home/matrim/workspace/cmake-examples/01-basic/G-compile-flags/build/CMakeFiles 1 [100%] Built target cmake_examples_compile_flags make[1]: Leaving directory `/home/matrim/workspace/cmake-examples/01-basic/G-compile-flags/build' /usr/bin/cmake -E cmake_progress_start /home/matrim/workspace/cmake-examples/01-basic/G-compile-flags/build/CMakeFiles 0總結
以上是生活随笔為你收集整理的CMake基础 第7节 编译标志的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: CMake基础 第5节 安装项目
- 下一篇: CMake基础 第8节 包含第三方库