Boost 库未在 Netbeans 中使用 G++ 编译

Posted

技术标签:

【中文标题】Boost 库未在 Netbeans 中使用 G++ 编译【英文标题】:Boost library not compiling in Netbeans with G++ 【发布时间】:2014-11-17 08:00:13 【问题描述】:

尝试在某些代码上运行一些正则表达式,但代码无法使用 boost 进行编译。当我安装 netbeans 和它的其余部分时,这个库是用 Cygwin 安装的,没有采取其他步骤。网上的说明不清楚还需要做什么,如果有的话,关于链接器,或者是否一切都应该开箱即用。

#include <iostream>
#include <string>
#include <boost/regex.hpp>
using namespace std;

bool regexValidate (string expr, string teststring) 

    boost::regex ex(expr);
    if ( boost::regex_match (teststring,ex) ) 
        cout << "true";
        return true;
    // else if (regex_match (teststring,regex("^\s*\d*d\d*\s*$"))) 
    //    cout << "true";
    //    return true;
    
    return false;


int main()

    string  diceexpr = "^\\s*\\d1,d\\d1,\\s*$";
    string  teststr = "10d10";
    string  teststr1 = "1d10";
    string  teststr2 = " 10d10 ";

    cout << teststr << " is ";
    if (regexValidate(diceexpr,teststr)) 
        cout << " valid!" << endl;
     else 
        cout << " invalid!" << endl;
    
    cout << teststr1 << " is ";
    if (regexValidate(diceexpr,teststr1)) 
        cout << " valid!" << endl;
     else 
        cout << " invalid!" << endl;
    
    cout << teststr2 << " is ";
    if (regexValidate(diceexpr,teststr2)) 
        cout << " valid!" << endl;
     else 
        cout << " invalid!" << endl;
    
    return 0;

给定以下输出。

"/usr/bin/make" -f nbproject/Makefile-CIS17A.mk QMAKE= SUBPROJECTS= .build-conf
make[1]: Entering directory '/cygdrive/c/Users/[ ... redacted ... ]/Inclass Experiments'
"/usr/bin/make"  -f nbproject/Makefile-CIS17A.mk dist/CIS17A/Cygwin_4.x-Windows/inclass_experiments.exe
make[2]: Entering directory '/cygdrive/c/Users/[ ... redacted ... ]/Inclass Experiments'
mkdir -p dist/CIS17A/Cygwin_4.x-Windows
g++ -std=c++11 -Wall -errors -Wextra    -o dist/CIS17A/Cygwin_4.x-Windows/inclass_experiments build/CIS17A/Cygwin_4.x-Windows/main.o 
/usr/lib/gcc/i686-pc-cygwin/4.8.3/../../../../i686-pc-cygwin/bin/ld: warning: cannot find entry symbol rrors; defaulting to 00401000
build/CIS17A/Cygwin_4.x-Windows/main.o: In function `regex_match<__gnu_cxx::__normal_iterator<char const*, std::basic_string<char> >, std::allocator<boost::sub_match<__gnu_cxx::__normal_iterator<char const*, std::basic_string<char> > > >, char, boost::regex_traits<char> >':
/usr/include/boost/regex/v4/regex_match.hpp:50: undefined reference to `boost::re_detail::perl_matcher<__gnu_cxx::__normal_iterator<char const*, std::string>, std::allocator<boost::sub_match<__gnu_cxx::__normal_iterator<char const*, std::string> > >, boost::regex_traits<char, boost::c_regex_traits<char> > >::match()'
build/CIS17A/Cygwin_4.x-Windows/main.o: In function `assign':
/usr/include/boost/regex/v4/basic_regex.hpp:382: undefined reference to `boost::basic_regex<char, boost::regex_traits<char, boost::c_regex_traits<char> > >::do_assign(char const*, char const*, unsigned int)'
build/CIS17A/Cygwin_4.x-Windows/main.o: In function `ZN5boost9re_detail12perl_matcherIN9__gnu_cxx17__normal_iteratorIPKcSsEESaINS_9sub_matchIS6_EEENS_12regex_traitsIcNS_14c_regex_traitsIcEEEEEC1ES6_S6_RNS_13match_resultsIS6_S9_EERKNS_11basic_regexIcSD_EENS_15regex_constants12_match_flagsES6_':
/usr/include/boost/regex/v4/perl_matcher.hpp:374: undefined reference to `boost::re_detail::perl_matcher<__gnu_cxx::__normal_iterator<char const*, std::string>, std::allocator<boost::sub_match<__gnu_cxx::__normal_iterator<char const*, std::string> > >, boost::regex_traits<char, boost::c_regex_traits<char> > >::construct_init(boost::basic_regex<char, boost::regex_traits<char, boost::c_regex_traits<char> > > const&, boost::regex_constants::_match_flags)'
collect2: error: ld returned 1 exit status
nbproject/Makefile-CIS17A.mk:62: recipe for target 'dist/CIS17A/Cygwin_4.x-Windows/inclass_experiments.exe' failed
make[2]: *** [dist/CIS17A/Cygwin_4.x-Windows/inclass_experiments.exe] Error 1
make[2]: Leaving directory '/cygdrive/c/Users/[ ... redacted ... ]/Inclass Experiments'
nbproject/Makefile-CIS17A.mk:59: recipe for target '.build-conf' failed
make[1]: *** [.build-conf] Error 2
make[1]: Leaving directory '/cygdrive/c/Users/[ ... redacted ... ]/Inclass Experiments'
nbproject/Makefile-impl.mk:39: recipe for target '.build-impl' failed
make: *** [.build-impl] Error 2

BUILD FAILED (exit value 2, total time: 2s)

【问题讨论】:

【参考方案1】:

这是一个链接错误。请验证以下两点:

Boost 库集中的一些库需要编译 - 其中包括“正则表达式”库。特别是,链接器需要将您的正则表达式相关代码与两个 Boost 库链接 - boost_systemboost_regex。确保它们已经建成。

告诉链接器这些库的方法完全是 NetBeans 责任 - 所以你需要在你的项目中配置 NetBeans 以这种方式传递有关 链接器的库名称和位置。我曾经在自己的 Makefile 中这样做,但也有其他方法可以做到这一点。您可以在 NetBeans 自己的帮助子系统中找到如何执行此操作的说明 - 只需查找 C/C++ Project Properties Dialog Box: Linker 页面即可。因此,请确保您已将 NetBeans 项目配置为访问 Boost 库。

您可以找到更多信息here。

【讨论】:

以上是关于Boost 库未在 Netbeans 中使用 G++ 编译的主要内容,如果未能解决你的问题,请参考以下文章

React 测试库未在快照中显示输入 onChange 道具

C ++静态库未在mac上的xcode中编译[重复]

使用 boost 正则表达式库未解析的外部对象

Android Tensorflow Lite C++ .SO 库未在运行时链接

[xamarin][uwp][custom renderer] 自定义渲染器 pcl 库未在发布模式下加载

为 C++/Boost 库设置环境(netbeans 或 eclipse)