如何从源代码 link 在 Mac 上提升 Regex C++?

How to link Boost Regex C++ on Mac from Source?

我已经从 https://www.boost.org/doc/libs/1_72_0/libs/regex/doc/html/index.html 下载了整个 Boost 源代码,并且我有安装说明:

tar -vcf boost_1_76_0.tar
./bootstrap.sh --with-toolset=clang --with-libraries=program_options,regex,filesystem,system
./b2
sudo ./b2 install

然后我收到一条消息,说一切都已成功安装:

The following directory should be added to compiler include paths:

    /Users/3omni/Documents/GitHub/PERSONAL/GEN/boost_1_76_0

The following directory should be added to linker library paths:

    /Users/3omni/Documents/GitHub/PERSONAL/GEN/boost_1_76_0/stage/lib

现在,我有一个代码可以加载 Boost 正则表达式 header:

#include <boost/regex.hpp>

[More code...]

现在问题出在库链接上,我尝试编译:

gcc  -I/Users/3omni/Documents/GitHub/PERSONAL/GEN/boost_1_76_0 -L/Users/3omni/Documents/GitHub/PERSONAL/GEN/boost_1_76_0/stage/lib -O3 -o readvcf readvcf.cc -lboost_regex

(不确定我是否应该使用 -lboost_regex 但这是我在互联网上找到的东西)。

这非常失败......

Undefined symbols for architecture x86_64:
  "std::logic_error::what() const", referenced from:
      vtable for boost::wrapexcept<std::invalid_argument> in readvcf-3b61f4.o
      vtable for boost::wrapexcept<std::logic_error> in readvcf-3b61f4.o
  "std::runtime_error::what() const", referenced from:
      vtable for boost::wrapexcept<std::runtime_error> in readvcf-3b61f4.o
      vtable for boost::regex_error in readvcf-3b61f4.o
      vtable for boost::wrapexcept<boost::regex_error> in readvcf-3b61f4.o
  "std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >::compare(char const*) const", referenced from:
      char* boost::re_detail_500::re_is_set_member<char*, char, boost::regex_traits<char, boost::cpp_regex_traits<char> >, unsigned int>(char*, char*, boost::re_detail_500::re_set_long<unsigned int> const*, boost::re_detail_500::regex_data<char, boost::regex_traits<char, boost::cpp_regex_traits<char> > > const&, bool) in readvcf-3b61f4.o
      std::__1::__wrap_iter<char const*> boost::re_detail_500::re_is_set_member<std::__1::__wrap_iter<char const*>, char, boost::regex_traits<char, boost::cpp_regex_traits<char> >, unsigned int>(std::__1::__wrap_iter<char const*>, std::__1::__wrap_iter<char const*>, boost::re_detail_500::re_set_long<unsigned int> const*, boost::re_detail_500::regex_data<char, boost::regex_traits<char, boost::cpp_regex_traits<char> > > const&, bool) in readvcf-3b61f4.o
  "std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >::compare(unsigned long, unsigned long, char const*, unsigned long) const", referenced from:
      unsigned int boost::re_detail_500::find_sort_syntax<boost::re_detail_500::cpp_regex_traits_implementation<char>, char>(boost::re_detail_500::cpp_regex_traits_implementation<char> const*, char*) in readvcf-3b61f4.o
      boost::re_detail_500::lookup_default_collate_name(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&) in readvcf-3b61f4.o
  "std::__1::__vector_base_common<true>::__throw_length_error() const", referenced from:
      VCF2SNP(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, int, int) in readvcf-3b61f4.o
      boost::re_detail_500::basic_regex_parser<char, boost::regex_traits<char, boost::cpp_regex_traits<char> > >::parse_open_paren() in readvcf-3b61f4.o
      boost::re_detail_500::basic_regex_parser<char, boost::regex_traits<char, boost::cpp_regex_traits<char> > >::parse_alt() in readvcf-3b61f4.o
      boost::re_detail_500::basic_regex_parser<char, boost::regex_traits<char, boost::cpp_regex_traits<char> > >::parse_perl_extension() in readvcf-3b61f4.o
      void boost::re_detail_500::named_subexpressions::set_name<char>(char const*, char const*, int) in readvcf-3b61f4.o
      boost::re_detail_500::basic_regex_parser<char, boost::regex_traits<char, boost::cpp_regex_traits<char> > >::parse_set_literal(boost::re_detail_500::basic_char_set<char, boost::regex_traits<char, boost::cpp_regex_traits<char> > >&) in readvcf-3b61f4.o
      boost::re_detail_500::basic_regex_creator<char, boost::regex_traits<char, boost::cpp_regex_traits<char> > >::create_startmaps(boost::re_detail_500::re_syntax_base*) in readvcf-3b61f4.o
      ...
  "std::__1::__vector_base_common<true>::__throw_out_of_range() const", referenced from:
      boost::re_detail_500::basic_regex_parser<char, boost::regex_traits<char, boost::cpp_regex_traits<char> > >::parse_open_paren() in readvcf-3b61f4.o
      boost::re_detail_500::basic_regex_parser<char, boost::regex_traits<char, boost::cpp_regex_traits<char> > >::parse_perl_extension() in readvcf-3b61f4.o
  "std::__1::__basic_string_common<true>::__throw_length_error() const", referenced from:
      boost::re_detail_500::cpp_regex_traits_char_layer<char>::init() in readvcf-3b61f4.o
      std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > std::__1::operator+<char, std::__1::char_traits<char>, std::__1::allocator<char> >(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&) in readvcf-3b61f4.o
      boost::re_detail_500::cpp_regex_traits_implementation<char>::lookup_classname(char const*, char const*) const in readvcf-3b61f4.o
      boost::re_detail_500::cpp_regex_traits_implementation<char>::lookup_classname_imp(char const*, char const*) const in readvcf-3b61f4.o
      boost::re_detail_500::cpp_regex_traits_implementation<char>::error_string(boost::regex_constants::error_type) const in readvcf-3b61f4.o
      boost::re_detail_500::cpp_regex_traits_implementation<char>::lookup_collatename(char const*, char const*) const in readvcf-3b61f4.o
      boost::re_detail_500::lookup_default_collate_name(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&) in readvcf-3b61f4.o
      ...
  "std::__1::locale::has_facet(std::__1::locale::id&) const", referenced from:
      boost::re_detail_500::cpp_regex_traits_base<char>::imbue(std::__1::locale const&) in readvcf-3b61f4.o
      std::__1::basic_filebuf<char, std::__1::char_traits<char> >::basic_filebuf() in readvcf-3b61f4.o
  "std::__1::locale::use_facet(std::__1::locale::id&) const", referenced from:
      getAlleles(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, std::__1::vector<std::__1::vector<bool, std::__1::allocator<bool> >, std::__1::allocator<std::__1::vector<bool, std::__1::allocator<bool> > > >&, int) in readvcf-3b61f4.o
      VCF2SNP(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, int, int) in readvcf-3b61f4.o
      std::__1::basic_filebuf<char, std::__1::char_traits<char> >::imbue(std::__1::locale const&) in readvcf-3b61f4.o
      boost::re_detail_500::cpp_regex_traits_base<char>::imbue(std::__1::locale const&) in readvcf-3b61f4.o
      boost::cpp_regex_traits<char>::toi(char const*&, char const*, int) const in readvcf-3b61f4.o
      std::__1::basic_ostream<char, std::__1::char_traits<char> >& std::__1::__put_character_sequence<char, std::__1::char_traits<char> >(std::__1::basic_ostream<char, std::__1::char_traits<char> >&, char const*, unsigned long) in readvcf-3b61f4.o
      std::__1::basic_filebuf<char, std::__1::char_traits<char> >::basic_filebuf() in readvcf-3b61f4.o
      ...

