当我尝试使用-flto构建静态库时,会得到未定义的引用错误:
library.cpp:
- #include <iostream>
- void foo() {
- std::cout << "Test!" << std::endl;
- }
main.cpp中:
- void foo();
- int main() {
- foo();
- return 0;
- }
编译输出:
- $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
如果我链接到library.o而不是library.a,它可以正常工作.我失踪了什么这是与GCC 4.9.1和binutils 2.24.