简体   繁体   English

CMake 编译时不包含链接库的路径

[英]CMake does not include path to link libraries when compiling

So my directory structure is like所以我的目录结构就像

/
-- 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

The root CMakeLists.txt is根 CMakeLists.txt 是

cmake_minimum_required(VERSION 3.16)
project(Torrent VERSION 1.0.0)
add_subdirectory(bencode)
add_subdirectory(torrent)

The bencode/CMakeLists.txt is 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}")
target_include_directories(Decoder PRIVATE "${CMAKE_CURRENT_SOURCE_DIR}")

The torrent/CMakeLists.txt is种子/CMakeLists.txt 是

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
)

Whenever I build, the Torrent.cpp compilation fails, as it includes Decoder.hpp which is not in the same directory, and the build command does not include the path during compilation每当我构建时, Torrent.cpp编译都会失败,因为它包含不在同一目录中的Decoder.hpp ,并且构建命令在编译期间不包含路径

[build] ../torrent/Torrent.cpp:5:10: fatal error: Decoder.hpp: No such file or directory
[build]  #include <Decoder.hpp>
[build]           ^~~~~~~~~~~~~
[build] comp

There should have been a -I flag while compiling but there isn't.编译时应该有一个-I标志,但没有。

Help me figure out why?帮我看看为什么?

如果您将Decoder.hpp的位置设置为Decoder公共包含目录,并使用target_link_librariesTorrent链接到Decoder ,CMake 会发现您需要Torrent来搜索适当的位置以找到Docoder的标题。

You have linked all of the library targets to main , but if there is a dependency between the Torrent library and the Decoder library, you should link Decoder to Torrent also.您已将所有库目标链接到main ,但如果Torrent库和Decoder库之间存在依赖关系,您还应该Decoder链接Torrent To propagate the include directories of the Decoder library to the Torrent library, use the PUBLIC argument in the target_include_directories() call instead.要将Decoder库的包含目录传播到Torrent库,请改用target_include_directories()调用中的PUBLIC参数。

bencode/CMakeLists.txt :本代码/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}")

torrent/CMakeLists.txt :种子/CMakeLists.txt

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
)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM