C语言 如何在一个文件中定义一个 C 函数,然后从另一个文件中调用它?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4271717/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I define a C function in one file, then call it from another?
提问by Sam H
Say I define a function in the file func1.c, and I want to call it from the file call.c,
how would I accomplish this? Thanks in advance!
假设我在文件中定义了一个函数func1.c,并且我想从文件中调用它call.c,我该如何实现呢?提前致谢!
回答by David Thornley
You would put a declaration for the function in the file func1.h, and add #include "func1.h"in call.c. Then you would compile or link func1.cand call.ctogether (details depend on which C system).
你会放一个声明文件中的函数func1.h,并添加#include "func1.h"中call.c。然后,你会编译或链接func1.c和call.c在一起(细节取决于其空调系统上)。
回答by Merlyn Morgan-Graham
Use a Forward Declaration
使用前向声明
For example:
例如:
typedef struct
{
int SomeMemberValue;
char* SomeOtherMemberValue;
} SomeStruct;
int SomeReferencedFunction(int someValue, SomeStruct someStructValue);
int SomeFunction()
{
SomeStruct s;
s.SomeMemberValue = 12;
s.SomeOtherMemberValue = "test string";
return SomeReferencedFunction(5, s) > 12;
}
There is a feature that allows you to reuse these forward declarations called Header Files. Simply take the forward declarations, place them in the header file, then use #includeto add them to each C source file you reference the forward declarations in.
有一项功能允许您重用这些称为Header Files 的前向声明。只需获取前向声明,将它们放在头文件中,然后使用#include它们将它们添加到您引用前向声明的每个 C 源文件中。
/* SomeFunction.c */
#include "SomeReferencedFunction.h"
int SomeFunction()
{
SomeStruct s;
s.SomeMemberValue = 12;
s.SomeOtherMemberValue = "test string";
return SomeReferencedFunction(5, s) > 12;
}
/* SomeReferencedFunction.h */
typedef SomeStruct
{
int SomeMemberValue;
char* SomeOtherMemberValue;
} SomeStruct;
int SomeReferencedFunction(int someValue, SomeStruct someStructValue);
/* SomeReferencedFunction.c */
/* Need to include SomeReferencedFunction.h, so we have the definition for SomeStruct */
#include "SomeReferencedFunction.h"
int SomeReferencedFunction(int someValue, SomeStruct someStructValue)
{
if(someStructValue.SomeOtherMemberValue == NULL)
return 0;
return someValue * 12 + someStructValue.SomeMemberValue;
}
Of course, to be able to compile both source files, and therefore the entire library or executable program, you'll need to add the output of both .c files to the linker command line, or include them in the same "project" (depending on your IDE/compiler).
当然,为了能够编译两个源文件,从而编译整个库或可执行程序,您需要将两个 .c 文件的输出添加到链接器命令行,或者将它们包含在同一个“项目”中(取决于您的 IDE/编译器)。
Many people suggest that you make header files for all your forward declarations, even if you don't think you'll need them. When you (or other people) go to modify your code, and change the signature of functions, it will save them time from having to modify all of the places where the function is forward-declared. It may also help save you from some subtle bugs, or at least confusing compiler errors.
许多人建议您为所有前向声明制作头文件,即使您认为不需要它们。当您(或其他人)修改您的代码并更改函数的签名时,这将节省他们的时间,而不必修改所有预先声明函数的地方。它还可以帮助您避免一些微妙的错误,或者至少是令人困惑的编译器错误。

