c++g++mingwgoogle-benchmark

Issues Linking Google Benchmark with g++ on Windows (MSYS2) using vcpkg


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

I have verified that the files benchmark.a and benchmark_main.a exist in the specified directory. 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:\Users\benji\AppData\Local\Temp\ccrIFsd8.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:\Users\benji\AppData\Local\Temp\ccrIFsd8.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:\Users\benji\AppData\Local\Temp\ccrIFsd8.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:\Users\benji\AppData\Local\Temp\ccrIFsd8.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:\Users\benji\AppData\Local\Temp\ccrIFsd8.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:\Users\benji\AppData\Local\Temp\ccrIFsd8.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:\Users\benji\AppData\Local\Temp\ccrIFsd8.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:\Users\benji\AppData\Local\Temp\ccrIFsd8.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:\Users\benji\AppData\Local\Temp\ccrIFsd8.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:\Users\benji\AppData\Local\Temp\ccrIFsd8.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:\Users\benji\AppData\Local\Temp\ccrIFsd8.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:\Users\benji\AppData\Local\Temp\ccrIFsd8.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:\Users\benji\AppData\Local\Temp\ccrIFsd8.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:\Users\benji\AppData\Local\Temp\ccrIFsd8.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?


Solution

  • If you want to link with the static benchmark libraries you must define BENCHMARK_STATIC_DEFINE before the benchmark's include.

    #define BENCHMARK_STATIC_DEFINE
    #include <benchmark/benchmark.h>
    
    // Your benchmark code goes here
    

    Or define in to the compiler

    g++ -std=c++23 test_benchmark.cpp -DBENCHMARK_STATIC_DEFINE -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