LNK2001 linker error while linking google benchmark lib

960 Views Asked by At

I have a problem with linking benchmark.lib in my project. I've built it as described here. I've got sln file, compiled it (in release mode) and then created my own VS project. Linker can find my lib but it doesn't link it anyway (LNK2001). I added path to needed header files, path to lib in additional library directories and my library name in additional dependencies but it still doesn't work. I try to compile it in release mode. My files tree:

Files in my project

Project properties (linker): additional dependencies

Additional library directories

Logs:

1>mybenchmark.obj : error LNK2001: unresolved external symbol "__declspec(dllimport) public: virtual __cdecl benchmark::internal::FunctionBenchmark::~FunctionBenchmark(void)" (__imp_??1FunctionBenchmark@internal@benchmark@@UEAA@XZ)
1>mybenchmark.obj : error LNK2001: unresolved external symbol "__declspec(dllimport) public: __cdecl benchmark::internal::FunctionBenchmark::FunctionBenchmark(char const *,void (__cdecl*)(class benchmark::State &))" (__imp_??0FunctionBenchmark@internal@benchmark@@QEAA@PEBDP6AXAEAVState@2@@Z@Z)
1>mybenchmark.obj : error LNK2001: unresolved external symbol "__declspec(dllimport) public: struct benchmark::State::StateIterator __cdecl benchmark::State::end(void)" (__imp_?end@State@benchmark@@QEAA?AUStateIterator@12@XZ)
1>mybenchmark.obj : error LNK2001: unresolved external symbol "__declspec(dllimport) public: struct benchmark::State::StateIterator __cdecl benchmark::State::begin(void)" (__imp_?begin@State@benchmark@@QEAA?AUStateIterator@12@XZ)
1>C:\Users\Paweł\source\repos\benchmarkTest\x64\Release\benchmarkTest.exe : fatal error LNK1120: 4 unresolved externals

And my source file with some exemplary benchmark (also from previously linked github repo):

#include <benchmark/benchmark.h>

static void BM_StringCreation(benchmark::State& state) {
  for (auto _ : state)
    std::string empty_string;
}
// Register the function as a benchmark
BENCHMARK(BM_StringCreation);

// Define another benchmark
static void BM_StringCopy(benchmark::State& state) {
  std::string x = "hello";
  for (auto _ : state)
    std::string copy(x);
}
BENCHMARK(BM_StringCopy);

BENCHMARK_MAIN();

Thank you in advance for your answers.

2

There are 2 best solutions below

0
D_Rai On

This a bit late but I also ran into this issue when I tried to use google benchmark in visual studio.

What I had to do was to set BENCHMARK_STATIC_DEFINE in 'Properties' -> 'C/C++' -> 'Preprocessor' -> 'Preprocessor Definitions'.

Preprocessor properties screenshot

Hope this works for you

0
golightlydev On

D_Rai's answer got rid of almost all of the linker error messages that google benchmark was giving me in visual studio, but it left me with one more: LNK2001 unresolved external symbol __imp_SHGetValueA

in order to solve this, I had to link to Shlwapi.lib.

Here are my full links and includes, for reference:

#pragma comment(lib, "dependencies/benchmark/build/src/Release/benchmark.lib")
#pragma comment ( lib, "Shlwapi.lib" )
#include "dependencies/benchmark/include/benchmark/benchmark.h"