undefined reference to main issue

后端 未结 3 1720
感动是毒
感动是毒 2021-01-22 20:03

I have written a c code file called \"utilfunc.c\" this code contains my functions that I will use through my code.

Now when I am compiling my \"utilfunc.c\" file an

相关标签:
3条回答
  • 2021-01-22 20:39

    Add -c to your compiler invocation, so that you only compile the translation unit but do not link it to a complete program:

    gcc -c -o foo.o foo.c
    
    0 讨论(0)
  • 2021-01-22 20:40

    This was not the issue for the original poster, but here is an unusual cause for the same error message: option with missing path.

    gcc -I../include -O -I. -g -O2 -DNDEBUG -O3 -fPIC -I -c graph.c -o graph.o
    /usr/lib64/gcc/x86_64-suse-linux/4.8/../../../../lib64/crt1.o: In function `_start':
    /home/abuild/rpmbuild/BUILD/glibc-2.19/csu/../sysdeps/x86_64/start.S:118: undefined reference to `main'
    collect2: error: ld returned 1 exit status
    

    Notice the spurious -I before -c graph.c. Without it it compiles just fine

    gcc -I../include -O -I. -g -O2 -DNDEBUG -O3 -fPIC -c graph.c -o graph.o
    

    The problem is that the path is missing (-I<path>). Adding the cwd as a path (-I.) works fine for instance

    gcc -I../include -O -I. -g -O2 -DNDEBUG -O3 -fPIC -I. -c graph.c -o graph.o
    

    Any missing path after any command can yield this error; for instance with -L.

    gcc -I../include -O -I. -g -O2 -DNDEBUG -O3 -fPIC -L -c graph.c -o graph.o
    /usr/lib64/gcc/x86_64-suse-linux/4.8/../../../../lib64/crt1.o: In function `_start':
    /home/abuild/rpmbuild/BUILD/glibc-2.19/csu/../sysdeps/x86_64/start.S:118: undefined reference to `main'
    collect2: error: ld returned 1 exit status
    
    0 讨论(0)
  • 2021-01-22 20:50

    Depending on the options you give the compiler (you didn't show any, so I can't say specifically about yours), the compiler/linker will try to build a whole application, in which case it would need main defined. But you can provide different options to tell it to just compile the file given, and stop there, which sounds like what you want.

    0 讨论(0)
提交回复
热议问题