c++ - 链接器返回 "relocation has an invalid symbol at symbol index..."

标签 c++ ubuntu g++

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

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

最佳答案

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

int main()
{
    // TODO: implementation
}

关于c++ - 链接器返回 "relocation has an invalid symbol at symbol index...",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10766256/

相关文章:

c++ - 3d max 与 c++ 的集成,Cal3D 从哪里开始?

linux - 文件解压再压缩后md5不一样

linux - 如何在 Linux Ubuntu 中创建 .sh 扩展文件?

linux - 是否可以在没有 root 的情况下在 CentOS 上安装 g++?

c++ - 链接共享库、静态库和动态库

c++ - 覆盖的方法不适用于基于范围的循环的 range_expression

c++ - 使用 MapViewOfFile 有什么限制吗?

PHP 代码未执行,PHP 新手

c++ - GCC constexpr 允许添加,但不允许使用地址进行按位或

c++ - 为什么我的 C++ 对象一创建就被删除?