Alex Rivera | Logout

How to compile C source code without a main function?

Asked 2011-04-23T12:59:48.987
56

How can I compile my C source files without needing to put a main function within them?

I get an error for the .cfiles that have no main function and don't want to have to add the main function just for compilation.

c
Edit
Report

1 Answer

14

Suppose you have hello.c:

#include<stdio.h>
#include<stdlib.h>
_start()
{
   exit(my_main());
}
int my_main()
{
   printf("Hello");
   return 0;
}

Compile as:

gcc  -nostartfiles  hello.c 

and you can get an executable out of it.

answered 2012-12-11T09:17:50.007

Your Answer