Undefined reference to main - collect2: ld returned 1 exit status

后端 未结 7 2185
栀梦
栀梦 2020-12-07 22:43

I\'m trying to compile a program (called es3), but, when I write from terminal:

gcc es3.c -o es3

it appears this message:

/usr/l         


        
7条回答
  •  爱一瞬间的悲伤
    2020-12-07 22:44

    Perhaps your main function has been commented out because of e.g. preprocessing. To learn what preprocessing is doing, try gcc -C -E es3.c > es3.i then look with an editor into the generated file es3.i (and search main inside it).

    First, you should always (since you are a newbie) compile with

      gcc -Wall -g -c es3.c
      gcc -Wall -g es3.o -o es3
    

    The -Wall flag is extremely important, and you should always use it. It tells the compiler to give you (almost) all warnings. And you should always listen to the warnings, i.e. correct your source code file es3.C till you got no more warnings.

    The -g flag is important also, because it asks gcc to put debugging information in the object file and the executable. Then you are able to use a debugger (like gdb) to debug your program.

    To get the list of symbols in an object file or an executable, you can use nm.

    Of course, I'm assuming you use a GNU/Linux system (and I invite you to use GNU/Linux if you don't use it already).

提交回复
热议问题