2012-05-26 21 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,而不是将其与测试源链接。