未定义的参考在GCC下交叉编译具有LTO的静态库 [英] undefined reference cross compiling static libraries with LTO under GCC

查看:466
本文介绍了未定义的参考在GCC下交叉编译具有LTO的静态库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图使用GCC 4.9.2为Windows(x86_64-w64-mingw32)从Linux(x86_64-pc-linux-gnu)交叉编译应用程序。



在构建与静态库链接的目标并使用链接时优化时,我会从链接器中获取目标从库中使用的所有符号的未定义引用错误。



例如,从bar.cpp构建bar.a

  int bar(void){return 42;} 

以及与foo.cpp连结

  extern int bar(void); 
int main(int,char **){bar();}

使用命令行

  x86_64-w64-mingw32-g ++ -flto -o foo.o -c foo.cpp 
x86_64 -w64-mingw32-g ++ -flto -o bar.o -c bar.cpp
x86_64-w64-mingw32-gcc-ar rc bar.a bar.o
x86_64-w64-mingw32-gcc- ranlib bar.a
x86_64-w64-mingw32-g ++ -flto -fuse-linker-plugin foo.o bar.a -o foo

错误结果

  /tmp/ccc3Twsc.lto.o:foo。 o :(。text + 0x15):未定义引用'bar()'
collect2:错误:ld返回1退出状态

从上面:


  • 我使用的是gcc-wrappers for ar / ranlib

  • 没有外部依赖关系

  • 所有文件都使用相同的选项编译



我曾尝试过使用-fuse-linker-plugin,gcc-ar和ar,符号可见性选项,optimsations等的各种组合,但我无法获得它的链接相关性ctly不关闭LTO。



所有目标在本地编译器(x86_64 Linux)下正确构建。



有什么明显的我在这里失踪?

解决方案

我能够在Mingw32-gcc 4.9.2上重现这个链接问题在Win7 64位下。但是,通过添加 -ffat -lto-objects 作为解决方法,我确实获得了它的链接。

  g ++ -flto -o foo.o -c foo.cpp 
g ++ -flto -ffat -lto-objects -o bar.o -c bar.cpp
ar rc bar.a bar.o
g ++ -flto -o foo.exe foo.o bar.a


I am attempting to use GCC 4.9.2 to cross compile an application from Linux (x86_64-pc-linux-gnu) for Windows (x86_64-w64-mingw32).

When building targets that link against static libraries and also using link-time optimisation I get undefined reference errors from the linker for all symbols the target uses from the library.

eg, building bar.a from bar.cpp

int bar (void) {return 42;}

and linking with foo.cpp

extern int bar (void);
int main (int, char**) {bar ();}

using the command line

x86_64-w64-mingw32-g++ -flto -o foo.o -c foo.cpp
x86_64-w64-mingw32-g++ -flto -o bar.o -c bar.cpp
x86_64-w64-mingw32-gcc-ar rc bar.a bar.o
x86_64-w64-mingw32-gcc-ranlib bar.a
x86_64-w64-mingw32-g++ -flto -fuse-linker-plugin foo.o bar.a -o foo

Results in the error

/tmp/ccc3Twsc.lto.o:foo.o:(.text+0x15): undefined reference to `bar()'
collect2: error: ld returned 1 exit status

From above:

  • I am using the gcc-wrappers for ar/ranlib
  • there are no external dependencies
  • all files are compiled with the same options

I have tried using various combinations of -fuse-linker-plugin, gcc-ar vs ar, symbol visibility options, optimsations, etc but I can't get it to link correctly without turning off LTO.

All targets build correctly under the native compiler (x86_64 Linux).

Is there something obvious I'm missing here?

解决方案

I'm able to reproduce this linking problem on Mingw32-gcc 4.9.2 under Win7 64-bit. However, I did get it to link successfully by adding -ffat-lto-objects as a workaround:

g++ -flto -o foo.o -c foo.cpp
g++ -flto -ffat-lto-objects -o bar.o -c bar.cpp
ar rc bar.a bar.o
g++ -flto -o foo.exe foo.o bar.a

这篇关于未定义的参考在GCC下交叉编译具有LTO的静态库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