VS2017 - Un explained linker error LNK2019 with usage of std::regex

Hadimani, Basavaraj 1 Reputation point
2021-03-09T16:43:22.837+00:00

Hello, I have a project where in I use std::regex to perform a check whether the given string matches the expression that I have set upfront. No issues what so ever. It builds, runs perfectly fine on VS2017 , professional version without any hazzles. I use the same code on on another project (my unit tests' project) and it throws up the following linker errors.

LNK2019: unresolved external symbol "public: static class std::locale::_Locimp * __cdecl std::locale::_Init(bool)" (?_Init@locale@std@@SAPAV_Locimp@12@_N@Z) referenced in function "public: __thiscall std::locale::locale(void)" (??0locale@std@@QAE@XZ)

LNK2019: unresolved external symbol "public: static class std::locale::_Locimp * __cdecl std::locale::_Getgloballocale(void)" (?_Getgloballocale@locale@std@@SAPAV_Locimp@12@XZ) referenced in function "public: class std::locale::facet const * __thiscall std::locale::_Getfacet(unsigned int)const " (?_Getfacet@locale@std@@QBEPBVfacet@12@I@Z) I have spent more than a day to understand the difference between the two projects (specifically the Properties-.Linker->Input->Additional Dependencies.. I dont find anything that explains the issue mentioned. I am unable to resolve the linker error as the mentioned internal methods are not used by me at all. Please help in resolving the same. Thanks in advance.

Developer technologies | C++
Developer technologies | C++

A high-level, general-purpose programming language, created as an extension of the C programming language, that has object-oriented, generic, and functional features in addition to facilities for low-level memory manipulation.

Developer technologies | Visual Studio | Other
Developer technologies | Visual Studio | Other

A family of Microsoft suites of integrated development tools for building applications for Windows, the web, mobile devices and many other platforms. Miscellaneous topics that do not fit into specific categories.

Community Center | Not monitored

Locked Question. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

2 answers

Sort by: Most helpful
  1. Gerardo Sanchez 0 Reputation points
    2026-10-29T16:53:09.96+00:00

    I get this error in my unit test code because i override "private" and "protected" keywords:

    #if defined(UNIT_TEST)
    #define protected public
    #define private public
    #endif
    

    Of course, all my code compile, but the problem raized in link time, because std is precompiled using private and protected keywords, but linker searchs for public implementations:

    public: static class std::locale::_Locimp * __cdecl std::locale::_Getgloballocale(void)" (?_Getgloballocale@locale@std@@SAPAV_Locimp@12@XZ)
    

    Correct name found in stdlib:

    private: static class std::locale::_Locimp * __cdecl std::locale::_Getgloballocale(void)" (?_Getgloballocale@locale@std@@SAPAV_Locimp@12@XZ)
    

    The only difference is private -> public

    SOLUTION: just remove #define's before include iostream and fstream

    iostream
    
    #if defined(UNIT_TEST)
    #	undef private
    #	undef protected
    #endif
    
    #include <iostream>
    #include <fstream>
    
    

    Was this answer helpful?

    0 comments No comments
  2. Dylan Zhu-MSFT 6,436 Reputation points
    2021-03-10T02:37:06.4+00:00

    Hi HadimaniBasavaraj,

    Please check if the VC++ directories of your test project is same with your project.
    76042-image.png

    Best Regards, Dylan

    If the answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our * *documentation* to enable e-mail notifications if you want to receive the related email notification for this thread.**

    Was this answer helpful?

    0 comments No comments