Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
508 views
in Technique[技术] by (71.8m points)

c++ - How can I use lto with static libraries?

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...