ruslo / CGold

:whale2: The Hitchhiker’s Guide to the CMake
https://cgold.readthedocs.io
BSD 2-Clause "Simplified" License
523 stars 56 forks source link

PUBLIC/PRIVATE linking #37

Open ruslo opened 5 years ago

ruslo commented 5 years ago
cmake_minimum_required(VERSION 3.2)
project(foo)

add_library(foo foo.cpp)
target_compile_definitions(foo PUBLIC FOO_FEATURE)

add_library(baz_1 baz_1.cpp)
target_link_libraries(baz_1 PUBLIC foo) # Case 1

add_library(baz_2 baz_2.cpp)
target_link_libraries(baz_2 PRIVATE foo) # Case 2

add_executable(boo_1 boo_1.cpp)
target_link_libraries(boo_1 PRIVATE baz_1)

add_executable(boo_2 boo_2.cpp)
target_link_libraries(boo_2 PRIVATE baz_2)
/usr/bin/c++ -DFOO_FEATURE -o foo.cpp.o -c foo.cpp

/usr/bin/c++ -DFOO_FEATURE -o baz_1.cpp.o -c baz_1.cpp
/usr/bin/c++ -DFOO_FEATURE -o boo_1.cpp.o -c boo_1.cpp # FOO_FEATURE added, foo headers can be used in boo_1.cpp
/usr/bin/c++ boo_1.cpp.o -o boo_1 libbaz_1.a libfoo.a  # libfoo.a added, linking is static, we may need symbols

/usr/bin/c++ -DFOO_FEATURE -o baz_2.cpp.o -c baz_2.cpp
/usr/bin/c++ -o boo_2.cpp.o -c boo_2.cpp               # FOO_FEATURE is missing, can't use foo headers in boo_2.cpp
/usr/bin/c++ boo_2.cpp.o -o boo_2 libbaz_2.a libfoo.a  # libfoo.a added even if PRIVATE used