Undefined link cross-compiling static libraries using LTO under GCC

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

When creating targets associated with static libraries, and also by optimizing link time, I get undefined help errors from the linker for all the characters that the target from the library uses.

e.g. building bar.a from bar.cpp

int bar (void) {return 42;} 

and linking to 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 

Error results

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

Above:

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

I tried using various combinations of -fuse-linker-plugin, gcc-ar vs ar, options for character visibility, optimizations, etc., but I can not get it to link correctly without disabling LTO.

All goals are built correctly under their own compiler (x86_64 Linux).

Is there something obvious I'm missing here?

+6
source share
1 answer

I can reproduce this binding problem on Mingw32-gcc 4.9.2 under Win7 64-bit. However, I did get a link to the connection 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 
+1
source

Source: https://habr.com/ru/post/979307/


All Articles