首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何允许编译器查找通过Brew安装的库

如何允许编译器查找通过Brew安装的库
EN

Stack Overflow用户
提问于 2020-01-28 16:57:58
回答 2查看 595关注 0票数 0

我正在编写macOS Catalina,我想使用CMake编译一些c++代码。

这段代码需要我通过Brew ()安装的库boost。在我的代码中,我添加了#include <boost/functional/hash.hpp>来使用它。

当我编译时,我得到了以下错误:

代码语言:javascript
复制
fatal error: boost/functional/hash.hpp: No such file or directory #include <boost/functional/hash.hpp>

显然,编译器找不到库,所以我的问题是:如何告诉他库在哪里?我更喜欢设置一些环境变量,如果不可能,我如何使用CMake来实现呢?

调用boost的文件:

代码语言:javascript
复制
#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);
  }
}
}

CMakeLists.txt:

代码语言:javascript
复制
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 )
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2020-01-28 17:11:52

通常,您会使用套餐查找和配置CMake库。Boost提供了一个CMake配置文件FindBoost

下面是一个使用目标的库的示例(当前推荐的方法)

代码语言:javascript
复制
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)

您必须链接所有使用Boost的可执行文件和库。

票数 1
EN

Stack Overflow用户

发布于 2020-01-28 17:15:57

看起来您已经在使用find_package()来告诉CMake寻找安装在系统上的Boost,特别是systemunit_test_framework组件。现在只需将导入的Boost目标链接到使用Boost的库或可执行文件。例如,将unit_test_framework链接到testSubsetInt.exe目标,如下所示:

代码语言:javascript
复制
target_link_libraries(testSubsetInt.exe PUBLIC 
    ${ALL} 
    Boost::unit_test_framework
)

此示例包含“现代”CMake方法。但是,Boost文档提供了通过CMake 这里包含Boost库的更多扩展示例。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59953609

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档