google / googletest

GoogleTest - Google Testing and Mocking Framework
https://google.github.io/googletest/
BSD 3-Clause "New" or "Revised" License
34.79k stars 10.15k forks source link

"unresolved external symbol" using FLAGS_gtest_death_test_style on MS Windows #3554

Open upnplib opened 3 years ago

upnplib commented 3 years ago

Describe the bug I add GTEST_FLAG_SET(death_test_style, "threadsafe"); to main() of a gtest as shown at Death Test Styles:

int main(int argc, char** argv) {
testing::InitGoogleTest(&argc, argv);
GTEST_FLAG_SET(death_test_style, "threadsafe");
return RUN_ALL_TESTS();
}

This compiles on Linux (Debian Bullseye) without any errors. On Microsoft Windows I get:

test_UpnpString.obj : error LNK2019: unresolved external symbol "class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > testing::FLAGS_gtest_death_test_style" (?FLAGS_gtest_death_test_style@testing@@3V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@A) referenced in function main [C:\Users\ingo\devel\upnplib-dev\upnplib\gtests\build\test_UpnpStri
ng_old.vcxproj]
C:\Users\ingo\devel\upnplib-dev\upnplib\gtests\build\Release\test_UpnpString_old.exe : fatal error LNK1120: 1 unresolved externals
[C:\Users\ingo\devel\upnplib-dev\upnplib\gtests\build\test_UpnpString_old.vcxproj]

I expect it to compiles without errors, like it does on Linux.

Steps to reproduce the bug On MS Windows 10 add the line GTEST_FLAG_SET(death_test_style, "threadsafe"); to main() as shown above to an existing gtest that compiles before without errors.

Does the bug persist in the most recent commit? Yes.

What operating system and version are you using? i use Microsoft Windows 10 with Microsoft Visual Studio 2019 Community version and selected the default development command line environment

ingo@WIN10-DEVEL C:\Users\ingo> "C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Auxiliary\Build\vcvarsall.bat" amd64
**********************************************************************
** Visual Studio 2019 Developer Command Prompt v16.9.5
** Copyright (c) 2021 Microsoft Corporation
**********************************************************************
[vcvarsall.bat] Environment initialized for: 'x64'

as described at https://docs.microsoft.com/en-us/cpp/build/building-on-the-command-line?view=msvc-160#use-the-developer-tools-in-an-existing-command-window

What compiler and version are you using?

PS> cl.exe
Microsoft (R) C/C++ Optimizing Compiler Version 19.28.29915 for x64
Copyright (C) Microsoft Corporation.  All rights reserved.

usage: cl [ option... ] filename... [ /link linkoption... ]

What build system are you using?

PS> cmake --version
cmake version 3.19.20122902-MSVC_2

CMake suite maintained and supported by Kitware (kitware.com/cmake).
derekmauro commented 3 years ago

Please share your CMake files and details about how you are using GoogleTest (is it installed, etc...).

bcaddy commented 3 years ago

I think I might be running into this same issue, on Linux though. I'm getting this error

src/main_tests.cpp:32:18: error: use of undeclared identifier 'death_test_style'
GTEST_FLAG_SET(death_test_style, "threadsafe");
                 ^
1 error generated.
make: *** [Makefile:165: src/main_tests.o] Error 1

Here's the compile command that was used

CC -lgtest -I/sw/spock/spack-envs/base/opt/linux-sles15-x86_64/gcc-7.5.0/googletest-1.11.0-xgcobwe63kttlqkm3z6ujt5faiooviqf/include -L/sw/spock/spack-envs/base/opt/linux-sles15-x86_64/gcc-7.5.0/googletest-1.11.0-xgcobwe63kttlqkm3z6ujt5faiooviqf/lib64 -lpthread -lhdf5_cpp -Ofast -std=c++11 -fopenmp -DHYDRO_GPU -DCUDA -DMPI_CHOLLA  -DBLOCK -DPRECISION=2 -DPPMP -DHLLC -DVL -DDENSITY_FLOOR -DTEMPERATURE_FLOOR -DDE -DCPU_TIME -DOUTPUT -DHDF5 -DMPI_GPU   -DPARALLEL_OMP -DN_OMP_THREADS=8 -DO_HIP -Isrc -I/opt/cray/pe/hdf5/1.12.0.6/crayclang/10.0/include -fopenmp  -D__HIP_PLATFORM_HCC__= -D__HIP_PLATFORM_AMD__= -I"/opt/rocm-4.2.0/hip/include" -I"/opt/rocm-4.2.0/llvm/bin/../lib/clang/12.0.0" -I/opt/rocm-4.2.0/include -c src/main_tests.cpp -o src/main_tests.o

Googletest is installed as a module on a HPC cluster, I don't know any details about how it was installed unfortunately.

Here's the entirety of my main tests function. What other information do you require?

// External Libraries and Headers
#include <gtest/gtest.h>

// Local includes
#include "utils/testing_utilities.h"

/// This is the global variable to store the path to the root of Cholla
testingUtilities::GlobalString globalChollaRoot;
testingUtilities::GlobalString globalChollaBuild;
testingUtilities::GlobalString globalChollaMachine;

/*!
 * \brief The main function for testing
 *
 * \param argc The number of CLI arguments
 * \param argv A list of the CLI arguments
 * \return int
 */
int main(int argc, char **argv) {
  // First we initialize Googletest. Note, this removes all gtest related
  // arguments from argv and argc
  ::testing::InitGoogleTest(&argc, argv);
  GTEST_FLAG_SET(death_test_style, "threadsafe");
  // Initialize the global cholla root path variable
  globalChollaRoot.initPath("/ccs/home/rcaddy/Code/cholla");
  globalChollaBuild.initPath("hydro");
  globalChollaMachine.initPath("spock");

  // Run test and return result
  return RUN_ALL_TESTS();
}
dimhotepus commented 3 weeks ago
//   GTEST_LINKED_AS_SHARED_LIBRARY
//                            - Define to 1 when compiling tests that use
//                              Google Test as a shared library (known as
//                              DLL on Windows).

Need to define on dependent target to import symbols defined in gtest and link successfully.

See https://github.com/google/googletest/blob/df1544bcee0c7ce35cd5ea0b3eb8cc81855a4140/googletest/include/gtest/internal/gtest-port.h#L95