gpt4 book ai didi

c++ - 无法将 boost 库与 CMake 链接

转载 作者:塔克拉玛干 更新时间:2023-11-03 07:05:02 28 4
gpt4 key购买 nike

我有一个使用 boost(程序选项模块)的示例 C++ 代码,如下所示:

#include <iostream>

#include <boost/program_options/options_description.hpp>
#include <boost/program_options/parsers.hpp>
#include <boost/program_options/variables_map.hpp>

namespace po = boost::program_options;

int main(int argc, const char* argv[]){
po::options_description description("MyTool Usage");

description.add_options()
("help,h", "Display this help message")
("version,v", "Display the version number");

po::positional_options_description p;

po::variables_map vm;
po::store(po::command_line_parser(argc, argv).options(description).positional(p).run(), vm);
po::notify(vm);

if(vm.count("help")){
std::cout << description;

return 0;
}

if(vm.count("version")){
std::cout << "MyTool Version 1.0" << std::endl;

return 0;
}

return 0;
}

我尝试使用 cmake 进行编译。对应的CMakeLists.txt文件如下所示:

cmake_minimum_required(VERSION 2.6)

set(CMAKE_CXX_LINK_FLAGS "-std=c++11 -g -Wall -fsigned-char -lboost_program_options-mt")

set(BOOST_ROOT /opt/local/include/boost)
set(BOOST_LIBRARYDIR /opt/local/lib)
find_package(Boost COMPONENTS program_options system filesystem REQUIRED)
include_directories(${BOOST_INCLUDE_DIR})
link_libraries(${BOOST_LIBRARIES})

add_executable(testboostpo testboostpo_simple.cpp)
target_link_libraries(testboostpo ${Boost_LIBRARIES})

尽管 cmake . 似乎成功了,而且它还检测到我通常使用的所有 boost 模块(程序选项、系统、文件系统),

$ cmake .
-- The C compiler identification is GNU 5.5.0
-- The CXX compiler identification is GNU 5.5.0
-- Checking whether C compiler has -isysroot
-- Checking whether C compiler has -isysroot - yes
-- Checking whether C compiler supports OSX deployment target flag
-- Checking whether C compiler supports OSX deployment target flag - yes
-- Check for working C compiler: /opt/local/bin/gcc-mp-5
-- Check for working C compiler: /opt/local/bin/gcc-mp-5 -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
-- Checking whether CXX compiler has -isysroot
-- Checking whether CXX compiler has -isysroot - yes
-- Checking whether CXX compiler supports OSX deployment target flag
-- Checking whether CXX compiler supports OSX deployment target flag - yes
-- Check for working CXX compiler: /opt/local/bin/g++-mp-5
-- Check for working CXX compiler: /opt/local/bin/g++-mp-5 -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Boost version: 1.66.0
-- Found the following Boost libraries:
-- program_options
-- system
-- filesystem
-- Configuring done
-- Generating done
-- Build files have been written to: /Users/params/libraries/coding_interview/every_test/boost_test

下一步 make 失败并出现以下错误:$制作

Scanning dependencies of target testboostpo
[ 50%] Building CXX object CMakeFiles/testboostpo.dir/testboostpo_simple.cpp.o
[100%] Linking CXX executable testboostpo
ld: library not found for -lboost_program_options-mt
collect2: error: ld returned 1 exit status
make[2]: *** [testboostpo] Error 1
make[1]: *** [CMakeFiles/testboostpo.dir/all] Error 2
make: *** [all] Error 2

为程序选项链接 boost 库似乎有问题。但是,我可以看到该库安装在 /opt/local/lib/libboost_program_options-mt.dylib

或者,我也尝试过隔离编译,但它也失败了。

$ g++ -L/opt/local/lib testboostpo_simple.cpp -lboost_program_options-mt
Undefined symbols for architecture x86_64:
"boost::program_options::to_internal(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)", referenced from:
std::vector<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::allocator<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > > > boost::program_options::to_internal<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >(std::vector<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::allocator<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > > > const&) in ccg8tggM.o
ld: symbol(s) not found for architecture x86_64
collect2: error: ld returned 1 exit status

$ g++ -L/opt/local/lib testboostpo_simple.cpp /opt/local/lib/libboost_program_options-mt.dylib
Undefined symbols for architecture x86_64:
"boost::program_options::to_internal(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)", referenced from:
std::vector<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::allocator<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > > > boost::program_options::to_internal<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >(std::vector<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::allocator<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > > > const&) in ccg8tggM.o
ld: symbol(s) not found for architecture x86_64
collect2: error: ld returned 1 exit status

我正在使用 gcc5、boost 1.66 和 cmake 3.12(它们都是使用 macports 安装的)。boost 头文件位于:/opt/local/include/boost/并且 boost 库文件位于:/opt/local/lib

尽管提供了路径,这里有没有人知道可能导致 boost 链接仍然失败的原因?这里还有什么遗漏或不正确指定的吗?

谢谢,

最佳答案

因为你已经使用了CMAKE_CXX_LINK_FLAGS,而cmake会先检查它而不是使用你的target_link_libraries,如果你想自己做,你还需要添加-L/opt/local/lib,更简单的方法是让 cmake 为你做。以下是我的示例:

cmake_minimum_required(VERSION 2.6)
project(server CXX)
set(CXX_FLAGS
-g
-Wall
)

# set(Boost_NO_SYSTEM_PATHS ON)
set(BOOST_ROOT /mnt/d/Code/boost)
set(BOOST_LIBRARYDIR /mnt/d/Code/boost/stage/lib)
find_package(Threads REQUIRED)
find_package(Boost 1.68.0 COMPONENTS program_options REQUIRED)
file(GLOB SRC
"*cpp*"
)
if(Boost_FOUND)
include_directories(${Boost_INCLUDE_DIRS})
message(${Boost_PROGRAM_OPTIONS_LIBRARIES})
add_executable(main ${SRC})
target_link_libraries(main Threads::Threads ${Boost_PROGRAM_OPTIONS_LIBRARIES})
endif()

此外,如果单独构建,则应先编译对象再链接,否则可能会变成 undefined reference 。在你的情况下,你应该在最后链接,当你把链接放在前面或尾部时结果会不同,很抱歉我不知道为什么会这样。

关于c++ - 无法将 boost 库与 CMake 链接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52011777/

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