【发布时间】:2012-11-20 17:19:54
【问题描述】:
g++ main.c f.c 以下适用于 g++-4.2.1,但g++ -O3 main.c f.c 会发出警告
/usr/libexec/gcc/powerpc-apple-darwin8/4.2.1/ld: Undefined symbols:
int f<int>(int const*)
collect2: ld returned 1 exit status
// main.c
template <typename T>
int f( const T* A );
int main()
{
int* A = new int[10];
int ftemplate = f( A );
}
// f.c
template <typename T>
int f( const T* A )
{ return A[0];
}
int call_f()
{ int* A = new int[10];
return f( A ); // ok here but not from main()
}
在 macOS 10.4.11 powerpc-apple-darwin8-g++-4.2.1 (GCC) 4.2.1(Apple Inc. build 5564)上,
-O2 有效,-O3 无效。
在 macosx 10.7.4 i686-apple-darwin11-llvm-g++-4.2
(来自https://github.com/kennethreitz/osx-gcc-installer),
普通的g++ *.c 有效,g++ -O *.c 给出相同的ld: Undefined symbols 错误。
也许是一个错误 g++ old /usr/bin/ld ?更有可能我做了一些愚蠢的事情......
谁能帮忙,或者看看这是否适用于非 Mac ?谢谢!
【问题讨论】:
-
一般经验法则:你不使用
-O3. -
@H2CO3,好的,但这不是问题:在 10.7 上,-O2 也不起作用。为什么要破坏链接器?
-
它不应该中断。我怀疑这是一个编译器错误。
标签: macos templates linker g++ linker-errors