Mit Cmake möchte ich einige allgemeine Flags sowohl auf ausführbare als auch auf Bibliotheken anwenden.Öffentliche Flags werden nicht auf Bibliotheken angewendet (cmake)
Nun, ich dachte, ich könnte target_compile_options mit PUBLIC-Schlüsselwort verwenden. Ich habe an einem kleinen Beispiel mit einer ausführbaren Datei und einer statischen Bibliothek getestet, die beide nur eine Datei haben (main.c & mylib.c), aber das funktioniert nicht wie erwartet.
Die Wurzel CMakeLists.txt sieht wie folgt aus:
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)
Und die CMakeLists.txt Bibliothek:
cmake_minimum_required(VERSION 3.0)
add_library(mylib STATIC mylib.c)
Die Flagge -Wall nur auf der main.c angewendet wird und nicht in der Bibliotheksdatei (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
Jetzt, wenn Flags werden auf die Bibliothek angewendet, anstatt auf die ausführbare Datei, die funktioniert.
# Add public flags on the library
target_compile_options(mylib PUBLIC -Wall)
ich:
[ 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
Das macht keinen Sinn allgemeine Flags zu setzen, wie die Art von Ziel auf einer Bibliothek.
Wie kann ich allgemeine Flags richtig teilen? Ich weiß, dass ich add_definitions() verwenden kann. Ist es der richtige Weg?
ich auch getestet:
set_target_properties(mytest PROPERTIES COMPILE_FLAGS -Wall)
Aber Flaggen nicht öffentlich sind.
Ja. Wahrscheinlich werde ich ** add_compile_options() ** verwenden, um Flags auf C- und C++ - Compiler anzuwenden. – Patsux
Ich wusste nicht über add_compile_options(). Du hast Recht, es ist eine bessere Option. Ich bearbeite meine Antwort – wasthishelpful