0

I am working on macOS Catalina and I want to compile some c++ code using CMake.

This code needs the library boost that I installed via Brew (brew install boost). In my code I added #include <boost/functional/hash.hpp> to use it.

When I compile I got the following error:

fatal error: boost/functional/hash.hpp: No such file or directory #include <boost/functional/hash.hpp>

Obviously, the compiler does not find the library, so my question is: how can I tell him where the library is? I prefer to set some environment variable, and if it is not possible, how can I do that using CMake?

The file that calls boost:

#include "../../include/util/SubsetInt.hpp"
#include <boost/functional/hash.hpp>

namespace cats {

SubsetInt::SubsetInt(const SubsetInt& p) {
  field = std::vector<uint64_t>();
  for (uint64_t e : p.field) {
    field.push_back(e);
  }
}
}

The CMakeLists.txt:

cmake_minimum_required(VERSION 3.1)
project(catsFramework)


find_package(Boost REQUIRED COMPONENTS system unit_test_framework)
find_package(OpenMP REQUIRED)


set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_FLAGS "${OpenMP_CXX_FLAGS} -O3 -DNDEBUG -Wall")
# set(CMAKE_CXX_FLAGS "${OpenMP_CXX_FLAGS} -O1 -g -Wall")



################### COMBINATOR LIBS ##########################

add_library(LDSCombinator STATIC
    include/combinators/LDSCombinator.hpp
    src/combinators/LDSCombinator.cpp
)

add_library(PrefixEquivalenceCombinator STATIC
    include/combinators/PrefixEquivalenceCombinator.hpp
    src/combinators/PrefixEquivalenceCombinator.cpp
)

add_library(IterativeBroadeningCombinator STATIC
    include/combinators/IterativeBroadeningCombinator.hpp
    src/combinators/IterativeBroadeningCombinator.cpp
)

add_library(ProbingCombinator STATIC
    include/combinators/ProbingCombinator.hpp
    src/combinators/ProbingCombinator.cpp
)

add_library(SolutionPartsCombinator STATIC
    include/combinators/SolutionPartsCombinator.hpp
    src/combinators/SolutionPartsCombinator.cpp
)

add_library(WeightedCombinator STATIC
    include/combinators/WeightedCombinator.hpp
    src/combinators/WeightedCombinator.cpp
)

# all combinators defintion
set( COMBINATORS
    LDSCombinator
    IterativeBroadeningCombinator
    PrefixEquivalenceCombinator
    ProbingCombinator
    SolutionPartsCombinator
    WeightedCombinator
)


############## TREE SEARCH ALGORITHMS #################


add_library(DFS STATIC
    include/treesearch/DFS.hpp
    src/treesearch/DFS.cpp
)

add_library(BFS STATIC
    include/treesearch/BFS.hpp
    src/treesearch/BFS.cpp
)

add_library(AStar STATIC
    include/treesearch/AStar.hpp
    src/treesearch/AStar.cpp
)

add_library(MBAStar STATIC
    include/treesearch/MBAStar.hpp
    src/treesearch/MBAStar.cpp
)

add_library(BeamSearch STATIC
    include/treesearch/BeamSearch.hpp
    src/treesearch/BeamSearch.cpp
)

add_library(GreedyRandom STATIC
    include/treesearch/GreedyRandom.hpp
    src/treesearch/GreedyRandom.cpp
)

add_library(WeightedAStar STATIC
    include/treesearch/WeightedAStar.hpp
    src/treesearch/WeightedAStar.cpp
)

add_library(Greedy STATIC
    include/treesearch/Greedy.hpp
    src/treesearch/Greedy.cpp
)

add_library(IterativeMBAStar STATIC
    include/treesearch/IterativeMBAStar.hpp
    src/treesearch/IterativeMBAStar.cpp
)

add_library(IterativeBeamSearch STATIC
    include/treesearch/IterativeBeamSearch.hpp
    src/treesearch/IterativeBeamSearch.cpp
)

add_library(LDS STATIC
    include/treesearch/LDS.hpp
    src/treesearch/LDS.cpp
)

add_library(BULB STATIC
    include/treesearch/BULB.hpp
    src/treesearch/BULB.cpp
)

add_library(IterativeBroadening STATIC
    include/treesearch/IterativeBroadening.hpp
    src/treesearch/IterativeBroadening.cpp
)

