[英]g++ 4.2.1: -O breaks linking to a templated function
g++ main.c fc
可與g ++-4.2.1一起使用,但
g++ -O3 main.c fc
發出警告
/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()
}
在macosx 10.4.11 powerpc-apple-darwin8-g ++-4.2.1(GCC)4.2.1(Apple Inc.內部版本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 ++ <->舊的/ usr / bin / ld錯誤? 我更有可能做了一些愚蠢的事情...
誰能幫忙,或者看看這是否適用於非Mac? 謝謝 !
除非您為在函數調用中使用的參數顯式實例化函數模板,否則必須使函數模板定義對其調用者可見。
這包括main中的呼叫。
它可能在未優化的構建中工作,因為編譯器會為隱式函數模板實例化發出導出的函數定義符號。 C ++ Standard允許編譯器省略此操作,而GCC在這里這樣做是為了優化構建(可能只是內聯了調用,然后未使用定義符號)。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.