web-dev-qa-db-fra.com

L'éditeur de liens renvoie "la relocalisation a un symbole non valide à l'indice de symbole ..."

J'essaie du code sur Ubuntu. J'essaye de lancer le code suivant

#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;
    }
}

Et quand j'essaye de compiler le code ci-dessus en utilisant g++, J'obtiens l'erreur suivante

@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

Toute aide ou des liens vers des questions qui aideraient seraient vraiment utiles! Merci!

66
seeker

Je ne suis pas sûr de vos erreurs de relocalisation non valides, mais il est évident qu'il vous manque une fonction main. Vous devez définir un point d'entrée pour votre application appelé main, défini au niveau global, tel que:

int main()
{
    // TODO: implementation
}
99
CB Bailey

La "référence indéfinie à 'main'" est due au fait que vous n'avez pas défini de fonction main(), qui constitue le point d'entrée de votre programme:

int main()
{
  // call other functions
}
11
Attila

Fait intéressant, je reçois la même erreur si je tente de compiler un fichier .h fichier au lieu d'un .c fichier et lien vers une bibliothèque, en une seule étape.

Voici un exemple grandement réduit:

$ 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

Dans ce cas, la solution consiste à renommer le fichier pour qu'il se termine par .c:

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

Je viens de faire face à la même chose lors de la liaison dans gtest avec CMake et l'inclusion d'un fichier contenant une fonction principale.

Donc, si vous êtes sûr d'avoir un principal et que vous liez quelque chose - assurez-vous de ne pas avoir deux int main() s!

Une solution simple consistait à scinder la main () dans main.cpp et non à la lier aux sources de test.

2
Chase Holland