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:
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.



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