some_id
some_id

Reputation: 29896

How to compile C source code without a main function?

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.

Upvotes: 56

Views: 76177

Answers (6)

GlenL
GlenL

Reputation: 39

To link with no main() in gcc, pick a function for the "entry" in place of main(). When linking, add this option:

-e entry

--entry=entry

Upvotes: 0

Abraham Le
Abraham Le

Reputation: 134

If you want to compile without linking, the above answers are correct, use the -c switch. If you want a "freestanding" executable, then use the -entry:function switch. The default or "hosted environment" executable has the entry point as -entry:mainCRTStartup which calls main. Note that if you are making a freestanding executable, you cannot use the standard libraries beyond the freestanding libraries <float.h>, <iso646.h>, <limits.h>, <stdalign.h>, <stdarg.h>, <stdbool.h>, <stddef.h>, <stdint.h>, and <stdnoreturn.h>.

When is a freestanding executable written? Kernels and Drivers. If you are into hobby os-dev, then you need to write them. If you work for a hardware manufacturer, such as graphics card manufacturer, you will need to write drivers for them.

Some use the embedded unit tests, while others write them as separate files with conditional compilation. Remember that #define and makefile commands have some degree of overlap.

Upvotes: 1

Kos
Kos

Reputation: 72299

On GCC, the -c switch is what you want.

-c means "compile, don't link", and you get a name.o output file.

Upvotes: 67

Vignesh
Vignesh

Reputation: 741

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.

Upvotes: 14

R.. GitHub STOP HELPING ICE
R.. GitHub STOP HELPING ICE

Reputation: 215457

You can compile individual files without main, but you cannot link them and of course cannot run them since they are not complete programs. Note that valgrind is not a static analysis tool but a runtime tool, and therefore it is useless on individual translation units not linked into a runnable program.

If you want to test individual files, a common practice is to include something like the following in each file:

#ifdef UNIT_TEST
int main(int argc, char **argv)
{
    /* unit test code goes here */
}
#endif

And compile the file with -DUNIT_TEST.

Upvotes: 4

Pascal Cuoq
Pascal Cuoq

Reputation: 80325

Use the -c option of your compiler (works for GCC, option probably identical for other c compilers).

From GCC's man page:

When you invoke GCC, it normally does preprocessing, compilation, assembly and linking. The "overall options" allow you to stop this process at an intermediate stage. For example, the -c option says not to run the linker. Then the output consists of object files output by the assembler.

The linking phase is the step that looks for main() and complains if it doesn't find it.

Upvotes: 10

Related Questions