CMake编译命令target_compile_definiti
本文使用的相关软件环境信息如下:
软件名称 | 软件版本 |
---|---|
Linux操作系统 | Ubuntu 22.04 LTS(X64) |
cmake | 3.22.1 |
g++ | 11.4.0 |
-
target_compile_definitions
命令格式
target_compile_definitions(<target> <INTERFACE|PUBLIC|PRIVATE> [items1...] [<INTERFACE|PUBLIC|PRIVATE> [items2...] ...])
该命令添加预编译选项到编译目标中,与add_compile_definitions不同的是,target_compile_definitions针对构建的目标添加预编译选项,而add_compile_definitions是针对源文件添加预编译选项(实际上也隐含对当前构建目标添加预编译选项)。
因此,target_compile_definitions要求构建目标已经存在,也就是被add_executable()或add_library()等函数创建出来。此外,target_compile_definitions还要求对每一个编译选项指定范围,范围有三个选项:PUBLIC、PRIVATE和INTERFACE。通过PUBLIC和PRIVATE范围限定的预编译选项会被添加到构建目标的COMPILE_DEFINITIONS属性中,通过PUBLIC和INTERFACE范围限定的预编译选项会被添加到构建目标的INTERFACE_COMPILE_DEFINITIONS属性中。总结起来就是PUBLIC既给自己用,也给别人用;PRIVATE只给自己用;INTERFACE只给别人用。
当多个构建目标在一起链接的时,最终的可执行文件会将各个构建目标的INTERFACE_COMPILE_DEFINITIONS属性继承过来。
对于-D的前导符和空字符串""会被CMake忽略,例如下面命令完全等价:
target_compile_definitions(target PUBLIC DEF_FLAG) target_compile_definitions(target PUBLIC -DDEF_FLAG) # -D 被移除 target_compile_definitions(target PUBLIC "" DEF_FLAG) # "" 被忽略 target_compile_definitions(target PUBLIC -D DEF_FLAG) # -D 变成 "" 然后被忽略
下面来看一个示例:
1、对于CMakeLists.txt,通过构建一个test库和一个可执行文件main,对test库添加3个编译选项:一个是范围为PRIVATE的PRIVATE_PRINT,一个是范围为PUBLIC的PUBLIC_PRINT,一个范围是INTERFACE的INTERFACE_TEST_DEF;对main可执行文件添加范围为PUBLIC的DEBUG_PRINT。
2、对于源文件:test.cpp在定义PRIVATE_PRINT宏时会打印一条"show test private print...",在定义PUBLIC_PRINT宏时候,会实现一个接口test_print(),该接口打印一条"show test public print...",该接口会在main.cpp中被调用(也会判断宏定义PUBLIC_PRINT),而main.cpp在定义DEBUG_PRINT宏时会打印一条"In debug print mode...",在定义PRIVATE_PRINT宏时会打印"show test private print in main..."。
文件目录结构为:
├── CMakeLists.txt ├── main.cpp ├── test.cpp └── test.h
CMakeLists.txt文件内容:
CMAKE_MINIMUM_REQUIRED(VERSION 3.22) PROJECT(compiler_options) ADD_LIBRARY(test test.cpp) # 为test增加预编译选项 TARGET_COMPILE_DEFINITIONS(test PRIVATE PRIVATE_PRINT PUBLIC PUBLIC_PRINT INTERFACE INTERFACE_TEST_DEF) ADD_EXECUTABLE(main main.cpp) TARGET_COMPILE_DEFINITIONS(main PUBLIC DEBUG_PRINT) TARGET_LINK_LIBRARIES(main test)
test.h文件内容:
#ifndef __TEST_TT__ #define __TEST_TT__ #ifdef PUBLIC_PRINT void test_print(); #endif #endif
test.cpp内容:
#ifdef PUBLIC_PRINT #include "test.h" #include <iostream> void test_print() { std::cout << "test:show test public print..." << std::endl; #ifdef PRIVATE_PRINT std::cout << "test:show test private print..." << std::endl; #endif } #endif
main.cpp内容:
#include "test.h" #include <iostream> int main(int argc, char** argv) { #ifdef DEBUG_PRINT std::cout << "main:In debug print mode..." << std::endl; #endif #ifdef PUBLIC_PRINT test_print(); #endif #ifdef PRIVATE_PRINT std::cout << "main:show test private print in main..." << std::endl; #endif return 0; }
运行
cmake .
和make VERBOSE=1
,关键信息输出如下:…… [ 25%] Building CXX object CMakeFiles/test.dir/test.cpp.o /usr/bin/c++ -DPRIVATE_PRINT -DPUBLIC_PRINT -MD -MT CMakeFiles/test.dir/test.cpp.o -MF CMakeFiles/test.dir/test.cpp.o.d -o CMakeFiles/test.dir/test.cpp.o -c /XXX/test.cpp [ 50%] Linking CXX static library libtest.a …… [ 75%] Building CXX object CMakeFiles/main.dir/main.cpp.o /usr/bin/c++ -DDEBUG_PRINT -DINTERFACE_TEST_DEF -DPUBLIC_PRINT -MD -MT CMakeFiles/main.dir/main.cpp.o -MF CMakeFiles/main.dir/main.cpp.o.d -o CMakeFiles/main.dir/main.cpp.o -c /home/shengyi/code/gitee_repo/projects/cmake/target_compile_definitions/main.cpp [100%] Linking CXX executable main
可以看到,在编译test库时,使用了-DPRIVATE_PRINT -DPUBLIC_PRINT,说明INTERFACE预编译选项test库本身并不使用;在编译main可执行文件时候,使用了-DDEBUG_PRINT -DINTERFACE_TEST_DEF -DPUBLIC_PRINT ,说明test库的PUBLIC和INTERFACE预编译选项传递给了main。