我正在尝试在 Windows 上使用 Google Benchmark 和 g++ 编译器。我使用 vcpkg 包管理器和三元组 x64-mingw-static 安装了该库。
这是我用来安装它的命令:
vcpkg install benchmark:x64-mingw-static
我已验证文件 benchmark.a 和 benchmark_main.a 存在于指定目录中。这是命令:
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
这导致了以下错误:
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
我的cpp是:
#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();
我已确保 vcpkg 安装与我的环境集成,并且包含路径正确。我目前正在使用 MSYS2 发行版的 g++。
任何人都可以帮我弄清楚为什么链接器找不到库以及如何在 Windows 上正确链接 Google Benchmark 和 g++ 吗?
如果您想链接静态基准测试库,您必须在基准测试包含之前定义
BENCHMARK_STATIC_DEFINE
。
#define BENCHMARK_STATIC_DEFINE
#include <benchmark/benchmark.h>
// Your benchmark code goes here
或者定义到编译器中
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
非常不幸的是,我能够编译它的唯一方法是使用 cmake ...
cmake_minimum_required(VERSION 3.16)
project(MyBenchmarkProject LANGUAGES CXX)
set(CMAKE_CXX_STANDARD 23)
set(CMAKE_BUILD_TYPE Release)
if(MSVC)
set(CMAKE_CXX_FLAGS_RELEASE "/O2 /DNDEBUG")
else()
set(CMAKE_CXX_FLAGS_RELEASE "-O3 -DNDEBUG -march=native -flto -funroll-loops -fstrict-aliasing -fno-plt")
endif()
find_package(benchmark REQUIRED)
add_executable(MyBenchmark ex.cpp)
target_link_libraries(MyBenchmark PRIVATE benchmark::benchmark benchmark::benchmark_main)
if(WIN32)
target_link_libraries(MyBenchmark PRIVATE pthread)
endif()