我如何使用lto与静态库? [英] How can I use lto with static libraries?
问题描述
当我尝试使用 -flto
构建静态库时,出现未定义的引用错误:
library.cpp :
#include
void foo(){
std :: cout<< 测试! <<的std :: ENDL;
}
main.cpp :
void foo();
int main(){
foo();
返回0;
}
汇编输出:
$ g ++ -flto -c library.cpp
$ ar rcs library.a library.o
$ g ++ -flto main.cpp library.a
/tmp/ccZIgxCY.ltrans0.ltrans.o:在函数`main'中:
ccZIgxCY.ltrans0.o :( .text + 0x5):undefined引用'foo()'
collect2:error:ld返回1退出状态
它如果我链接到 library.o
而不是 library.a
,则工作良好。我错过了什么?这是与GCC 4.9.1和binutils 2.24。
答案,正如我从这个由GCC开发者HonzaHubička发布的帖子是使用 gcc-ar
封装,而不是 ar
本身:
$ gcc-ar rcs library.a library.o
使用正确的插件参数调用 ar
,在我的例子中是 $ b $
- -plugin /usr/lib/gcc/x86_64-unknown-linux-gnu/4.9.1/liblto_plugin.so
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.
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
这篇关于我如何使用lto与静态库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!