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