链接目标文件,使包含变得多余?
我正在尝试一点 C 语言。我以为我已经理解了这个链接业务。但我想不是。我有一个简单的文件 main.c:
#include "function.h"
int main(char args[])
{
int print = myfunction();
}
然后是第二对文件 function.c/function.h
int myfunction(); //function.h
int myfunction() //function.c
{
return 5;
}
编译此文件效果很好。但是,无论我是否在主文件中使用 #include "function.h"
,它都可以很好地工作。那么为什么我需要包含 function.h 呢?
I am trying my hand at a bit of C. And I thought I had understood this linking business. But I guess not. I have a simple file main.c:
#include "function.h"
int main(char args[])
{
int print = myfunction();
}
then a second pair of files function.c/function.h
int myfunction(); //function.h
int myfunction() //function.c
{
return 5;
}
compiling this works great. However, it works great regardless, whether I use #include "function.h"
in my main file or not. Why would I need to include function.h then?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我不知道您使用的是什么系统,但如果它使用 gcc 或兼容的东西,请使用
Or 再次尝试,或者为您的系统打开等效选项。
大型程序总是以这种方式构建,以便编译器检查参数类型。与动态语言和脚本语言不同,C 生成实际的机器代码,并且不会在运行时检查参数计数或兼容性。
因此,函数原型被添加到基础语言中以在编译时进行类型检查。它们是可选的。
I don't know what system you are on, but if it uses gcc or something compatible, try it again with
Or, turn on the equivalent options for your system.
Large programs are always built this way, so that the compiler checks the argument types. Unlike the dynamic and scripting languages, C generates actual machine code and does not check for parameter count or compatibility at runtime.
So, the function prototypes were added to the base language to do type checking at compile time. They are optional.
AC 编译器不要求您在使用函数1 之前指定它的原型。原型只是让编译器验证您传递的参数类型是否与函数所需的类型相符——如果不正确,则隐式转换为正确的类型,并且存在隐式转换来自/到所涉及的类型。
只要您的代码是完美的,并且您使用函数的方式与该函数的预期使用方式之间没有不匹配,就不会有问题。在您的测试中,您有一个不带参数并返回 int 的函数,并且使用它的代码基本上不执行任何其他操作。这种情况很难搞砸,而且效果很好。在具有数百或数千个采用复杂类型的多个参数等的函数的真实程序中,情况变化得很快。让编译器确保您正确调用函数变得更加重要。
1 除了可变参数函数之外,即使有“可变”参数仍然基本上遵循相同的规则,就好像该函数没有原型一样。
A C compiler doesn't require that you specify the prototype for a function1 before you use it. The prototype just lets the compiler verify that the type(s) of parameter(s) you pass fit with the type(s) the function requires -- and implicitly convert to the right type if it's not right, and there is an implicit conversion from/to the types involved.
As long as your code is perfect, and there's no mismatch between the how you use a function and how that function was intended to be used, you won't have a problem. In your test, you have a function that takes no parameters, and returns an int, and the code that uses it does essentially nothing else. That's a situation that's pretty hard to screw up, and it works just fine. In a real program with hundreds or thousands of functions taking multiple parameters of complex types, etc., the situation changes pretty quickly. Letting the compiler assure that you're calling functions correctly becomes much more important.
1 With the exception of a variadic function, and even there the "variable" parameters still basically follow the same rules as if there was no prototype for the function.
当您使用尚未定义的函数时,许多 C 编译器只会假设它是一个返回 int 的 extern 函数(您经常会收到警告,但代码会编译。)当您开始使用函数时,您会遇到问题不过,参数和返回类型更复杂。
When you use a function that has not been defined, many C compilers will just assume that it's an extern function returning an int (you will often get a warning, but the code will compile.) You will run into problems when you start using functions with more complex arguments and return types, though.
如果您使用尚未声明的函数,较旧的 C 编译器将假定它具有参数
...
和返回类型int
。不建议这样做。如果您打开编译器警告,您可能会收到有关使用未声明函数的警告。如果编译器在编译 main.c 之前看到 function.h,则可以捕获更多错误,因此它确切地知道myfunction
应该如何运行。If you use a function that has not been declared, older C compilers will assume it has arguments
...
and return typeint
. This is not recommended. If you turn up compiler warnings, you'll likely get a warning about using an undeclared function. The compiler can catch more mistakes if it sees function.h before compiling main.c so it knows exactly howmyfunction
is supposed to act.您可以在头文件中声明一个函数,以便在不同的模块中使用它。您不一定需要源代码。您可以链接二进制库。在编译时,您需要声明用于检查参数、类型等的方法。
在您的简单示例中,这不是必需的。
You declare a function within a headerfile to use it in different modules. You don't necessarily need the source code. You can link against binary libraries. During compile time you need a declaration of the method for checking arguments,types etc.
In your simple example this wouldn't be required.