在cmake配置后的链接阶段不尊重BOOST_ROOT

时间:2019-01-15 18:44:11

标签: c++ boost cmake

对冗长的标题表示歉意。

由于cmake目录中存在boost库,我正在努力将boost与/usr/lib64链接。我的助推器在另一个位置编译,我用cmake指向BOOST_ROOT。意识到潜在的问题,我正在设置最低版本和Boost_NO_SYSTEM_PATH。配置阶段工​​作正常,但是在链接时出现错误:

test.cpp:(.text._ZN5boost15program_options25basic_command_line_parserIcEC2EiPKPKc[_ZN5boost15program_options25basic_command_line_parserIcEC5EiPKPKc]+0xa8): undefined reference to `boost::program_options::detail::cmdline::cmdline(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&)'

这显然是选择错误库的问题,我可以通过make VERBOSE=2看到g++行不遵守我先前在BOOST_ROOT中发现的boost设置

g++ CMakeFiles/test.dir/test.cpp.o -o test -rdynamic -lboost_program_options-mt

但我期望遵循以下原则:

g++ ... -L/path/to/my/own/boost/lib -lboost_program_options-mt

作为“调试”步骤,我在cmake中用Boost_LIBRARY_DIRS打印了一条消息,并且可以看到/path/to/my/own/boost/lib。当我“手动”添加-L标志时,链接有效,这就是我知道系统库仍在干扰的方式。另外,我的*LIBRARY_PATH仅指向/path/to/my/own/boost/lib

也许,这不是FindBoost模块的错误,但我发现很难相信。在我看来,cmake还有一些我不理解的专业?为什么在此实例或指向特定文件的链接中未生成-L标志?请告知。

这是我的CMakeLists.txt

cmake_minimum_required(VERSION 2.8)

set(Boost_NO_SYSTEM_PATHS ON)
find_package(Boost 1.67.0 REQUIRED COMPONENTS program_options)

include_directories(${Boost_INCLUDE_DIR})

add_executable(test test.cpp)

target_link_libraries(test LINK_PUBLIC ${Boost_PROGRAM_OPTIONS_LIBRARY})

还有我的程序

#include <iostream>
#include <string>

#include <boost/program_options.hpp>

namespace po=boost::program_options;

int main( int argc, char* argv[]) 
{ 
    po::options_description options_description;
    po::positional_options_description positional_options_description;
    po::variables_map variables_map;


    options_description.add_options()
    ("help,h", "print usage message");
    po::store(po::command_line_parser(argc, argv).options(
        options_description).positional(positional_options_description).run(),
        variables_map);

    return 0; 
} 

其他相关输出

我正在使用messagecmake之后的find_package配置步骤中提取变量:

${Boost_INCLUDE_DIR} /path/to/my/own/boost/include
${Boost_LIBRARY_DIRS} /path/to/my/own/boost/lib
${Boost_LIBRARIES} /path/to/my/own/boost/lib/libboost_program_options-mt.so
${Boost_PROGRAM_OPTIONS_LIBRARY} /path/to/my/own/boost/lib/libboost_program_options-mt.so

2 个答案:

答案 0 :(得分:0)

我不知道您使用的是哪个CMake版本,但是您是否尝试使用Boost导入的目标而不是老式的CMake变量:

即 更换: target_link_libraries(test LINK_PUBLIC ${Boost_PROGRAM_OPTIONS_LIBRARY}) 通过 target_link_libraries(test LINK_PUBLIC Boost::program_options)

答案 1 :(得分:0)

在致电rule "Slapper" dialect "mvel" when itm : item( ) from pkg.items List( size() == itm.size() ) from collect ( field( fieldId == "111" , value == "1" ) from itm.fields field( fieldId == "222" , value == "2" ) from itm.fields ) from itm then ... end 邮件列表并最终在cmake gitlab issues上发帖后,我意识到问题不仅仅在于我的cmake版本,而是我继承的cmake-developer一个较旧的项目。

我正在设置:

cmake_minimum_required

将其更新为3.12后,我的行为正确:

cmake_minium_required(VERSION 2.8)

阅读cmake_minimum_required文档,我了解“命令隐式调用” /path/to/g++ -rdynamic CMakeFiles/test.dir/test.cpp.o -o test -lboost_program_options-mt 。我相信后者在我的3.12`cmake中与cmake_policy配合不好。

我不确定这篇文章对SO是否有用,但是我确实在这里学到了我的cmake课程。