I’m trying to use Google Benchmark with the g++ compiler on Windows. I installed the library using the vcpkg package manager with the triplet x64-mingw-static.
Here is the command I used to install it:
vcpkg install benchmark:x64-mingw-static
To compile my benchmark code, I am using the following g++ command:
g++ -std=c++23 test_benchmark.cpp -isystem "C:\Program Files\vcpkg\installed\x64-mingw-static\include" "C:\Program Files\vcpkg\installed\x64-mingw-static\lib\benchmark.a" "C:\Program Files\vcpkg\installed\x64-mingw-static\lib\benchmark_main.a" -lpthread -o benchmark_test.exe
However, I am getting the following errors:
C:/Program Files/MSYS2/ucrt64/bin/../lib/gcc/x86_64-w64-mingw32/14.1.0/../../../../x86_64-w64-mingw32/bin/ld.exe: cannot find C:Program Filesvcpkginstalledx64-mingw-staticlibbenchmark.a: No such file or directory
C:/Program Files/MSYS2/ucrt64/bin/../lib/gcc/x86_64-w64-mingw32/14.1.0/../../../../x86_64-w64-mingw32/bin/ld.exe: cannot find C:Program Filesvcpkginstalledx64-mingw-staticlibbenchmark_main.a: No such file or directory
collect2.exe: error: ld returned 1 exit status
I have verified that the files benchmark.a and benchmark_main.a exist in the specified directory. I also tried using the -L flag to specify the library directory and the -l flag to link against the libraries, but encountered similar issues. Here is the command:
g++ -std=c++23 test_benchmark.cpp -isystem "C:\Program Files\vcpkg\installed\x64-mingw-static\include" -L "C:\Program Files\vcpkg\installed\x64-mingw-static\lib" -lbenchmark -lbenchmark_main -lpthread -o benchmark_test.exe
This resulted in the following errors:
C:/Program Files/MSYS2/ucrt64/bin/../lib/gcc/x86_64-w64-mingw32/14.1.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:UsersbenjiAppDataLocalTempccrIFsd8.o:test_benchmark:(.text+0x66): undefined reference to `__imp__ZN9benchmark5State16StartKeepRunningEv'
C:/Program Files/MSYS2/ucrt64/bin/../lib/gcc/x86_64-w64-mingw32/14.1.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:UsersbenjiAppDataLocalTempccrIFsd8.o:test_benchmark:(.text+0xf4): undefined reference to `__imp__ZN9benchmark5State17FinishKeepRunningEv'
C:/Program Files/MSYS2/ucrt64/bin/../lib/gcc/x86_64-w64-mingw32/14.1.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:UsersbenjiAppDataLocalTempccrIFsd8.o:test_benchmark:(.text+0x1a3): undefined reference to `__imp__ZN9benchmark5State16StartKeepRunningEv'
C:/Program Files/MSYS2/ucrt64/bin/../lib/gcc/x86_64-w64-mingw32/14.1.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:UsersbenjiAppDataLocalTempccrIFsd8.o:test_benchmark:(.text+0x23b): undefined reference to `__imp__ZN9benchmark5State17FinishKeepRunningEv'
C:/Program Files/MSYS2/ucrt64/bin/../lib/gcc/x86_64-w64-mingw32/14.1.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:UsersbenjiAppDataLocalTempccrIFsd8.o:test_benchmark:(.text+0x2ca): undefined reference to `__imp__ZN9benchmark16PrintDefaultHelpEv'
C:/Program Files/MSYS2/ucrt64/bin/../lib/gcc/x86_64-w64-mingw32/14.1.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:UsersbenjiAppDataLocalTempccrIFsd8.o:test_benchmark:(.text+0x2df): undefined reference to `__imp__ZN9benchmark10InitializeEPiPPcPFvvE'
C:/Program Files/MSYS2/ucrt64/bin/../lib/gcc/x86_64-w64-mingw32/14.1.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:UsersbenjiAppDataLocalTempccrIFsd8.o:test_benchmark:(.text+0x2f1): undefined reference to `__imp__ZN9benchmark27ReportUnrecognizedArgumentsEiPPc'
C:/Program Files/MSYS2/ucrt64/bin/../lib/gcc/x86_64-w64-mingw32/14.1.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:UsersbenjiAppDataLocalTempccrIFsd8.o:test_benchmark:(.text+0x305): undefined reference to `__imp__ZN9benchmark22RunSpecifiedBenchmarksEv'
C:/Program Files/MSYS2/ucrt64/bin/../lib/gcc/x86_64-w64-mingw32/14.1.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:UsersbenjiAppDataLocalTempccrIFsd8.o:test_benchmark:(.text+0x30e): undefined reference to `__imp__ZN9benchmark8ShutdownEv'
C:/Program Files/MSYS2/ucrt64/bin/../lib/gcc/x86_64-w64-mingw32/14.1.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:UsersbenjiAppDataLocalTempccrIFsd8.o:test_benchmark:(.text+0x335): undefined reference to `__imp__ZN9benchmark8internal17InitializeStreamsEv'
C:/Program Files/MSYS2/ucrt64/bin/../lib/gcc/x86_64-w64-mingw32/14.1.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:UsersbenjiAppDataLocalTempccrIFsd8.o:test_benchmark:(.text+0x398): undefined reference to `__imp__ZN9benchmark8internal25RegisterBenchmarkInternalEPNS0_9BenchmarkE'
C:/Program Files/MSYS2/ucrt64/bin/../lib/gcc/x86_64-w64-mingw32/14.1.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:UsersbenjiAppDataLocalTempccrIFsd8.o:test_benchmark:(.text+0x409): undefined reference to `__imp__ZN9benchmark8internal25RegisterBenchmarkInternalEPNS0_9BenchmarkE'
C:/Program Files/MSYS2/ucrt64/bin/../lib/gcc/x86_64-w64-mingw32/14.1.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:UsersbenjiAppDataLocalTempccrIFsd8.o:test_benchmark:(.text$_ZN9benchmark8internal17FunctionBenchmarkC1ERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEPFvRNS_5StateEE[_ZN9benchmark8internal17FunctionBenchmarkC1ERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEPFvRNS_5StateEE]+0x22): undefined reference to `__imp__ZN9benchmark8internal9BenchmarkC2ERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE'
C:/Program Files/MSYS2/ucrt64/bin/../lib/gcc/x86_64-w64-mingw32/14.1.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:UsersbenjiAppDataLocalTempccrIFsd8.o:test_benchmark:(.text$_ZN9benchmark8internal17FunctionBenchmarkC1ERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEPFvRNS_5StateEE[_ZN9benchmark8internal17FunctionBenchmarkC1ERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEPFvRNS_5StateEE]+0x2b): undefined reference to `__imp__ZTVN9benchmark8internal17FunctionBenchmarkE'
collect2.exe: error: ld returned 1 exit status
My cpp is:
#include <benchmark/benchmark.h>
static void BM_StringCreation(benchmark::State& state) {
for (auto _ : state)
std::string empty_string;
}
BENCHMARK(BM_StringCreation);
static void BM_StringCopy(benchmark::State& state) {
std::string x = "hello";
for (auto _ : state)
std::string copy(x);
}
BENCHMARK(BM_StringCopy);
BENCHMARK_MAIN();
I’ve ensured that the vcpkg installation is integrated with my environment and that the include paths are correct. I’m currently using g++ from the MSYS2 distribution.
Can anyone help me figure out why the linker can’t find the libraries and how to properly link Google Benchmark with g++ on Windows?
Benjiro Fukuoka is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.