c++ Boost.Regex v1.71链接器错误与MSVC有关,但与Clang无关

6mw9ycah  于 2023-03-20  发布在  其他
关注(0)|答案(1)|浏览(129)

我有一个源文件,它试图在一个翻译单元中包含所有Boost.regex实现文件(是的,我知道这不是“官方”方法):

#include <boost/regex.hpp>
#include <boost/../libs/regex/src/cpp_regex_traits.cpp>
#include <boost/../libs/regex/src/cregex.cpp>
#include <boost/../libs/regex/src/c_regex_traits.cpp>
#include <boost/../libs/regex/src/fileiter.cpp>
#include <boost/../libs/regex/src/icu.cpp>
#include <boost/../libs/regex/src/instances.cpp>
#include <boost/../libs/regex/src/internals.hpp>
#include <boost/../libs/regex/src/posix_api.cpp>
#include <boost/../libs/regex/src/regex.cpp>
#include <boost/../libs/regex/src/regex_debug.cpp>
#include <boost/../libs/regex/src/regex_raw_buffer.cpp>
#include <boost/../libs/regex/src/regex_traits_defaults.cpp>
#include <boost/../libs/regex/src/static_mutex.cpp>
#include <boost/../libs/regex/src/usinstances.cpp>
#include <boost/../libs/regex/src/w32_regex_traits.cpp>
#include <boost/../libs/regex/src/wc_regex_traits.cpp>
#include <boost/../libs/regex/src/wide_posix_api.cpp>
#include <boost/../libs/regex/src/winstances.cpp>

int main()
{
    std::string s = "Boost Libraries";
    boost::regex expr("\\w+\\s\\w+");
    boost::regex_match(s, expr);
}

如果我随后下载Boost v1.71,并尝试使用 x64 Native Tools命令提示符for VS 2022 构建此文件:

cl -DBOOST_ALL_NO_LIB=1 -EHsc -I boost_1_71_0 temp.cpp

我得到了一堆链接器错误:

