Googletest: Unresolved symbol when using parameterized test

Created on 28 Jul 2015  路  13Comments  路  Source: google/googletest

I'm using WXP, MSVC 2008 with gtest 1.5 built as DLL.

I have a parameterized testcase (TEST_P) cpp file which is also linked into 
DLL. When building the testcase, I get the following linker error:


Error   1   error LNK2001: unresolved external symbol "class 
testing::internal::Mutex testing::internal::g_linked_ptr_mutex" 
(?g_linked_ptr_mutex@internal@testing@@3VMutex@12@A)    test_Mechanic.obj

Original issue reported on code.google.com by [email protected] on 11 Jun 2010 at 7:56

OpSys-Windows Priority-Medium Type-Defect auto-migrated

Most helpful comment

I've been seeing this crop up when building static libraries on Windows 10 with MSVC 2017. Any specific details you want on this? It may also be a problem when I'm building dynamic libraries but I'm running into other problems then that may be occluding seeing this as a problem for dynamic builds.

All 13 comments

We do have tests in the test/ directory to verify this scenario and they pass.  
Are you still seeing the error?

Original comment by [email protected] on 17 Jun 2010 at 7:48

  • Added labels: OpSys-Windows
No repro.  Closing for now for lack of user feedback.

Original comment by [email protected] on 22 Jul 2010 at 4:52

  • Changed state: Invalid

I've been seeing this crop up when building static libraries on Windows 10 with MSVC 2017. Any specific details you want on this? It may also be a problem when I'm building dynamic libraries but I'm running into other problems then that may be occluding seeing this as a problem for dynamic builds.

@louis-langholtz I think @GoogleCodeExporter is a bot or something. I don't know if anyone else is watching this issue...

@sgraham @gennadiycivil Can this issue be reopened?

The original issue is for MSVC 2008. This is a very old version of Visual Studio.
Does this still happen with more modern compiler?

Yes. I am seeing it as of VS 15.3.3 2017. The full error in my case is:

LNK2019 unresolved external symbol "class testing::internal::Mutex testing::internal::g_linked_ptr_mutex" (?g_linked_ptr_mutex@internal@testing@@3VMutex@12@A) referenced in function "public: virtual void __cdecl testing::internal::ParameterizedTestCaseInfo::RegisterTests(void)" (?RegisterTests@?$ParameterizedTestCaseInfo@VVerticalStackTest@@@internal@testing@@UEAAXXZ) UnitTests C:\pathto\PlayRho\Build\cmake-build\UnitTests\World.obj 1

I've just stumbled upon this issue while implementing value-parameterized tests (beforehand we had no issues with linking against dynamic gtest library). And I've managed to fix it by building my tests with a preprocessor definition GTEST_LINKED_AS_SHARED_LIBRARY.

Is GTEST_LINKED_AS_SHARED_LIBRARY something that you use in your code, or an official cmake switch for GTest?

It's a preprocessor definition that should be set while building the code that uses GTest as shared library. I've personally set it in my project's properties.

using target_compile_definitions( GMockConsumer PRIVATE -DGTEST_LINKED_AS_SHARED_LIBRARY ) fixes the compiler error, but causes a lot of
warning C4251: 'identifier' : class 'type' needs to have dll-interface to be used by clients of class 'type2'
warnings for me.

If those C4251 warnings mention standard library components (such as std::basic_string), you can ignore/disable them as long as you build GTest and your own code using the same compiler and the same 'Runtime Library' settings.

I'm trying to build gtest 1.8 with CMake and VS2017 with Value Parameterized Tests and I get this error:

Error LNK2001 unresolved external symbol "class testing::internal::Mutex testing::internal::g_linked_ptr_mutex" (?g_linked_ptr_mutex@internal@testing@@3vmutex@12@A)

I don't want to apply the solution of setting GTEST_LINKED_AS_SHARED_LIBRARY=1 because I don't want to compile my code into a shared lib (I'm building gtest 1.8 as a shared lib). Is there a solution in this case?

My code compiles if I remove this code:

TEST_P(DerivedTests, SomeTest)
{
    ...
}

INSTANTIATE_TEST_CASE_P(JustSomeTestCaseNamePrefix,
    DerivedTests,
    ::testing::Values( custom_value_a, custom_value_b) );

Complete Code:

Class TestsBase : public Base, public ::testing::WithParamInterface< CustomType_t >
{
public:
    virtual void SetUp()
    {
        val_ = GetParam();
    }
protected:
    CustomType_t val_;

};

and Class Base is:

Class Base : public ::testing::Test 
{
    ...
};

I am building with set(BUILD_SHARED_LIBS on) and also

set(BUILD_SHARED_LIBS ON CACHE BOOL "Build shared libraries (DLLs)." FORCE)
set(gtest_force_shared_crt ON CACHE BOOL "" FORCE)

Any help would be appreciated. Thanks

Was this page helpful?
0 / 5 - 0 ratings