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
)