Jugal Rawlani
Jugal Rawlani

Reputation: 157

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

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

The bencode/CMakeLists.txt is

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

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

[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.

Help me figure out why?

Upvotes: 0

Views: 976

Answers (2)

Kevin
Kevin

Reputation: 18333

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. To propagate the include directories of the Decoder library to the Torrent library, use the PUBLIC argument in the target_include_directories() call instead.

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}")

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

Upvotes: 0

Zuodian Hu
Zuodian Hu

Reputation: 1009

If you set the location of Decoder.hpp as a public include directory of Decoder, and use target_link_libraries to link Torrent to Decoder, CMake will pick up that you need Torrent to search the proper locations to find Docoder's headers.

Upvotes: 2

Related Questions