24

When I try to build static libraries with -flto, I get undefined reference errors:

library.cpp:

#include <iostream>

void foo() {
  std::cout << "Test!" << std::endl;
}

main.cpp:

void foo();

int main() {
  foo();
  return 0;
}

Compilation output:

$ g++ -flto -c library.cpp
$ ar rcs library.a library.o
$ g++ -flto main.cpp library.a
/tmp/ccZIgxCY.ltrans0.ltrans.o: In function `main':
ccZIgxCY.ltrans0.o:(.text+0x5): undefined reference to `foo()'
collect2: error: ld returned 1 exit status

It works fine if I link with library.o instead of library.a. What am I missing? This is with GCC 4.9.1 and binutils 2.24.

2
  • Did you try g++ -flto -lrary main.cpp? Sep 16, 2014 at 21:08
  • @πάνταῥεῖ Also broken. But I found the answer as I was writing the question, it's posted below. Sep 16, 2014 at 21:10

2 Answers 2

30

The answer, as I found out from this post by GCC developer Honza Hubička, is to use the gcc-ar wrapper instead of ar by itself:

$ gcc-ar rcs library.a library.o

This invokes ar with the right plugin arguments, in my case were

--plugin /usr/lib/gcc/x86_64-unknown-linux-gnu/4.9.1/liblto_plugin.so
3
  • 7
    It is worth noting that there is also gcc-ranlib and gcc-nm if you need them.
    – rodrigo
    Apr 15, 2015 at 16:41
  • 2
    @rodrigo Indeed - it seems that ar is only part of the picture. I also needed to override with gcc-ranlib, as the system default couldn't handle the LTO objects. So, since it seems difficult to find anything approaching a one-liner solution to this: I got LTO working as follows: ./configure --prefix=/opt/testmake AR=gcc-ar RANLIB=gcc-ranlib CXXFLAGS='-O3 -flto' Oct 22, 2016 at 22:11
  • @underscore_d As man ranlib states, it's equivalent to ar s. I create all my static libraries with gcc-ar rcs, only one step. Jan 11, 2017 at 14:43
6

As a complimentary answer: with GCC there is also the possibility to use -ffat-lto-objects which adds classic object code to the files in the archive. This makes it possible to use the static library in code that you build without -flto.

1
  • 3
    The trouble is it's difficult to catch where LTO is failing when fat-objects are used
    – jberryman
    Mar 14, 2019 at 18:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.