error LNK2019: unresolved external symbol "private: class boost::basic_regex<char,struct boost::regex_traits<char,class boost::w32_regex_traits<char> > > & __cdecl boost::basic_regex<char,struct boost::regex_traits<char,class boost::w32_regex_traits<char> > >::do_assign(char const *,char const *,unsigned int)" referenced in function "public: class boost::basic_regex<char,struct boost::regex_traits<char,class boost::w32_regex_traits<char> > > & __cdecl boost::basic_regex<char,struct boost::regex_traits<char,class boost::w32_regex_traits<char> > >::assign(char const *,char const *,unsigned int)"
error LNK2019: unresolved external symbol "public: void __cdecl boost::match_results<char const *,class std::allocator<struct boost::sub_match<char const *> > >::maybe_assign(class boost::match_results<char const *,class std::allocator<struct boost::sub_match<char const *> > > const &)" referenced in function "private: bool __cdecl boost::re_detail_107100::perl_matcher<char const *,class std::allocator<struct boost::sub_match<char const *> >,struct boost::c_regex_traits<char> >::match_match(void)"
error LNK2019: unresolved external symbol "public: bool __cdecl boost::re_detail_107100::perl_matcher<char const *,class std::allocator<struct boost::sub_match<char const *> >,struct boost::regex_traits<char,class boost::w32_regex_traits<char> > >::match(void)" referenced in function "bool __cdecl boost::regex_match<char const *,class std::allocator<struct boost::sub_match<char const *> >,char,struct boost::regex_traits<char,class boost::w32_regex_traits<char> > >(char const *,char const *,class boost::match_results<char const *,class std::allocator<struct boost::sub_match<char const *> > > &,class boost::basic_regex<char,struct boost::regex_traits<char,class boost::w32_regex_traits<char> > > const &,enum boost::regex_constants::_match_flags)"
error LNK2019: unresolved external symbol "public: bool __cdecl boost::re_detail_107100::perl_matcher<char const *,class std::allocator<struct boost::sub_match<char const *> >,struct boost::regex_traits<char,class boost::w32_regex_traits<char> > >::find(void)" referenced in function "unsigned int __cdecl boost::regex_grep<struct boost::re_detail_107100::pred1,char const *,char,struct boost::regex_traits<char,class boost::w32_regex_traits<char> > >(struct boost::re_detail_107100::pred1,char const *,char const *,class boost::basic_regex<char,struct boost::regex_traits<char,class boost::w32_regex_traits<char> > > const &,enum boost::regex_constants::_match_flags)"
error LNK2019: unresolved external symbol "private: void __cdecl boost::re_detail_107100::perl_matcher<char const *,class std::allocator<struct boost::sub_match<char const *> >,struct boost::regex_traits<char,class boost::w32_regex_traits<char> > >::construct_init(class boost::basic_regex<char,struct boost::regex_traits<char,class boost::w32_regex_traits<char> > > const &,enum boost::regex_constants::_match_flags)" referenced in function "public: __cdecl boost::re_detail_107100::perl_matcher<char const *,class std::allocator<struct boost::sub_match<char const *> >,struct boost::regex_traits<char,class boost::w32_regex_traits<char> > >::perl_matcher<char const *,class std::allocator<struct boost::sub_match<char const *> >,struct boost::regex_traits<char,class boost::w32_regex_traits<char> > >(char const *,char const *,class boost::match_results<char const *,class std::allocator<struct boost::sub_match<char const *> > > &,class boost::basic_regex<char,struct boost::regex_traits<char,class boost::w32_regex_traits<char> > > const &,enum boost::regex_constants::_match_flags,char const *)"
error LNK2019: unresolved external symbol "public: bool __cdecl boost::re_detail_107100::perl_matcher<class std::_String_const_iterator<class std::_String_val<struct std::_Simple_types<char> > >,class std::allocator<struct boost::sub_match<class std::_String_const_iterator<class std::_String_val<struct std::_Simple_types<char> > > > >,struct boost::regex_traits<char,class boost::w32_regex_traits<char> > >::find(void)" referenced in function "unsigned int __cdecl boost::regex_grep<class boost::re_detail_107100::split_pred<class std::back_insert_iterator<class std::vector<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >,class std::allocator<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > > > >,char,struct std::char_traits<char>,class std::allocator<char> >,class std::_String_const_iterator<class std::_String_val<struct std::_Simple_types<char> > >,char,struct boost::regex_traits<char,class boost::w32_regex_traits<char> > >(class boost::re_detail_107100::split_pred<class std::back_insert_iterator<class std::vector<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >,class std::allocator<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > > > >,char,struct std::char_traits<char>,class std::allocator<char> >,class std::_String_const_iterator<class std::_String_val<struct std::_Simple_types<char> > >,class std::_String_const_iterator<class std::_String_val<struct std::_Simple_types<char> > >,class boost::basic_regex<char,struct boost::regex_traits<char,class boost::w32_regex_traits<char> > > const &,enum boost::regex_constants::_match_flags)"
error LNK2019: unresolved external symbol "private: void __cdecl boost::re_detail_107100::perl_matcher<class std::_String_const_iterator<class std::_String_val<struct std::_Simple_types<char> > >,class std::allocator<struct boost::sub_match<class std::_String_const_iterator<class std::_String_val<struct std::_Simple_types<char> > > > >,struct boost::regex_traits<char,class boost::w32_regex_traits<char> > >::construct_init(class boost::basic_regex<char,struct boost::regex_traits<char,class boost::w32_regex_traits<char> > > const &,enum boost::regex_constants::_match_flags)" referenced in function "public: __cdecl boost::re_detail_107100::perl_matcher<class std::_String_const_iterator<class std::_String_val<struct std::_Simple_types<char> > >,class std::allocator<struct boost::sub_match<class std::_String_const_iterator<class std::_String_val<struct std::_Simple_types<char> > > > >,struct boost::regex_traits<char,class boost::w32_regex_traits<char> > >::perl_matcher<class std::_String_const_iterator<class std::_String_val<struct std::_Simple_types<char> > >,class std::allocator<struct boost::sub_match<class std::_String_const_iterator<class std::_String_val<struct std::_Simple_types<char> > > > >,struct boost::regex_traits<char,class boost::w32_regex_traits<char> > >(class std::_String_const_iterator<class std::_String_val<struct std::_Simple_types<char> > >,class std::_String_const_iterator<class std::_String_val<struct std::_Simple_types<char> > >,class boost::match_results<class std::_String_const_iterator<class std::_String_val<struct std::_Simple_types<char> > >,class std::allocator<struct boost::sub_match<class std::_String_const_iterator<class std::_String_val<struct std::_Simple_types<char> > > > > > &,class boost::basic_regex<char,struct boost::regex_traits<char,class boost::w32_regex_traits<char> > > const &,enum boost::regex_constants::_match_flags,class std::_String_const_iterator<class std::_String_val<struct std::_Simple_types<char> > >)"
error LNK2019: unresolved external symbol "public: void __cdecl boost::match_results<wchar_t const *,class std::allocator<struct boost::sub_match<wchar_t const *> > >::maybe_assign(class boost::match_results<wchar_t const *,class std::allocator<struct boost::sub_match<wchar_t const *> > > const &)" referenced in function "private: bool __cdecl boost::re_detail_107100::perl_matcher<wchar_t const *,class std::allocator<struct boost::sub_match<wchar_t const *> >,struct boost::c_regex_traits<wchar_t> >::match_match(void)"

然而,如果我使用clang-cl而不是cl,我不会得到这样的错误。
我似乎无法弄清楚cl可能缺少clang-cl没有的定义。
我错过了什么?

2ic8powd

2ic8powd1#

看起来这些模板被显式声明为extern,阻止编译器查看定义。
传递-DBOOST_REGEX_NO_EXTERNAL_TEMPLATES可修复此问题。

相关问题