C++ 编译时,CMake不包括链接库的路径
所以我的目录结构是C++ 编译时,CMake不包括链接库的路径,c++,build,cmake,compiler-errors,C++,Build,Cmake,Compiler Errors,所以我的目录结构是 / -- CMakeLists.txt -- bencode/ ---- bType.hpp ---- bType.cpp ---- Decoder.hpp ---- Decoder.cpp ---- CMakeLists.txt -- torrent/ ---- main.cpp ---- Torrent.hpp ---- Torrent.cpp ---- Tracker.hpp ---- Tracker.cpp ---- CMakeLists.txt 根CMakeL
/
-- CMakeLists.txt
-- bencode/
---- bType.hpp
---- bType.cpp
---- Decoder.hpp
---- Decoder.cpp
---- CMakeLists.txt
-- torrent/
---- main.cpp
---- Torrent.hpp
---- Torrent.cpp
---- Tracker.hpp
---- Tracker.cpp
---- CMakeLists.txt
根CMakeLists.txt是
cmake_minimum_required(VERSION 3.16)
project(Torrent VERSION 1.0.0)
add_subdirectory(bencode)
add_subdirectory(torrent)
add_library(
Decoder
Decoder.hpp
Decoder.cpp
)
add_library(
bType
bType.hpp
bType.cpp
)
target_include_directories(bType PRIVATE "${CMAKE_CURRENT_SOURCE/_DIR}")
target_include_directories(Decoder PRIVATE "${CMAKE_CURRENT_SOURCE_DIR}")
add_library(
Torrent
Torrent.hpp
Torrent.cpp
)
add_library(
Tracker
Tracker.hpp
Tracker.cpp
)
add_executable(main main.cpp)
target_link_libraries(
main PRIVATE
Torrent Tracker
Decoder bType
)
bencode/CMakeLists.txt是
cmake_minimum_required(VERSION 3.16)
project(Torrent VERSION 1.0.0)
add_subdirectory(bencode)
add_subdirectory(torrent)
add_library(
Decoder
Decoder.hpp
Decoder.cpp
)
add_library(
bType
bType.hpp
bType.cpp
)
target_include_directories(bType PRIVATE "${CMAKE_CURRENT_SOURCE/_DIR}")
target_include_directories(Decoder PRIVATE "${CMAKE_CURRENT_SOURCE_DIR}")
add_library(
Torrent
Torrent.hpp
Torrent.cpp
)
add_library(
Tracker
Tracker.hpp
Tracker.cpp
)
add_executable(main main.cpp)
target_link_libraries(
main PRIVATE
Torrent Tracker
Decoder bType
)
torrent/CMakeLists.txt是
cmake_minimum_required(VERSION 3.16)
project(Torrent VERSION 1.0.0)
add_subdirectory(bencode)
add_subdirectory(torrent)
add_library(
Decoder
Decoder.hpp
Decoder.cpp
)
add_library(
bType
bType.hpp
bType.cpp
)
target_include_directories(bType PRIVATE "${CMAKE_CURRENT_SOURCE/_DIR}")
target_include_directories(Decoder PRIVATE "${CMAKE_CURRENT_SOURCE_DIR}")
add_library(
Torrent
Torrent.hpp
Torrent.cpp
)
add_library(
Tracker
Tracker.hpp
Tracker.cpp
)
add_executable(main main.cpp)
target_link_libraries(
main PRIVATE
Torrent Tracker
Decoder bType
)
每当我构建时,Torrent.cpp
编译都会失败,因为它包含不在同一目录中的Decoder.hpp
,并且build命令在编译期间不包含路径
[build] ../torrent/Torrent.cpp:5:10: fatal error: Decoder.hpp: No such file or directory
[build] #include <Decoder.hpp>
[build] ^~~~~~~~~~~~~
[build] comp
[build]../torrent/torrent.cpp:5:10:致命错误:Decoder.hpp:没有这样的文件或目录
[构建]#包括
[建造]^~~~~~~~~~~~~
[建造]公司
编译时应该有一个-I
标志,但是没有
如果您将
解码器.hpp
的位置设置为解码器
的公共包含目录,并使用目标链接库
将Torrent
链接到解码器
,请帮助我找出原因,CMake将获取您需要的Torrent
,以搜索适当的位置以查找Docoder
的标题 如果您将Decoder.hpp
的位置设置为Decoder
的公共包含目录,并使用target\u link\u libraries
将Torrent
链接到Decoder
,CMake将选择您需要的Torrent
来搜索适当的位置以查找Docoder
的头 您已将所有库目标链接到main
,但如果Torrent
库和解码器
库之间存在依赖关系,则还应将解码器
链接到Torrent
。要将解码器
库的include目录传播到Torrent
库,请改用目标_include_directories()
调用中的PUBLIC
参数
bencode/CMakeLists.txt:
add_library(
Decoder
Decoder.hpp
Decoder.cpp
)
add_library(
bType
bType.hpp
bType.cpp
)
target_include_directories(bType PRIVATE "${CMAKE_CURRENT_SOURCE_DIR}")
# Use PUBLIC here to propagate the include directories to consumers.
target_include_directories(Decoder PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}")
add_library(
Torrent
Torrent.hpp
Torrent.cpp
)
add_library(
Tracker
Tracker.hpp
Tracker.cpp
)
add_executable(main main.cpp)
# Link Decoder to Torrent here, because Torrent depends on Decoder.
target_link_libraries(Torrent PUBLIC Decoder)
# Link the other libraries to main, Decoder will be brought along with Torrent.
target_link_libraries(
main PRIVATE
Torrent
Tracker
bType
)
torrent/CMakeLists.txt:
add_library(
Decoder
Decoder.hpp
Decoder.cpp
)
add_library(
bType
bType.hpp
bType.cpp
)
target_include_directories(bType PRIVATE "${CMAKE_CURRENT_SOURCE_DIR}")
# Use PUBLIC here to propagate the include directories to consumers.
target_include_directories(Decoder PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}")
add_library(
Torrent
Torrent.hpp
Torrent.cpp
)
add_library(
Tracker
Tracker.hpp
Tracker.cpp
)
add_executable(main main.cpp)
# Link Decoder to Torrent here, because Torrent depends on Decoder.
target_link_libraries(Torrent PUBLIC Decoder)
# Link the other libraries to main, Decoder will be brought along with Torrent.
target_link_libraries(
main PRIVATE
Torrent
Tracker
bType
)
您已将所有库目标链接到
main
,但如果Torrent
库和解码器
库之间存在依赖关系,则还应将解码器链接到Torrent
。要将解码器
库的include目录传播到Torrent
库,请改用目标_include_directories()
调用中的PUBLIC
参数
bencode/CMakeLists.txt:
add_library(
Decoder
Decoder.hpp
Decoder.cpp
)
add_library(
bType
bType.hpp
bType.cpp
)
target_include_directories(bType PRIVATE "${CMAKE_CURRENT_SOURCE_DIR}")
# Use PUBLIC here to propagate the include directories to consumers.
target_include_directories(Decoder PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}")
add_library(
Torrent
Torrent.hpp
Torrent.cpp
)
add_library(
Tracker
Tracker.hpp
Tracker.cpp
)
add_executable(main main.cpp)
# Link Decoder to Torrent here, because Torrent depends on Decoder.
target_link_libraries(Torrent PUBLIC Decoder)
# Link the other libraries to main, Decoder will be brought along with Torrent.
target_link_libraries(
main PRIVATE
Torrent
Tracker
bType
)
torrent/CMakeLists.txt:
add_library(
Decoder
Decoder.hpp
Decoder.cpp
)
add_library(
bType
bType.hpp
bType.cpp
)
target_include_directories(bType PRIVATE "${CMAKE_CURRENT_SOURCE_DIR}")
# Use PUBLIC here to propagate the include directories to consumers.
target_include_directories(Decoder PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}")
add_library(
Torrent
Torrent.hpp
Torrent.cpp
)
add_library(
Tracker
Tracker.hpp
Tracker.cpp
)
add_executable(main main.cpp)
# Link Decoder to Torrent here, because Torrent depends on Decoder.
target_link_libraries(Torrent PUBLIC Decoder)
# Link the other libraries to main, Decoder will be brought along with Torrent.
target_link_libraries(
main PRIVATE
Torrent
Tracker
bType
)