How can I compile my C source files without needing to put a main
function within them?
如何编译我的C源文件而不需要在其中放置主函数?
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函数的.cfiles错误,并且不想只为编译添加main函数。
4 个解决方案
#1
31
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输出文件。
#2
9
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. 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.
当您调用GCC时,它通常会进行预处理,编译,汇编和链接。 “整体选项”允许您在中间阶段停止此过程。例如,-c选项表示不运行链接器。然后输出由汇编器输出的目标文件组成。
The linking phase is the step that looks for main()
and complains if it doesn't find it.
链接阶段是查找main()的步骤,如果找不到则抱怨。
#3
6
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.
你可以从中获得一个可执行文件。
#4
3
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.
您可以编译没有main的单个文件,但是您无法链接它们,当然也不能运行它们,因为它们不是完整的程序。请注意,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编译该文件。
#1
31
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输出文件。
#2
9
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. 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.
当您调用GCC时,它通常会进行预处理,编译,汇编和链接。 “整体选项”允许您在中间阶段停止此过程。例如,-c选项表示不运行链接器。然后输出由汇编器输出的目标文件组成。
The linking phase is the step that looks for main()
and complains if it doesn't find it.
链接阶段是查找main()的步骤,如果找不到则抱怨。
#3
6
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.
你可以从中获得一个可执行文件。
#4
3
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.
您可以编译没有main的单个文件,但是您无法链接它们,当然也不能运行它们,因为它们不是完整的程序。请注意,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编译该文件。