67

我正在 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

任何帮助或帮助问题的链接都会非常有帮助!谢谢!

4

5 回答 5

101

我不确定您的无效重定位错误,但明显缺少的是您没有main功能。您需要为应用程序定义一个名为 的入口点main,在全局范围内定义,例如:

int main()
{
    // TODO: implementation
}
于 2012-05-26T12:40:01.953 回答
12

“未定义对'main'的引用”是因为您没有定义main()函数,这是程序的入口点:

int main()
{
  // call other functions
}
于 2012-05-26T12:41:12.590 回答
8

有趣的是,如果我尝试编译一个.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
于 2014-05-26T05:54:12.250 回答
2

在使用 CMake 链接 gtest 并包含一个包含 main 函数的文件时,我也遇到了同样的事情。

所以,如果你确定你有一个 main,并且你正在链接一些东西——确保你没有两个int main()s!

简单的解决方案是将 main() 拆分为 main.cpp 并且不将其与测试源链接。

于 2015-04-30T04:36:48.193 回答
-4

您为 g++ 输入了错误的命令。您应该输入如下内容:

g++ file_name random.cpp

您需要命名输出文件。否则就像“g++ 语法错误”。

于 2014-06-05T12:41:50.823 回答