简体   繁体   中英

Example from Boosts Getting Started Manual for Unix not working

I am trying to install boost on MacOS for use with an open source piece of software. First I wanted to follow along with the example.

I first use Homebrew to install boost ( brew install boost ).

Then I try to compile the example.cpp code

#include <boost/regex.hpp>
#include <iostream>
#include <string>

int main()
{
    std::string line;
    boost::regex pat( "^Subject: (Re: |Aw: )*(.*)" );

    while (std::cin)
    {
        std::getline(std::cin, line);
        boost::smatch matches;
        if (boost::regex_match(line, matches, pat))
            std::cout << matches[2] << std::endl;
    }
}

Using this command: c++ -I /usr/local/Cellar/boost/1.64.0_1/ example.cpp -o example /usr/local/Cellar/boost/1.64.0_1/lib/libboost_regex.a

And I get a very long error message:

Undefined symbols for architecture x86_64:
  "boost::re_detail::perl_matcher<__gnu_cxx::__normal_iterator<char const*, std::basic_string<char, std::char_traits<char>, std::allocator<char> > >, std::allocator<boost::sub_match<__gnu_cxx::__normal_iterator<char const*, std::basic_string<char, std::char_traits<char>, std::allocator<char> > > > >, boost::regex_traits<char, boost::cpp_regex_traits<char> > >::construct_init(boost::basic_regex<char, boost::regex_traits<char, boost::cpp_regex_traits<char> > > const&, boost::regex_constants::_match_flags)", referenced from:
      boost::re_detail::perl_matcher<__gnu_cxx::__normal_iterator<char const*, std::basic_string<char, std::char_traits<char>, std::allocator<char> > >, std::allocator<boost::sub_match<__gnu_cxx::__normal_iterator<char const*, std::basic_string<char, std::char_traits<char>, std::allocator<char> > > > >, boost::regex_traits<char, boost::cpp_regex_traits<char> > >::perl_matcher(__gnu_cxx::__normal_iterator<char const*, std::basic_string<char, std::char_traits<char>, std::allocator<char> > >, __gnu_cxx::__normal_iterator<char const*, std::basic_string<char, std::char_traits<char>, std::allocator<char> > >, boost::match_results<__gnu_cxx::__normal_iterator<char const*, std::basic_string<char, std::char_traits<char>, std::allocator<char> > >, std::allocator<boost::sub_match<__gnu_cxx::__normal_iterator<char const*, std::basic_string<char, std::char_traits<char>, std::allocator<char> > > > > >&, boost::basic_regex<char, boost::regex_traits<char, boost::cpp_regex_traits<char> > > const&, boost::regex_constants::_match_flags, __gnu_cxx::__normal_iterator<char const*, std::basic_string<char, std::char_traits<char>, std::allocator<char> > >) in ccCPUnqq.o

... OMMITTED ...

"std::__1::locale::operator=(std::__1::locale const&)", referenced from:
      boost::re_detail_106400::cpp_regex_traits_base<char>::imbue(std::__1::locale const&) in libboost_regex.a(instances.o)
  "std::__1::collate<char>::id", referenced from:
      boost::re_detail_106400::cpp_regex_traits_base<char>::imbue(std::__1::locale const&) in libboost_regex.a(instances.o)
  "std::__1::ios_base::init(void*)", referenced from:
      boost::cpp_regex_traits<char>::toi(char const*&, char const*, int) const in libboost_regex.a(instances.o)
  "std::__1::ios_base::clear(unsigned int)", referenced from:
      boost::cpp_regex_traits<char>::toi(char const*&, char const*, int) const in libboost_regex.a(instances.o)
  "std::__1::messages<char>::id", referenced from:
      boost::re_detail_106400::cpp_regex_traits_base<char>::imbue(std::__1::locale const&) in libboost_regex.a(instances.o)
  "std::__1::numpunct<char>::id", referenced from:
      boost::cpp_regex_traits<char>::toi(char const*&, char const*, int) const in libboost_regex.a(instances.o)
  "std::__1::basic_ios<char, std::__1::char_traits<char> >::~basic_ios()", referenced from:
      boost::cpp_regex_traits<char>::toi(char const*&, char const*, int) const in libboost_regex.a(instances.o)
  "typeinfo for std::__1::basic_streambuf<char, std::__1::char_traits<char> >", referenced from:
      typeinfo for boost::re_detail_106400::parser_buf<char, std::__1::char_traits<char> > in libboost_regex.a(instances.o)
  "vtable for std::__1::basic_istream<char, std::__1::char_traits<char> >", referenced from:
      boost::cpp_regex_traits<char>::toi(char const*&, char const*, int) const in libboost_regex.a(instances.o)
  NOTE: a missing vtable usually means the first non-inline virtual member function has no definition.
      NOTE: a missing vtable usually means the first non-inline virtual member function has no definition.
    ld: symbol(s) not found for architecture x86_64
    collect2: error: ld returned 1 exit status

I have spent 2 days on this and cannot for the life of me figure out what I am doing wrong. Does anyone see anything obvious / have any suggestions? The open source software I am trying to install can be found here if anyone wants to try compiling it with boost.

Here is the result of calling 'g++': /opt/local/bin/g++

And my $PATH :

/usr/local/gcc/bin:/opt/local/var/macports/sources/rsync.macports.org/release/tarballs/ports/net/dnscrypt-proxy/Portfile:/Applications:/Applications:/Applications/sratoolkit.2.8.0-mac64/bin:/Applications/STAR/bin/MacOSX_x86_64:/Users/johngiorgi/.local/bin:/Users/johngiorgi/anaconda3/bin:/opt/local/bin:/opt/local/sbin:/Applications/ncbi-blast-2.4.0+/bin:/Library/Frameworks/Python.framework/Versions/3.4/bin:/usr/local/var/rbenv/shims:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/Cellar/boost/1.64.0_1/include:/usr/local/Cellar/boost/1.64.0_1/include/boost/tr1 :/opt/X11/bin:/usr/local/go/bin:/Library/TeX/texbin

You shouldn't need all those additional paths into the Homebrew directories, or the explicit link to libboost_regex.a.

The following worked for me on macOS Sierra 10.12.4 with Xcode 8.3.2 installed:

g++ -Wall example.cpp -lboost_regex

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM