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

Boost library not compiling in Netbeans with G++

本文关键字:G++ 编译 Netbeans Boost      更新时间:2023-10-16

尝试对某些代码运行一些正则表达式,但代码无法使用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*dd*s*$"))) {
    //    cout << "true";
    //    return true;
    }
    return false;
}
int main()
{
    string  diceexpr = "^\s*\d{1,}d\d{1,}\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)

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

  • 来自 Boost 库集的某些库需要编译 -其中包括"正则表达式"库。特别是,链接器需要将您的正则表达式相关代码与两个 Boost 库链接 - boost_systemboost_regex 。确保它们已构建。

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

您可以在此处找到更多信息。