【问题标题】:linking errors after trying to include boost in a cmake project尝试在 cmake 项目中包含 boost 后链接错误
【发布时间】:2019-09-24 15:48:35
【问题描述】:

我尝试将 boost 包含到我的一个 cmake 项目中,但我在编译该项目时遇到了一些困难。具体来说,我想使用 boost/filesystem.hpp 标头中定义的东西。

我已经使用apt install libboost-all-dev 安装了 boost,并且我已经尝试在必要的地方对 cmake 文件进行必要的调整,这是本网站的一些帖子所推荐的。但是,以下 CMakeLists.txt 文件对我不起作用(有一些,但这是唯一应该引用新的 boost 头文件和库的文件):

project(run_backtest)

add_subdirectory(markets)
set(SOURCE_FILES main.cpp)


set(Boost_USE_STATIC_LIBS OFF) 
set(Boost_USE_MULTITHREADED ON)  
set(Boost_USE_STATIC_RUNTIME OFF) 
find_package(Boost 1.65.1 COMPONENTS system filesystem) 

find_package (Eigen3 3.3 REQUIRED NO_MODULE)

include_directories(${Boost_INCLUDE_DIRS})

link_directories( ${Boost_LIBRARIES})

find_library(mysqlcppconn 1.1.12 REQUIRED)

add_executable(run_backtest ${SOURCE_FILES})
target_link_libraries(run_backtest markets Eigen3::Eigen stdc++fs mysqlcppconn ${Boost_LIBRARIES})
install(TARGETS run_backtest DESTINATION ${MARKETS_INSTALL_BIN_DIR})

我导航到名为~/markets/build/manual 的目录并输入cmake ../..。看起来它有效;它打印出来:

taylor@taylor-XPS-13-9365:~/markets/build/manual$ cmake ../..
-- The C compiler identification is GNU 7.4.0
-- The CXX compiler identification is GNU 7.4.0
-- Check for working C compiler: /usr/bin/cc
-- Check for working C compiler: /usr/bin/cc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
-- Check for working CXX compiler: /usr/bin/c++
-- Check for working CXX compiler: /usr/bin/c++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Boost version: 1.65.1
-- Found the following Boost libraries:
--   system
--   filesystem
/usr/lib/x86_64-linux-gnu/libboost_system.so/usr/lib/x86_64-linux-gnu/libboost_filesystem.so
-- Configuring done
-- Generating done
-- Build files have been written to: /home/taylor/markets/build/manual

然后make && make install 会导致一些undefined reference to 错误。这是第一个复制/粘贴:

../src/markets/libmarkets.a(data_readers.cpp.o): In function `number_of_files_in_directory(boost::filesystem::path)':
data_readers.cpp:(.text+0x28b): undefined reference to `boost::filesystem::detail::directory_iterator_construct(boost::filesystem::directory_iterator&, boost::filesystem::path const&, boost::system::error_code*)'

data_readers.h 文件的顶部如下所示:

.
.
.
#include <string>
#include <vector>
#include <queue>
#include <boost/filesystem.hpp>
.
.
.

查看cmake docs,我注意到库目录中有大量变量。也许我用的是破旧的?

【问题讨论】:

  • ${Boost_LIBRARIES} 变量的内容是什么?您是否尝试打印此内容以查看是否确实找到了库?
  • 您正在为link_directories 使用Boost_LIBRARIES(库名称列表)。我认为你应该在那里使用Boost_LIBRARY_DIRS。另外你应该在寻找提升时将REQUIRED 添加到find_package
  • @squareskittles 我添加了message( ${Boost_LIBRARIES}),并将调用cmake ../.. 的输出添加到问题正文中
  • @VTT 仍然没有骰子。同样的错误
  • 您已经检查了this question 关于未解决的对boost::filesystem 的引用,不是吗?另外,根据错误消息,导致链接器错误的是libmarkets.a,这是在add_subdirectory(markets) 没有 find_package(Boost) 中内置的。这不是很糟糕——静态库在创建时没有链接——但这闻起来不好。

标签: c++ boost cmake ubuntu-18.04


【解决方案1】:

在这里,我对您的 CMakeLists.txt 进行了一些修改,并添加了一些 cmets。

project(run_backtest)

add_subdirectory(markets)
set(SOURCE_FILES main.cpp)

# You shouldn't need these, i got my copy working without them 
# set(Boost_USE_STATIC_LIBS OFF) 
# set(Boost_USE_MULTITHREADED ON)  
# set(Boost_USE_STATIC_RUNTIME OFF) 
# Here i added 'Required' so that the build will fail if it cannot find boost
find_package(Boost 1.65.1 REQUIRED COMPONENTS system filesystem) 

find_package (Eigen3 3.3 REQUIRED NO_MODULE)

# You shouldn't have to use find_library, it's a lower level command
# and find_package should encapsulate it's functionality
find_package (mysqlcppconn 1.1.12 REQUIRED)

add_executable(run_backtest ${SOURCE_FILES})

# Here i replaced the variables (e.g ${BOOST_Libraries}) with Boost::system
# This is called an 'alias'. They're added by the Find_Package() call.
target_link_libraries(run_backtest markets
    Eigen3::Eigen
    stdc++fs
    mysqlcppconn
    Boost::system)
install(TARGETS run_backtest DESTINATION ${MARKETS_INSTALL_BIN_DIR})

【讨论】:

  • 感谢您的帮助(+1)
  • @Taylor 你是否能够让它与我建议的更改一起工作?我想到的另一件事是,如果您的编译目标是 x64 并且您的提升是 x32,有时这可能是问题的原因。
  • 我能够让它工作,但不是来自您的信息。抱歉,如果这听起来很刻薄,但这是我的错……这个问题不合适,我发布了一个后续问题,这让我的一切都得到了解决。
猜你喜欢
  • 1970-01-01
  • 2013-10-25
  • 2022-11-27
  • 2020-05-19
  • 2011-10-01
  • 1970-01-01
  • 2014-09-26
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多