我的简单项目包含2个“库”a
和b
以及可执行文件c
。我将库a
(PUBLIC)的包含路径设置为X
(出于测试目的,实际上没有这样的路径)。
现在,b
私下与a
和c
链接b
。我希望在编译c.cpp
时X不在包含路径中,因为“它不应该知道a
,因为它在b
中是私有的”但显然它就在这里。
所以我的问题是它的意图行为与否以及原因。
我的代码:
a.cpp
void a() {}
b.cpp
void b() {}
c.cpp
int main() {}
的CMakeLists.txt
cmake_minimum_required(VERSION 2.8)
add_library(a a.cpp)
target_include_directories(a PUBLIC X)
add_library(b b.cpp)
target_link_libraries(b PRIVATE a)
add_executable(c c.cpp)
target_link_libraries(c PUBLIC b)
(详细)编译日志:
/usr/local/Cellar/cmake/3.8.1/bin/cmake -H/Users/alexeyd/test/dir -B/Users/alexeyd/test/dir/build --check-build-system CMakeFiles/Makefile.cmake 0
/Applications/Xcode.app/Contents/Developer/usr/bin/make -f CMakeFiles/Makefile2 c
/usr/local/Cellar/cmake/3.8.1/bin/cmake -H/Users/alexeyd/test/dir -B/Users/alexeyd/test/dir/build --check-build-system CMakeFiles/Makefile.cmake 0
/usr/local/Cellar/cmake/3.8.1/bin/cmake -E cmake_progress_start /Users/alexeyd/test/dir/build/CMakeFiles 6
/Applications/Xcode.app/Contents/Developer/usr/bin/make -f CMakeFiles/Makefile2 CMakeFiles/c.dir/all
/Applications/Xcode.app/Contents/Developer/usr/bin/make -f CMakeFiles/a.dir/build.make CMakeFiles/a.dir/depend
cd /Users/alexeyd/test/dir/build && /usr/local/Cellar/cmake/3.8.1/bin/cmake -E cmake_depends "Unix Makefiles" /Users/alexeyd/test/dir /Users/alexeyd/test/dir /Users/alexeyd/test/dir/build /Users/alexeyd/test/dir/build /Users/alexeyd/test/dir/build/CMakeFiles/a.dir/DependInfo.cmake --color=
Dependee "/Users/alexeyd/test/dir/build/CMakeFiles/a.dir/DependInfo.cmake" is newer than depender "/Users/alexeyd/test/dir/build/CMakeFiles/a.dir/depend.internal".
Dependee "/Users/alexeyd/test/dir/build/CMakeFiles/CMakeDirectoryInformation.cmake" is newer than depender "/Users/alexeyd/test/dir/build/CMakeFiles/a.dir/depend.internal".
Scanning dependencies of target a
/Applications/Xcode.app/Contents/Developer/usr/bin/make -f CMakeFiles/a.dir/build.make CMakeFiles/a.dir/build
[ 16%] Building CXX object CMakeFiles/a.dir/a.cpp.o
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/c++ -I/Users/alexeyd/test/dir/X -o CMakeFiles/a.dir/a.cpp.o -c /Users/alexeyd/test/dir/a.cpp
[ 33%] Linking CXX static library liba.a
/usr/local/Cellar/cmake/3.8.1/bin/cmake -P CMakeFiles/a.dir/cmake_clean_target.cmake
/usr/local/Cellar/cmake/3.8.1/bin/cmake -E cmake_link_script CMakeFiles/a.dir/link.txt --verbose=1
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/ar qc liba.a CMakeFiles/a.dir/a.cpp.o
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/ranlib liba.a
[ 33%] Built target a
/Applications/Xcode.app/Contents/Developer/usr/bin/make -f CMakeFiles/b.dir/build.make CMakeFiles/b.dir/depend
cd /Users/alexeyd/test/dir/build && /usr/local/Cellar/cmake/3.8.1/bin/cmake -E cmake_depends "Unix Makefiles" /Users/alexeyd/test/dir /Users/alexeyd/test/dir /Users/alexeyd/test/dir/build /Users/alexeyd/test/dir/build /Users/alexeyd/test/dir/build/CMakeFiles/b.dir/DependInfo.cmake --color=
Dependee "/Users/alexeyd/test/dir/build/CMakeFiles/b.dir/DependInfo.cmake" is newer than depender "/Users/alexeyd/test/dir/build/CMakeFiles/b.dir/depend.internal".
Dependee "/Users/alexeyd/test/dir/build/CMakeFiles/CMakeDirectoryInformation.cmake" is newer than depender "/Users/alexeyd/test/dir/build/CMakeFiles/b.dir/depend.internal".
Scanning dependencies of target b
/Applications/Xcode.app/Contents/Developer/usr/bin/make -f CMakeFiles/b.dir/build.make CMakeFiles/b.dir/build
[ 50%] Building CXX object CMakeFiles/b.dir/b.cpp.o
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/c++ -I/Users/alexeyd/test/dir/X -o CMakeFiles/b.dir/b.cpp.o -c /Users/alexeyd/test/dir/b.cpp
[ 66%] Linking CXX static library libb.a
/usr/local/Cellar/cmake/3.8.1/bin/cmake -P CMakeFiles/b.dir/cmake_clean_target.cmake
/usr/local/Cellar/cmake/3.8.1/bin/cmake -E cmake_link_script CMakeFiles/b.dir/link.txt --verbose=1
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/ar qc libb.a CMakeFiles/b.dir/b.cpp.o
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/ranlib libb.a
[ 66%] Built target b
/Applications/Xcode.app/Contents/Developer/usr/bin/make -f CMakeFiles/c.dir/build.make CMakeFiles/c.dir/depend
cd /Users/alexeyd/test/dir/build && /usr/local/Cellar/cmake/3.8.1/bin/cmake -E cmake_depends "Unix Makefiles" /Users/alexeyd/test/dir /Users/alexeyd/test/dir /Users/alexeyd/test/dir/build /Users/alexeyd/test/dir/build /Users/alexeyd/test/dir/build/CMakeFiles/c.dir/DependInfo.cmake --color=
Dependee "/Users/alexeyd/test/dir/build/CMakeFiles/c.dir/DependInfo.cmake" is newer than depender "/Users/alexeyd/test/dir/build/CMakeFiles/c.dir/depend.internal".
Dependee "/Users/alexeyd/test/dir/build/CMakeFiles/CMakeDirectoryInformation.cmake" is newer than depender "/Users/alexeyd/test/dir/build/CMakeFiles/c.dir/depend.internal".
Scanning dependencies of target c
/Applications/Xcode.app/Contents/Developer/usr/bin/make -f CMakeFiles/c.dir/build.make CMakeFiles/c.dir/build
[ 83%] Building CXX object CMakeFiles/c.dir/c.cpp.o
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/c++ -I/Users/alexeyd/test/dir/X -o CMakeFiles/c.dir/c.cpp.o -c /Users/alexeyd/test/dir/c.cpp
[100%] Linking CXX executable c
/usr/local/Cellar/cmake/3.8.1/bin/cmake -E cmake_link_script CMakeFiles/c.dir/link.txt --verbose=1
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/c++ -Wl,-search_paths_first -Wl,-headerpad_max_install_names CMakeFiles/c.dir/c.cpp.o -o c libb.a liba.a
[100%] Built target c
/usr/local/Cellar/cmake/3.8.1/bin/cmake -E cmake_progress_start /Users/alexeyd/test/dir/build/CMakeFiles 0
日志的有趣部分:
[ 83%] Building CXX object CMakeFiles/c.dir/c.cpp.o
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/c++ -I/Users/alexeyd/test/dir/X -o CMakeFiles/c.dir/c.cpp.o -c /Users/alexeyd/test/dir/c.cpp
我使用CMake 3.8.1
答案 0 :(得分:3)
如果指定最小CMake版本> = 2.8.12:
,则不会出现奇怪的行为cmake_minimum_required(VERSION 2.8.12)
在CMake 2.8.12之前, target_include_directories
和PRIVATE
/ PUBLIC
区别不存在。我的看法是,对于早于CMake的版本,CMake选择默认创建所有内容PUBLIC
。