Home > OS >  undefined main during linking but defined during full compilitaion process
undefined main during linking but defined during full compilitaion process

Time:11-09

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

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.

CodePudding user response:

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.

  • Related