2012-05-26 2 views
65

우분투에서 일부 코드를 시험 중입니다.링커에서 "재배치에 심볼 색인에 잘못된 기호가 있습니다 ..."

@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 

오류 도움이 될 것이라고 질문에 어떤 도움이나 링크를 다음 코드를

#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++를 사용하여 위의 코드를 컴파일 할 때, 나는 다음과 같은 얻을 정말 도움이! 감사!

답변

94

잘못된 재배치 오류에 대해서는 잘 모르겠지만 확실한 결점은 main 기능이 없다는 것입니다. 당신과 같은 글로벌 범위에서 정의 main라는 응용 프로그램에 진입 점을 정의해야

int main() 
{ 
    // TODO: implementation 
} 
+0

재배치 오류가 사라진 것 같았습니다. 고칠 때 고마워! – KodeSeeker

+4

주 정의로도이 내용을 알 수 있습니다. 그러면 그 오류는 무엇을 의미할까요? –

+1

@LennartRolland,'main()'을 호출 한 파일을 저장하지 않았다는 것을 의미 할 수도 있습니다. – gsamaras

11

당신이 항목이있는 main() 함수를 정의하지 않았기 때문에 " '주'에 대한 정의되지 않은 참조는" 프로그램의 점 : 나는 도서관에 대한 .h 파일 대신 .c 파일 및 링크를 컴파일하려고하면 흥미롭게도

int main() 
{ 
    // call other functions 
} 
7

, 나는 한 번에 모두 같은 오류가 발생합니다. 여기

이 크게 감소 예입니다 :이 경우

$ 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로 종료 할 수있는 파일의 이름을 변경하는 것입니다 : 당신은 ++ g에 대한 잘못된 명령을 입력 한

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

'g ++'드라이버를 사용하고 직접 백엔드 컴파일러를 사용하지 않으므로, 이것은 전혀 놀라운 일이 아닙니다. 드라이버는 spec 파일을 사용하여 접미어로 파일을 처리하는 방법을 찾습니다. * 모든 라이브러리와'.h' 파일로 시험해 보면'.h.gch' (미리 컴파일 된 헤더) 파일이 삭제된다는 것을 알 수 있습니다. 그게 당신이 ** 운전사에게 ** 지시 한 것입니다. – 0xC0000022L

+0

위의 오류는 입력 소스 코드의 파일명이'g ++'의 결과에 영향을 끼치는 것을 처음 보았습니다. 나는 오류의 원인을 비 명백하고 놀랄만한 것으로 생각한다. 나는 스펙 파일과 컴파일러 드라이버를 기쁜 마음으로 모르고 있으며, 이전에는 그러한 변화를 알 필요가 없었습니다.오류의 원인을 비 명백하고 놀라운 것으로 생각하는 동안, 나는 그 행동이 잘못되었다는 것을 결코 믿지도 암시도하지 못했습니다. 동시에 g ++에 대한 실무 지식을 뛰어 넘더라도 설명에 감사드립니다. – mpb

-4

. 다음과 같이 입력해야합니다.

g++ file_name random.cpp 

출력 파일의 이름을 지정해야합니다. 그렇지 않으면 "g ++ 구문 오류"와 같습니다.

+5

아마도 당신이 의미 한 것입니다. g ++ -o file_name random.cpp – Bulletmagnet

2

나는 gtest와 CMake를 연결하고 main 함수를 포함하는 파일을 포함 할 때이 똑같은 문제에 직면했다.

메인을 가지고 있고 무엇인가 연결하는 경우 - int main()!

간단한 해결책은 main.cpp 파일을 main.cpp 파일로 분할하고 테스트 소스와 링크하지 않는 것입니다.

관련 문제