C: main not found, but it's there | Compilation error

戏子无情 提交于 2019-12-11 00:02:26

问题


When I compile two .c files like given below I get a really weird error.

Code for compiling on terminal

  gcc -I. -o main.c matrix.c -lblas -lgfortran

Error:

  /usr/bin/ld: /usr/lib/debug/usr/lib/i386-linux-gnu/crt1.o(.debug_info): relocation 0 has invalid symbol index 11
  /usr/bin/ld: /usr/lib/debug/usr/lib/i386-linux-gnu/crt1.o(.debug_info): relocation 1 has invalid symbol index 12
  /usr/bin/ld: /usr/lib/debug/usr/lib/i386-linux-gnu/crt1.o(.debug_info): relocation 2 has invalid symbol index 2
  /usr/bin/ld: /usr/lib/debug/usr/lib/i386-linux-gnu/crt1.o(.debug_info): relocation 3 has invalid symbol index 2
  /usr/bin/ld: /usr/lib/debug/usr/lib/i386-linux-gnu/crt1.o(.debug_info): relocation 4 has invalid symbol index 11
  /usr/bin/ld: /usr/lib/debug/usr/lib/i386-linux-gnu/crt1.o(.debug_info): relocation 5 has invalid symbol index 13
  /usr/bin/ld: /usr/lib/debug/usr/lib/i386-linux-gnu/crt1.o(.debug_info): relocation 6 has invalid symbol index 13
  ...

I did a bit of reading and the solution seems to be to add a main file. But I know for a fact I had the main.c file with the int main() in it before compiling. Here's the list of everything before compiling:

  > ls
  errors.txt  main.c  main.c~  matrix.c  matrix.c~  matrix.h  matrix.h~

After compilation this is the list of everything present:

  > ls
  errors.txt  main.c~  matrix.c  matrix.c~  matrix.h  matrix.h~

For some reason my main.c is getting deleted everytime I compile. What's weird is everything was working perfectly till a couple of minutes ago. Can someone help?


回答1:


gcc -I. -o main.c matrix.c -lblas -lgfortran

the -o main.c tells the compiler to write its compiled output into the file main.c, which is probably not what you want. You probably want:

gcc -I. -o progname main.c matrix.c -lblas -lgfortran



回答2:


-o main.c means the result of the compilation should be written to main.c (default is something like a.out). This is definitely not what you intended, and main.c is being deleted since the compilation fails.

If you meant "turn on optimizations", it's -O (uppercase).




回答3:


-o Option lets the c compiler save the compiled code in the file whose name is immediately after -o option

By default in the gcc compiler in unix save the compiled out put in a.out file

with -o option we can make it save in desired_filname.out file



来源:https://stackoverflow.com/questions/26623029/c-main-not-found-but-its-there-compilation-error

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!