CMake Code Snippets
- Show all options
bash
cmake -LAPRIVATE / PUBLIC / INTERFACE
Ref: Transitive Usage Requirements
| Keyword | 自己用 | 傳給依賴自己的 target | 什麼時候用 |
|---|---|---|---|
PRIVATE | ✅ | ❌ | 只有 cpp 用到 |
INTERFACE | ❌ | ✅ | 只有 header 用到 |
PUBLIC | ✅ | ✅ | cpp 和 header 都用到 |
Libraries and targets following PUBLIC are linked to, and are made part of the link interface. Libraries and targets following PRIVATE are linked to, but are not made part of the link interface. Libraries following INTERFACE are appended to the link interface and are not used for linking <target>. -- target_link_libraries
範例:main.c 依賴 hello_world.so,hello_world.so 依賴 hello。

PRIVATE:只有 hello_world.so 看得到 hello。

INTERFACE:hello 傳給 main.c,hello_world.so 本身不 link。

PUBLIC:兩邊都看得到。

Link Protobuf
用 PROTOBUF_LIBRARIES(複數),不是 PROTOBUF_LIBRARY:後者不含 pthread 等依賴,會在 runtime 噴 libprotobuf FATAL ... SCCInfoBase::kRunning。
cmake
find_package(Protobuf 3.14.0 REQUIRED)
target_link_libraries(main proto ${PROTOBUF_LIBRARIES})
# 或用 imported target:protobuf::libprotobuf原文:Protobuf 3.14.0 和 CMake 的一些小細節