gpt4 book ai didi

c++ - 尝试在 cmake 项目中包含 boost 后出现链接错误

转载 作者:行者123 更新时间:2023-11-30 04:43:43 27 4
gpt4 key购买 nike

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

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

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 ,我注意到有大量变量提供了库目录。也许我用的是旧款?

最佳答案

在这里,我对您的 CMakeLists.txt 进行了一些修改,并添加了一些注释以确保清晰度。

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

关于c++ - 尝试在 cmake 项目中包含 boost 后出现链接错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58083990/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com