1. 重點關注對象屬性(Property)的編譯標誌(Compile flags)和鏈接標誌(Link flags):
- Compile flags
- [INTERFACE_]INCLUDE_DIRECTORIES
- [INTERFACE_]COMPILE_DEFINITIONS
- [INTERFACE_]COMPILE_OPTIONS
- Link flags
- [INTERFACE_]LINK_LIBRARIES
- [INTERFACE_]LINK_OPTIONS
2. 傳播機制按照作用先後分爲兩個層次:
2.1 CMake 對象屬性設置時引入PRIVATE,INTERFACE 和PUBLIC關鍵詞,用以控制對象本身的屬性。
例如:
target_compile_definitions(targetA PRIVATE FOO)
target_compile_definitions(targetA INTERFACE BAR)
target_compile_definitions(targetA PUBLIC FOOBAR)
Property | targetA |
---|---|
COMPILE_DEFINITIONS | FOO FOOBAR |
INTERFACE_COMPILE_DEFINITIONS | BAR FOOBAR |
2.2 當調用target_link_libraries時引入PRIVATE, INTERFACE和PUBLIC關鍵詞,用以控制屬性的傳遞。
target_link_libraries(targetB PRIVATE targetA)
target_link_libraries(targetC PUBLIC targetB)
Property | targetC | targetB | targetA |
---|---|---|---|
COMPILE_DEFINITIONS |
|
BAR FOOBAR | FOO FOOBAR |
INTERFACE_COMPILE_DEFINITIONS | BAR FOOBAR |
target_link_libraries(targetB INTERFACE targetA)
target_link_libraries(targetC PUBLIC targetB)
Property | targetC | targetB | targetA |
---|---|---|---|
COMPILE_DEFINITIONS | BAR FOOBAR | FOO FOOBAR | |
INTERFACE_COMPILE_DEFINITIONS | BAR FOOBAR | BAR FOOBAR | BAR FOOBAR |
target_link_libraries(targetB PUBLIC targetA)
target_link_libraries(targetC PUBLIC targetB)
Property | targetC | targetB | targetA |
---|---|---|---|
COMPILE_DEFINITIONS | BAR FOOBAR | BAR FOOBAR | FOO FOOBAR |
INTERFACE_COMPILE_DEFINITIONS | BAR FOOBAR | BAR FOOBAR | BAR FOOBAR |