简体   繁体   中英

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 .c files that have no main function and don't want to have to add the main function just for compilation.

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

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

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.

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.

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 .

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM