简体   繁体   English

如何在没有 main 函数的情况下编译 C 源代码?

[英]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?如何编译我的 C 源文件而不需要在其中放置main函数?

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.对于没有 main 函数并且不想添加 main 函数只是为了编译的.c文件,我收到一个错误。

On GCC, the -c switch is what you want.在 GCC 上, -c开关就是你想要的。

-c means "compile, don't link", and you get a name.o output file. -c意思是“编译,不链接”,你会得到一个name.o输出文件。

Suppose you have hello.c:假设你有 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).使用编译器的-c选项(适用于 GCC,其他 c 编译器的选项可能相同)。

From GCC's man page:从 GCC 的手册页:

When you invoke GCC, it normally does preprocessing, compilation, assembly and linking.当您调用 GCC 时,它通常会进行预处理、编译、汇编和链接。 The "overall options" allow you to stop this process at an intermediate stage. “整体选项”允许您在中间阶段停止此过程。 For example, the -c option says not to run the linker.例如,-c 选项表示不运行链接器。 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.链接阶段是寻找main()并在找不到时抱怨的步骤。

You can compile individual files without main , but you cannot link them and of course cannot run them since they are not complete programs.您可以在没有main情况下编译单个文件,但不能链接它们,当然也不能运行它们,因为它们不是完整的程序。 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.请注意,valgrind 不是静态分析工具而是运行时工具,因此它对未链接到可运行程序的单个翻译单元无用。

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 .并使用-DUNIT_TEST编译文件。

If you want to compile without linking, the above answers are correct, use the -c switch.如果你想在不链接的情况下编译,上面的答案是正确的,使用-c开关。 If you want a "freestanding" executable, then use the -entry:function switch.如果您想要一个“独立的”可执行文件,请使用-entry:function开关。 The default or "hosted environment" executable has the entry point as -entry:mainCRTStartup which calls main .默认或“托管环境”可执行文件的入口点为-entry:mainCRTStartup ,它调用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> .请注意,如果您正在制作freestanding可执行文件,则不能使用freestanding库之外的标准库<float.h><iso646.h><limits.h><stdalign.h><stdarg.h><stdbool.h><stddef.h><stdint.h><stdnoreturn.h>

When is a freestanding executable written?什么时候编写freestanding可执行文件? Kernels and Drivers . KernelsDrivers If you are into hobby os-dev , then you need to write them.如果您对os-dev ,那么您需要编写它们。 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.请记住,# #definemakefile命令有一定程度的重叠。

To link with no main() in gcc, pick a function for the "entry" in place of main().要在 gcc 中不使用 main() 进行链接,请为“条目”选择一个函数来代替 main()。 When linking, add this option:链接时,添加此选项:

-e entry -e 条目

--entry=entry --entry=入口

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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