1
随着Cmake,我想申请一些通用的标志,可执行文件和库。公共标志不适用于库(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)
但标志是不公开的。
是。可能我会使用** add_compile_options()**在C和C++编译器上应用标志。 – Patsux
我不知道add_compile_options()。你说得对,这是一个更好的选择。我编辑我的答案 – wasthishelpful