Link errors in cross compilation
- Dominant language
- C++
- Stars
- 10.4k
- Forks
- 1.8k
- Avg merge
- 2d 4h
- Merged PRs (30d)
- 8
Description
**Describe the bug**
I tried to cross compile benchmark to x86_64-w64-mingw32. When I set "-DBUILD_SHARED_LIBS=ON", no error generated. When I set "-DBUILD_SHARED_LIBS=OFF", the libbenchmark could be built. But when I tried to run the test, link errors generated.
The test code:
```c++
void BM_empty(benchmark::State& state) {
for (auto _ : state) {
benchmark::DoNotOptimize(state.iterations());
}
}
BENCHMARK(BM_empty);
```
The command:
```
x86_64-w64-mingw32-g++ test.cpp -std=c++20 -isystem ./include -L./lib -lbenchmark
```
The errors:
```
/home/luo/x86_64-w64-mingw32/lib/gcc/x86_64-w64-mingw32/13.0.0/../../../../x86_64-w64-mingw32/bin/ld: /tmp/cciXACO6.o:test.cpp:(.text+0x106): undefined reference to `__imp__ZN9benchmark5State16StartKeepRunningEv'
/home/luo/x86_64-w64-mingw32/lib/gcc/x86_64-w64-mingw32/13.0.0/../../../../x86_64-w64-mingw32/bin/ld: /tmp/cciXACO6.o:test.cpp:(.text.startup+0x44): undefined reference to `__imp__ZN9benchmark16PrintDefaultHelpEv'
/home/luo/x86_64-w64-mingw32/lib/gcc/x86_64-w64-mingw32/13.0.0/../../../../x86_64-w64-mingw32/bin/ld: /tmp/cciXACO6.o:test.cpp:(.text.startup+0x52): undefined reference to `__imp__ZN9benchmark10InitializeEPiPPcPFvvE'
/home/luo/x86_64-w64-mingw32/lib/gcc/x86_64-w64-mingw32/13.0.0/../../../../x86_64-w64-mingw32/bin/ld: /tmp/cciXACO6.o:test.cpp:(.text.startup+0x5f): undefined reference to `__imp__ZN9benchmark27ReportUnrecognizedArgumentsEiPPc'
/home/luo/x86_64-w64-mingw32/lib/gcc/x86_64-w64-mingw32/13.0.0/../../../../x86_64-w64-mingw32/bin/ld: /tmp/cciXACO6.o:test.cpp:(.text.startup+0x70): undefined reference to `__imp__ZN9benchmark22RunSpecifiedBenchmarksEv'
/home/luo/x86_64-w64-mingw32/lib/gcc/x86_64-w64-mingw32/13.0.0/../../../../x86_64-w64-mingw32/bin/ld: /tmp/cciXACO6.o:test.cpp:(.text.startup+0x76): undefined reference to `__imp__ZN9benchmark8ShutdownEv'
/home/luo/x86_64-w64-mingw32/lib/gcc/x86_64-w64-mingw32/13.0.0/../../../../x86_64-w64-mingw32/bin/ld: /tmp/cciXACO6.o:test.cpp:(.text.startup+0x9a): undefined reference to `__imp__ZN9benchmark8internal17InitializeStreamsEv'
/home/luo/x86_64-w64-mingw32/lib/gcc/x86_64-w64-mingw32/13.0.0/../../../../x86_64-w64-mingw32/bin/ld: /tmp/cciXACO6.o:test.cpp:(.text.startup+0xab): undefined reference to `__imp__ZN9benchmark8internal9BenchmarkC2EPKc'
/home/luo/x86_64-w64-mingw32/lib/gcc/x86_64-w64-mingw32/13.0.0/../../../../x86_64-w64-mingw32/bin/ld: /tmp/cciXACO6.o:test.cpp:(.text.startup+0xc1): undefined reference to `__imp__ZTVN9benchmark8internal17FunctionBenchmarkE'
/home/luo/x86_64-w64-mingw32/lib/gcc/x86_64-w64-mingw32/13.0.0/../../../../x86_64-w64-mingw32/bin/ld: /tmp/cciXACO6.o:test.cpp:(.text.startup+0xcb): undefined reference to `__imp__ZN9benchmark8internal25RegisterBenchmarkInternalEPNS0_9BenchmarkE'
```
It seemed that the linker couldn't find functions with "__imp__".
The amazing thing is that when I added "-DBENCHMARK_ENABLE_LTO=ON" and compiled the test with lto, it worked correctly again.
The successful command:
```
x86_64-w64-mingw32-g++ test.cpp -std=c++20 -isystem ./include -L./lib -lbenchmark -flto
```
**System**
- Host: Linux 5.19.0-38-generic
- Target: Windows x86_64-w64-mingw32
- Compiler and version: x86_64-w64-mingw32-g++ 13.0.0 20220812 (experimental) (GCC)
**To reproduce**
Steps to reproduce the behavior:
1. download the release v1.7.1
2. ccmake -B build --toolchain toolchain.cmake --install-prefix=/home/luo/benchmark/install -DBUILD_SHARED_LIBS=OFF
toolchain.cmake:
``` cmake
set(CMAKE_SYSTEM_NAME Windows)
set(CMAKE_C_COMPILER x86_64-w64-mingw32-gcc)
set(CMAKE_CXX_COMPILER x86_64-w64-mingw32-g++)
```
3. cd build && make install -j
4. See error
**Expected behavior**
The benchmark for static links should also work.
Contributor guide
Assessment
This issue has not been assessed yet.