我有一个CMake-libraryadd_library(helpers helpers.h)
,我想通过target_include_directories
包括源文件。然后我必须在参数PRIVATE
,PUBLIC
和INTERFACE
之间进行选择。
有什么区别?
PRIVATE
: include只能被helper库自己使用。PUBLIC
:包含可以被helper库本身和任何使用helper库的目标使用,例如通过target_link_libraries(MainApplication PUBLIC libhelpers)
。INTERFACE
:包含不能被helper库使用,只能被使用helper库的目标使用。
这个<</ol>strong>示例说明可能的导入。结构:
│ CMakeLists.txt
│ helper.cpp
│ main.cpp
│
├───details_interface
│ details_interface.cpp
│
├───details_private
│ details_private.cpp
│
└───details_public
details_public.cpp
CMakeLists.txt:
cmake_minimum_required(VERSION 3.1)
set(CMAKE_CXX_STANDARD 17)
project(cmake_experiment)
add_executable(cmake_experiment main.cpp)
target_link_libraries(cmake_experiment libhelper)
add_library(libhelper helper.cpp)
target_include_directories(libhelper
INTERFACE details_interface
PUBLIC details_public
PRIVATE details_private)
main.cpp:
#include <iostream>
#include "details_public.cpp"
#include "details_interface.cpp"
// fatal error: details_private.cpp: No such file or directory:
// #include "details_private.cpp"
int main() {
std::cout << "Hello, World!" << std::endl;
return 0;
}
helper.cpp:
#include "details_public.cpp"
#include "details_private.cpp"
// fatal error: details_interface.cpp: No such file or directory:
// #include "details_interface.cpp"