2017-07-04 16 views
0

我正在將gcc從gcc 4.1.2升級到gcc 4.9.4。gcc升級導致模板函數的靜態局部變量變得不知道

我先用gcc4.9編譯,並啓動程序,看到如下錯誤:

[email protected]:~/bin> Load BusinessSoCfg, Name GameSvr, Dll ../SO/so, CreateFunc CreateBusinessSo 
Open Dll ../SO/so failed, ../SO/so: undefined symbol: _ZZN6CTable24IsGameTableSOSupportFuncIM5IGameFiisiiP13tagSOItemInfoEEEbT_E7pHandle 

pHandle在模板函數中定義一個靜態變量。 碼是這樣的:

template<class TPMF> 
bool CTable::IsGameTableSOSupportFunc(TPMF pfunc) 
{ 
    static void *pHandle = NULL; 
    // somethign todo 
    return true; 
} 

在我們的測試ENV(gcc4.1.2),納米-C所以下面顯示,說的符號是不明類型

0000000004d1d9d0 ? bool CTable::IsGameTableSOSupportFunc<int (IGame::*)(int, short, int, int, tagSOItemInfo*)>(int (IGame::*)(int, short, int, int, tagSOItemInfo*))::pHandle 

,但是,從編譯ENV(GCC4。 9.4)納米-C所以顯示以下,示出了符號被定義

0000000004d1d9d0 u bool CTable::IsGameTableSOSupportFunc<int (IGame::*)(int, short, int, int, tagSOItemInfo*)>(int (IGame::*)(int, short, int, int, tagSOItemInfo*))::pHandle 

更重要的是,該gcc4.9.4從烴源代碼編譯與下面配置gcc4.1.2:

$ ./configure --prefix=/data/gcc-4.9.4/ --enable-languages=c,c++ --enable-lto --enable-vtable-verify --disable-libquadmath-support --disable-libada 

causion:下面的例子未顯示如以上所描述的相同結果

例如:

//singleton.hpp, must be a hpp 
// if singleton was defined in the same file with main,everything goes fine 
#include <stdio.h> 
#include <stdlib.h> 


template <class TYPE> 
class CSingleton 
{ 
public: 
    static TYPE* Instance(void) 
    { 
     if(m_pSingleton == NULL) 
     { 
      CSingleton *pTmpCSingleton = new CSingleton; 
      m_pSingleton = pTmpCSingleton; 
     } 
     return &m_pSingleton->m_stInstance; 
    } 
protected: 
    TYPE m_stInstance; 
    static CSingleton<TYPE>* m_pSingleton; 
}; 

template <class TYPE> 
CSingleton<TYPE>* CSingleton<TYPE>::m_pSingleton = NULL; 

// main.cpp 
// if singleton was defined in the same file with main,everything goes fine 
#include "singleton.hpp" 
#include <stdio.h> 
typedef CSingleton<int> s; 
int main(){ 
    printf("%d\n", *s::Instance()); 
    return 0; 
} 

編譯gcc4.9,並運行的結果是:0

但是當我將二進制文件複製到gcc4.1.2機器時,excution將成爲核心。

Floating point exception (core dumped) 

fathermore納米-C將顯示somehing錯誤:

nm: a.out: File format not recognized 

但readelf兩個機器工作正常:

ELF Header: 
Magic: 7f 45 4c 46 02 01 01 00 00 00 00 00 00 00 00 00 
Class:        ELF64 
Data:        2's complement, little endian 
Version:       1 (current) 
OS/ABI:       UNIX - System V 
ABI Version:      0 
Type:        EXEC (Executable file) 
Machine:       Advanced Micro Devices X86-64 
Version:       0x1 
Entry point address:    0x400520 
Start of program headers:   64 (bytes into file) 
Start of section headers:   3072 (bytes into file) 
Flags:        0x0 
Size of this header:    64 (bytes) 
Size of program headers:   56 (bytes) 
Number of program headers:   8 
Size of section headers:   64 (bytes) 
Number of section headers:   30 
Section header string table index: 27 
+0

一個編譯器的bug? –

+0

因爲我不確定如何從源代碼升級gcc。我仍然想知道它是否由壞gcc/g ++工具鏈造成的。 – laughing

+0

你可以創建一個[SSCCE](http://sscce.org/)嗎? –

回答

0

它是由GCC升級造成的。從較高的gcc版本編譯的二進制文件使用gnu散列。但運行電腦的binutil不能識別這個。用-Wl,--hash-style=both-Wl,--hash-style=sysv重新編譯就可以解決這個問題。

如果我早點查看核心文件,我可以更快地理解這一點。