从零开始 CMake 学习笔记 (G)compile-flags
從零開始 CMake 學習筆記 (G)compile-flags
開始前先默念三遍口訣:
- Declare a target
- Declare target’s traits
- It’s all about targets
本系列主要根據GitHub上的 cmake-examples 項目進行翻譯總結,同時對于不清晰的概念及函數進行查閱理解記錄形成。
文章目錄
- 從零開始 CMake 學習筆記 (G)compile-flags
- 1 介紹
- 1.1 文件樹
- 1.2 文件簡介
- 2 概念解析
- 2.1 設置不同目標的編譯標志
- 2.1.1 target_compile_definitions拓展
- 2.2 設置默認的編譯標志
- 3 運行結果
1 介紹
本節展示了如何在 CMake 項目中靈活使用編譯標志。整體的文件架構如下所示:
1.1 文件樹
G-compile-flags $ tree
.
├── CMakeLists.txt
├── main.cpp
1.2 文件簡介
- CMakeLists.txt - 包含了希望運行的 CMake 命令
- main.cpp - main文件
2 概念解析
? ? 首先,什么是編譯標志? 首先,像我們在編譯整個項目時,要用哪一個 C++/C 標準來來編譯每個項目。CMake 中像我們提供了 CMAKE_CXX_FLAGS、CMAKE_C_FLAGS 這樣的標志來選擇C++/C版本的編譯標志。
? ? 其次,對于大型的項目,如果我們想對提升代碼的復用性,在使用不同編譯標志時,分別執行不同的代碼等等,那同樣可以對不同的目標設置自己的編譯標志,利用 target_compile_definitions() 這個函數。目前視野比較窄,暫時沒想到其他用法。
2.1 設置不同目標的編譯標志
在現代CMake中設置C ++標志的推薦方法是專門針對某個目標設置編譯標志,可以通過target_compile_definitions() 函數設置某個目標的編譯標志。
target_compile_definitions(cmake_examples_compile_flagsPRIVATE EX3 )上面的語句就是對 可執行二進制文件cmake_examples_compile_flags設置了編譯標志 EX3 (如果在此之前它有默認編譯標志的話,會繼續追加,而不會更改!)。 PRIVATE、PUBLIC或INTERFACE的含義與之前從零開始 CMake 學習筆記 (C)static-librarytarget_include_directories()函數中說過的一樣,無非就是三種,我用你不能用、我有我不用、你用我用大家用,比較好理解。
2.1.1 target_compile_definitions拓展
主要作用是對個目標設置編譯標志,完整參數展示如下:
target_compile_definitions(<target><INTERFACE|PUBLIC|PRIVATE> [items1...][<INTERFACE|PUBLIC|PRIVATE> [items2...] ...])指定編譯給定目標時要使用的編譯標志。命名目標必須是由 add_executable() 或 add_library() 等命令創建的,并且不能是 ALIAS 目標。
定義、刪除與忽略
當然,可以指定,那就需要可以刪除的方法,下面命令就展示了清除target_compile_definitions() 指定的編譯標志的方法:
2.2 設置默認的編譯標志
CMake同樣提供了 默認的CMAKE_CXX_FLAGS / CMAKE_C_FLAGS的標志來設置整個項目的默認編譯標志。 使用如下:
set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DEX2" CACHE STRING "Set C++ Compiler Flags" FORCE)關于這個 set 命令,我們在上節已經詳細展開過了,也知道要清除 set 設置的值,可以給定一個空值。可以參考上篇博客從零開始 CMake 學習筆記 (F)Build Type。
這里的作用就是強制設置默認C++編譯標志變量為緩存變量,該緩存變量被定義在文件中,相當于全局變量,源文件中也可以使用這個變量。
注意:
? ? 這個變量原本為空的話,會設置為新的給定值,并寫入緩存。如果這個變量不為空的話,會追加的寫入這個份變量的值,并追加的強行寫入緩存。
清除默認標志:
像上面,我們用 set 命令設置了默認的 C++ 編譯器的標志,若需要清除這個標志,目前了解到的有兩種方法:
清除 CMAKE_CXX_FLAGS 變量的值,這里添加了 CACHE 表示同時清除緩存中的值
3 運行結果
G-compile-flags $ mkdir buildG-compile-flags $ cd build/G-compile-flags $ 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/buildG-compile-flags $ 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 学习笔记 (G)compile-flags的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 不过如此! jdk 的安装/配置环境变量
- 下一篇: OctopusWallets(章鱼钱包)