Search code examples
c++boostcmakec++17

Lib boost not being accessible outside main


I'm trying to use Boost in my project, but it is only being accessed inside main.cpp. If I try to include it in another file I get an error. I don't know if it's something that must be specified in the CMakeLists.txt.

# CMakeLists.txt
...

include (cmake/CPM.cmake)

CPMAddPackage(
  NAME PackageProject.cmake
  GITHUB_REPOSITORY TheLartians/PackageProject.cmake
  VERSION 1.4
)

# ------------------------------   Boost    ----------------------------------
CPMAddPackage("gh:Orphis/boost-cmake#[email protected]")
# -----------------------------------------------------------------------------

...

# Core
add_library(
  ProjectCore
  include/ProjectCore.hpp
  src/ProjectCore.cpp
  )

# Utils
add_library(
  Utils
  include/utils.hpp
  src/utils.cpp
  )

...

target_include_directories(ProjectCore PUBLIC include/)
target_include_directories(Utils PUBLIC include/)

add_executable(Project src/main.cpp)
target_link_libraries(Project
  PUBLIC Boost::system
  PRIVATE ProjectCore
  PRIVATE Utils
  )
# -----------------------------------------------------------------------------
set_target_properties(Project PROPERTIES CXX_STANDARD 17)

I proceed with the following commands:

core$ md build && cd build && cmake ..
core$ bear -- make -C build
core$ ./build/Project

Trying to include boost on main.cpp

Trying to include boost on main.cpp

Trying to include boost on utils.cpp Trying to include boost on utils.cpp


Solution

  • As Tsyvarev pointed out in the main message thread, I forgot to link Boost with the other libs I wanted to use. I ended up being able to run it by adding the following line

    target_link_libraries(Utils PRIVATE Boost::system)