无法将Boost库与CMake链接

时间:2018-08-24 21:10:55

标签: c++ c++11 gcc boost cmake

我有一个使用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失败,并显示以下错误: $ 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链接仍然失败的原因是什么?这里还有其他遗漏或错误指定的内容吗?

谢谢

2 个答案:

答案 0 :(得分:1)

因为您已经使用了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()

此外,如果单独构建它,则应首先编译该对象然后进行链接,否则它可能会变成未定义的引用。对于您来说,应该在最后一个链接,将链接放在前面或后面时结果会有所不同,很抱歉,我不明白为什么会发生这种情况。

答案 1 :(得分:0)

您似乎正在尝试将32位库与64位版本链接。提示是ld: symbol(s) not found for architecture x86_64

链接库要求您匹配确切的构建类型。这是有关链接过程的更多信息。

https://www.boost.org/doc/libs/1_63_0/more/getting_started/windows.html