Linker возвращает "Перемещение имеет недопустимый символ в индексе символов..."

Я тестирую код 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

Любая помощь или ссылки на вопросы, которые помогут, будут действительно полезны! Благодарю!

Ответ 1

Я не уверен в ваших неверных ошибках перемещения, но очевидная вещь отсутствует, так как у вас нет функции main. Вам необходимо определить точку входа в ваше приложение под названием main, определенное в глобальной области видимости, например:

int main()
{
    // TODO: implementation
}

Ответ 2

Ссылка "undefined на" main "означает, что вы не определили функцию main(), которая является точкой входа вашей программы:

int main()
{
  // call other functions
}

Ответ 3

Интересно, что я получаю ту же ошибку, если попытаюсь скомпилировать файл .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

Ответ 4

Я просто столкнулся с тем же самым при соединении в gtest с CMake и включая файл, включающий основную функцию.

Итак, если вы уверены, что у вас есть главное, и вы что-то связываете - убедитесь, что у вас нет двух int main() s!

Простым решением было разделить main() на main.cpp и не связывать его с источниками тестирования.

Ответ 5

Вы набрали неверную команду для g++. Вы должны были набрать что-то вроде:

g++ file_name random.cpp

Вам нужно указать выходной файл. В противном случае это похоже на "синтаксическую ошибку g++".