还有更多。最多:

      _main in readvcf-3b61f4.o
      std::__1::basic_istringstream<char, std::__1::char_traits<char>, std::__1::allocator<char> >::~basic_istringstream() in readvcf-3b61f4.o
      virtual thunk to std::__1::basic_istringstream<char, std::__1::char_traits<char>, std::__1::allocator<char> >::~basic_istringstream() in readvcf-3b61f4.o
      ...
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

我 运行 没有想法,我找不到任何解决方案。请,任何帮助将不胜感激!提前谢谢你

您正在使用 GCC 构建 C++ 程序,因此我们收到这些链接错误。 g++工具用于构建c++程序,它会自动为您添加标准库链接。

可以通过

修复
  1. 使用 g++

    g++ -I/Users/3omni/Documents/GitHub/PERSONAL/GEN/boost_1_76_0 -L/Users/3omni/Documents/GitHub/PERSONAL/GEN/boost_1_76_0/stage/lib -O3 -o readvcf readvcf.cc -lboost_rege

  2. 使用带有附加选项的 GCC(强烈不推荐)

    gcc -x c++ -I/Users/3omni/Documents/GitHub/PERSONAL/GEN/boost_1_76_0 -L/Users/3omni/Documents/GitHub/PERSONAL/GEN/boost_1_76_0/stage/lib -O3 -o readvcf readvcf.cc -lboost_regex -lstdc++

相关问题:undefined reference to __cxa_end_cleanup'