Linker returns “relocation has an invalid symbol at symbol index…”

后端 未结 5 1972
伪装坚强ぢ
伪装坚强ぢ 2020-12-08 00:03

I am trying out some code on Ubuntu. I\'m trying to run the following code

#include 
#include 

        
5条回答
  •  时光取名叫无心
    2020-12-08 00:38

    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
    

提交回复
热议问题