Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

undefined main during linking but defined during full compilitaion process

I am novice in C programming. So I learned different process of compilation(preproccessing, compiling, linking). My program is

#include <stdio.h>

#define testDefinition(x) printf(#x " is equal to %lf\n",x)

int main(void)
{
    testDefinition(3.15);
    return 0;
}

It is simple program which doesn’t have any sense,but problem is when I use gcc -o test test.c it works fine, but when I do that

gcc -E test.c -o test.i
gcc -C test.i -o test.o
gcc  test.o -o test

I get error

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

usr/bin/ld: /usr/lib/gcc/x86_64-linux-gnu/9/../../../x86_64-linux-gnu/Scrt1.o: in function `_start':
(.text+0x24): undefined reference to `main'
collect2: error: ld returned 1 exit status

I am using Ubuntu 20.04 and GCC compiler.

>Solution :

test.o is already the executable, you did not pass -c.

$ gcc -E test.c -o test.i
$ gcc -C test.i -o test.o
$ ./test.o
3.15 is equal ....

Because of it, test.o is an ELF file and gcc treats it as shared library (I think). Because there are no source files passed in gcc test.o -o test there is no main, so it’s undefined.

I guess, you wanted to do gcc -C -c test.i -o test.o to create an object file.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading