Linker returns "The move has an invalid character in the character index ..."

I am testing Ubuntu code. I am trying to run the following code

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

And when I try to compile the above code with g++ , I get the following error

 @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 

Any help or links to questions that help will be really helpful! Thank you

+63
c ++ ubuntu g ++
May 26 '12 at 12:35
source share
5 answers

I'm not sure about your incorrect move errors, but the obvious thing is missing, since you don't have a main function. You need to define an entry point to your application called main , defined in the global scope, for example:

 int main() { // TODO: implementation } 
+94
May 26 '12 at 12:40
source share

The undefined reference to main means that you did not define the main() function, which is the entry point of your program:

 int main() { // call other functions } 
+11
May 26 '12 at 12:41 pm
source share

Interestingly, I get the same error if I try to compile the .h file instead of the .c file and link it to the library all in one step.

Here is an example below:

 $ 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 

In this case, the solution is to rename the file to the end using .c :

 $ echo 'int main () {}' > test.c $ g++ test.c -ltommath && echo success success 
+7
May 26 '14 at 5:54
source share

I just came across the same thing when connecting to gtest with CMake and including a file including the main function.

So, if you are sure that you have the main thing, and you are connecting something - make sure that you do not have two int main() s!

A simple solution was to split main () into main.cpp and not associate it with test sources.

+2
Apr 30 '15 at 4:36
source share

You typed the wrong command for g ++. You should have typed something like:

 g++ file_name random.cpp 

You need to specify the output file. Otherwise, it looks like a "g ++ syntax error".

-four
Jun 05 '14 at 12:41
source share



All Articles