Search code examples
c++boostlinker-errors

Boost.Regex v1.71 linker error with MSVC, but not with Clang


I have this source file that attempts to include all Boost.Regex implementation files in one translation unit (yes, I know this is not the "official" method):

#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);
}

If I then download Boost v1.71 and attempt to build this with x64 Native Tools Command Prompt for VS 2022:

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

I get a bunch of linker errors:

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)"

However, I do NOT get such an error if I use clang-cl instead of cl.

I can't seem to figure out what definition cl might be missing that clang-cl isn't.

What am I missing?


Solution

  • It seems these templates are explicitly declared extern, preventing the compiler from looking at the definition.

    Passing -DBOOST_REGEX_NO_EXTERNAL_TEMPLATES fixes this.