2012-05-26 23 views
65

我嘗試了一些Ubuntu上的代碼。我試圖運行下面的代碼鏈接器返回「重定位在符號索引處有一個無效符號...」

#include <cstdlib> 
#include <cmath> 
#include <ctime> 
#include "random.h" 

using namespace std; 

/* Function prototype! */ 
void initRandomSeed(); 

int randomInteger(int low,int high){ 
    initRandomSeed(); 
    double d= rand()/(double(RAND_MAX)+1); 
    double s= d*(double(high)-low+1); 
    return int(floor(low)+s);  
} 

double randomReal(int low,int high){ 
    initRandomSeed(); 
    double d=rand()/(double(RAND_MAX)+1); 
    double s=d*(double(high)-low+1); 
    return low+s; 
}  

bool randomChance(double p){ 
    initRandomSeed(); 
    return randomReal(0,1)<p; 
}    

void setRandomSeed(int seed){  
    initRandomSeed(); 
    srand(seed); 
}  

void initRandomSeed(){ 
    // to retain updated values across different stack frames! nice! 
    static bool initialized=false; 

    // this is executed only very first time and random value obtained from system clock! 
    if(!initialized){ 
     srand(int(time(NULL))); 
     initialized=true; 
    } 
} 

,當我嘗試使用g++編譯上面的代碼,我收到以下錯誤

@ubuntu:~/Chardway$ g++ random.cpp 
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 0 has invalid symbol index 10 
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 1 has invalid symbol index 11 
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 2 has invalid symbol index 2 
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 3 has invalid symbol index 2 
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 4 has invalid symbol index 10 
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 5 has invalid symbol index 12 
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 6 has invalid symbol index 12 
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 7 has invalid symbol index 12 
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 8 has invalid symbol index 2 
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 9 has invalid symbol index 2 
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 10 has invalid symbol index 11 
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 11 has invalid symbol index 12 
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 12 has invalid symbol index 12 
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 13 has invalid symbol index 12 
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 14 has invalid symbol index 12 
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 15 has invalid symbol index 12 
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 16 has invalid symbol index 12 
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 17 has invalid symbol index 12 
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 18 has invalid symbol index 12 
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 19 has invalid symbol index 12 
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 20 has invalid symbol index 19 
/usr/lib/gcc/x86_64-linux-gnu/4.6/../../../x86_64-linux-gnu/crt1.o: In function `_start': 
(.text+0x20): undefined reference to `main' 
collect2: ld returned 1 exit status 

任何幫助或鏈接問題,這幫助將是真的有幫助!謝謝!

回答

94

我不確定您的無效重定位錯誤,但顯而易見的是您沒有main函數。你需要一個切入點定義你的應用程序調用main,在全球範圍內,如定義:

int main() 
{ 
    // TODO: implementation 
} 
+0

重新定位錯誤似乎消失了,當我解決這個問題時,謝謝! – KodeSeeker

+4

即使主要定義,我也能得到這個結果。那麼錯誤意味着什麼? –

+1

@LennartRolland,這可能意味着你還沒有保存調用'main()'的文件。 – gsamaras

11

「未定義參考‘主’」是因爲你沒有定義main()功能,是入門你的程序的點:

int main() 
{ 
    // call other functions 
} 
7

有趣的是,我得到了同樣的錯誤,如果我嘗試編譯.h文件,而不是.c文件,鏈接對圖書館,只需一個步驟。

這裏是一個大大降低了例如:

$ echo 'int main() {}' > test.h 
$ g++ test.h -ltommath && echo success 
/usr/lib/gcc/x86_64-linux-gnu/4.8/../../../x86_64-linux-gnu/crt1.o: In function `_start': 
(.text+0x20): undefined reference to `main' 
collect2: error: ld returned 1 exit status 

在這種情況下,該解決方案是重命名文件結束與.c

$ echo 'int main() {}' > test.c 
$ g++ test.c -ltommath && echo success 
success 
+0

,請試着清理您的項目並重建它。由於您直接使用'g ++'驅動程序而不是後端編譯器,所以這並不令人意外。驅動程序使用spec文件來了解如何通過後綴處理文件。試用*任何*庫和任何'.h'文件,你會注意到它會刪除一個'.h.gch'(預編譯頭文件)。因爲這是你指示**司機**做的。 – 0xC0000022L

+0

上面的錯誤是我第一次觀察輸入源代碼的文件名,以對'g ++'的輸出產生影響。我認爲錯誤的原因既不明顯,也令人驚訝。我對於spec文件和編譯器驅動程序沒有太多的瞭解,而且我以前從來不需要知道這些變幻莫測的東西。雖然我認爲錯誤的原因既不明顯,也令人驚訝,但我從未相信也不暗示這種行爲是錯誤的。同時,感謝解釋,即使它超出了我對'g ++'的工作知識。 – mpb

-4

已鍵入錯誤的命令爲克++。您應該鍵入類似:

g++ file_name random.cpp 

您需要命名輸出文件。否則就像「g ++語法錯誤」。

+5

也許你的意思是 g ++ -o file_name random.cpp – Bulletmagnet

2

當gtest與CMake連接幷包含一個包含主函數的文件時,我只是面對同樣的事情。

所以,如果你確定你有一個主體,並且你在鏈接某個東西 - 確保你沒有兩個int main()

簡單的解決方案是將main()拆分爲main.cpp,而不是將其與測試源鏈接。