【发布时间】:2016-12-02 13:15:58
【问题描述】:
使用 Cmake,我想将一些通用标志应用于可执行文件和库。
好吧,我想我可以通过使用 PUBLIC 关键字来使用 target_compile_options。我在一个带有可执行文件和静态库的小示例上进行了测试,两者都只有一个文件(main.c 和 mylib.c),但这并没有按预期工作。
根 CMakeLists.txt 如下所示:
cmake_minimum_required(VERSION 3.0)
# Add the library
add_subdirectory(mylib)
# Create the executable
add_executable(mytest main.c)
# Link the library
target_link_libraries(mytest mylib)
# Add public flags
target_compile_options(mytest PUBLIC -Wall)
还有库的 CMakeLists.txt:
cmake_minimum_required(VERSION 3.0)
add_library(mylib STATIC mylib.c)
-Wall 标志仅应用于 main.c 而不是库文件 (mylib.c):
[ 25%] Building C object mylib/CMakeFiles/mylib.dir/mylib.c.o
cd /patsux/Programmation/Repositories/test-cmake-public/build/mylib && /usr/lib/hardening-wrapper/bin/cc -o CMakeFiles/mylib.dir/mylib.c.o -c /patsux/Programmation/Repositories/test-cmake-public/mylib/mylib.c
[ 50%] Linking C static library libmylib.a
[ 25%] Building C object CMakeFiles/mytest.dir/main.c.o
/usr/lib/hardening-wrapper/bin/cc -Wall -o CMakeFiles/mytest.dir/main.c.o -c /patsux/Programmation/Repositories/test-cmake-public/main.c
现在,如果在库而不是可执行文件上应用标志,则可以。
# Add public flags on the library
target_compile_options(mylib PUBLIC -Wall)
我明白了:
[ 25%] Building C object mylib/CMakeFiles/mylib.dir/mylib.c.o
cd /patsux/Programmation/Repositories/test-cmake-public/build/mylib && /usr/lib/hardening-wrapper/bin/cc -Wall -o CMakeFiles/mylib.dir/mylib.c.o -c /patsux/Programmation/Repositories/test-cmake-public/mylib/mylib.c
[ 50%] Linking C static library libmylib.a
[ 75%] Building C object CMakeFiles/mytest.dir/main.c.o
/usr/lib/hardening-wrapper/bin/cc -Wall -o CMakeFiles/mytest.dir/main.c.o -c /patsux/Programmation/Repositories/test-cmake-public/main.c
[100%] Linking C executable mytest
在库上设置诸如目标类型之类的通用标志是没有意义的。
如何正确共享通用标志?我知道我可以使用 add_definitions()。方法对吗?
我也测试过:
set_target_properties(mytest PROPERTIES COMPILE_FLAGS -Wall)
但标志是不公开的。
【问题讨论】: