Linker returns "relocation has an invalid symbol at symbol index..."
Solution 1
I'm not sure about your invalid relocation errors but the obvious thing missing is that you have no main
function. You need to define an entry point to your application called main
, defined at global scope such as:
int main()
{
// TODO: implementation
}
Solution 2
The "undefined reference to 'main'" is because you did not define a main()
function, which is the entry point of your program:
int main()
{
// call other functions
}
Solution 3
Interestingly, I get the same error if I try to compile a .h
file instead of a .c
file, and link against a library, all in one step.
Here is a greatly reduced example:
$ 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 end with .c
:
$ echo 'int main () {}' > test.c
$ g++ test.c -ltommath && echo success
success
Solution 4
I just faced this same thing when linking in gtest with CMake and including a file that included a main function.
So, if you're sure you have a main, and you're linking something -- make sure you don't have two int main()
s!
Simple solution was to split the main() into main.cpp and not link it with the test sources.
seeker
Craving for knowledge and whatever follows as a result . :)
Updated on July 05, 2022Comments
-
seeker almost 2 years
I am trying out some code on Ubuntu. I'm 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 using
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 would be really helpful! Thanks!
-
seeker about 12 yearsthe relocation errors seemed to vanish, when I fixed this, Thanks!
-
Lennart Rolland over 9 yearsI get this even with main defined. What would the error mean then?
-
gsamaras almost 9 years@LennartRolland, it might mean that you haven't saved the file calling
main()
. -
Mawg says reinstate Monica almost 9 yearsTry also to clean your project and rebuild it, if you have only recently added main()
-
0xC0000022L over 7 yearsSince you use the
g++
driver and not the backend compiler directly, this is not at all surprising, though. The driver uses the spec file to find out how to handle files by their suffix. Try it out with any library and any.h
file and you'll notice it drops a.h.gch
(precompiled header) file. Because that's what you instructed the driver to do. -
mpb over 7 yearsThe above error was the first time I ever observed the filename of input source code to have an effect on the output of
g++
. I consider the cause of the error to be both non-obvious and surprising. I am blissfully unaware of spec files and compiler drivers, and I never needed to know about such vagaries previously. While I consider the cause of the error to be both non-obvious and surprising, I have never believed nor implied the behavior is incorrect. At the same time, thanks for the explanation, even if it exceeds my working knowledge ofg++
.