Link errors in cross compilation

未关闭
#1,586 2 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

维护者通常 1 天内回复

还没有人认领这个 Issue。

评估

难度
4/5
预计耗时
3-5 天
新手友好度
35/100
Issue 类型
缺陷
描述清晰度
基本清楚
活跃度
停滞
技术栈
cmake, cpp
领域
build-system

调研方向

使用提供的 toolchain.cmake、v1.7.1 版本和静态构建命令重现该失败。将生成的静态链接配置和链接器行为与正常工作的 -flto 命令进行比较;对于 x86_64-w64-mingw32 目标,不使用 LTO 即可成功链接示例测试时,即表示完成。

由索引模型根据 Issue 内容生成。

描述

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:

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:
set(CMAKE_SYSTEM_NAME Windows)
set(CMAKE_C_COMPILER x86_64-w64-mingw32-gcc)
set(CMAKE_CXX_COMPILER x86_64-w64-mingw32-g++)
  1. cd build && make install -j
  2. See error

Expected behavior
The benchmark for static links should also work.

主要语言
C++
星标
10.5k
派生
1.8k
平均合并
2 天 1 小时
30 天内合并 PR
28

环境准备

  • 没有 Dockerfile 或 Docker Compose 文件
  • 没有 Pull Request 模板
  • 阅读贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

google/benchmark 的其他 Issue

查看 google/benchmark 的全部 Issue

相似的 Issue

更多 C++ Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。