add_library(BranchAndGreed STATIC
    include/treesearch/BranchAndGreed.hpp
    src/treesearch/BranchAndGreed.cpp
)

add_library(ACO STATIC
    include/treesearch/ACO.hpp
    src/treesearch/ACO.cpp
)

add_library(RouletteWheelGreedy STATIC
    include/treesearch/RouletteWheelGreedy.hpp
    src/treesearch/RouletteWheelGreedy.cpp
)

add_library(IterativeRouletteWheelGreedy STATIC
    include/treesearch/IterativeRouletteWheelGreedy.hpp
    src/treesearch/IterativeRouletteWheelGreedy.cpp
)

add_library(MCTS STATIC
    include/treesearch/RouletteWheelGreedy.hpp
    src/treesearch/RouletteWheelGreedy.cpp
    include/treesearch/MCTS.hpp
    src/treesearch/MCTS.cpp
)

add_library(AnytimeColumnSearch STATIC
    include/treesearch/AnytimeColumnSearch.hpp
    src/treesearch/AnytimeColumnSearch.cpp
)


# all tree search algorithms
set( ALGORITHMS
    DFS
    BFS
    AStar
    MBAStar
    BeamSearch
    GreedyRandom
    WeightedAStar
    Greedy
    BranchAndGreed
    IterativeBroadening
    BULB
    LDS
    IterativeBeamSearch
    IterativeMBAStar
    ACO
    RouletteWheelGreedy
    IterativeRouletteWheelGreedy
    MCTS
    AnytimeColumnSearch
)


# base code
add_library(catsBase STATIC
    include/Node.hpp
    include/SearchManager.hpp
    include/Store.hpp
    include/io.hpp
    include/numeric.hpp
    include/SolutionParts.hpp
    include/Combinator.hpp
    include/PrefixEquivalence.hpp

    src/Node.cpp
    src/SearchManager.cpp
    src/io.cpp
)


# util code
add_library(catsUtil STATIC
# set ( UTIL
    include/util/includeAlgorithms.hpp
    include/util/SubsetInt.hpp
    src/util/SubsetInt.cpp
)

set ( ALL
    ${COMBINATORS}
    ${ALGORITHMS}
    catsUtil
    catsBase
)

# UNIT TESTING
enable_testing()

add_executable( testInverseBS.exe unittest/testInverseBS.cpp unittest/InverseGuideBS.hpp)
target_link_libraries(testInverseBS.exe ${ALL})

add_executable( testSubsetInt.exe unittest/testSubsetInt.cpp)
target_link_libraries(testSubsetInt.exe ${ALL})

add_test( InverseBS testInverseBS.exe )
add_test( SubsetInt testSubsetInt.exe )
Kevin
  • 16,549
  • 8
  • 60
  • 74
Aurelien
  • 67
  • 5
  • Please provide the code you have so far, and show us what you tried. There are already lots of answered questions on this site about using Boost with CMake. – Kevin Jan 28 '20 at 17:01
  • You need to add options to your compilers command line to tell it where to search for includes (`-I` with GCC/clang) and where to find the runtime libraries to link with (`-L` with GCC/clang). – Jesper Juhl Jan 28 '20 at 17:03

2 Answers2

1

Usually you would use find_package to find and configure libraries with CMake. Boost provides a CMake configuration file FindBoost.

Here is an example for one library using targets (currently the recommended way)

find_package(Boost REQUIRED COMPONENTS system unit_test_framework)

add_library(LDSCombinator STATIC
    include/combinators/LDSCombinator.hpp
    src/combinators/LDSCombinator.cpp
)
target_link_libraries(LDSCombinator Boost::system)

You have to link all executables and libraries that use Boost.

Thomas Sablik
  • 16,127
  • 7
  • 34
  • 62
1

It looks like you are already using find_package() to tell CMake to look for Boost installed on your system, specifically the system and unit_test_framework components. You now simply need to link the imported Boost targets to your library or executable that is using Boost. For example, link unit_test_framework to your testSubsetInt.exe target like this:

target_link_libraries(testSubsetInt.exe PUBLIC 
    ${ALL} 
    Boost::unit_test_framework
)

This example encompasses the "modern" CMake approach. However, the Boost documentation provides more extended examples for including Boost libraries via CMake here.

Kevin
  • 16,549
  • 8
  • 60
  • 74