具有printf()函数调用的程序,不包括< stdio.h> [英] program having printf() function invocation without including <stdio.h>

查看:55
本文介绍了具有printf()函数调用的程序,不包括< stdio.h>的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,


int main()

{

printf(" not included stdio.h") ;

}


是的,我没有包括stdio.h,我的编译器会生成一个

警告,并且会假设它会返回一个int,我的问题是链接器如何设置将函数调用链接到正确的

printf函数?

并且包含一个具有函数原型的头文件以任何方式帮助

链接器?

Hi Everyone,

int main()
{
printf("not included stdio.h");
}

Yes, i haven''t included stdio.h and my compiler would generate a
warning and would assume that it would return a int, my question is how
does the linker manage to link the function invocation to the proper
printf function?
and does including a header file having function prototype help the
linker in any way?

推荐答案

在文章< 11 ********************** @ 11g2000cwr.googlegroups中。 com>,

< sa ***** @ yahoo.co.inwrote:
In article <11**********************@11g2000cwr.googlegroups. com>,
<sa*****@yahoo.co.inwrote:

>大家好,

int main()

{

printf(不包括stdio.h);

}


是的,我没有包含stdio.h,我的编译器会生成一个
警告,并假设它会返回一个int,我的问题是如何
链接器是否设法将函数调用链接到正确的printf函数?

并且包含一个包含函数原型的头文件以任何方式帮助
链接器?
>Hi Everyone,

int main()
{
printf("not included stdio.h");
}

Yes, i haven''t included stdio.h and my compiler would generate a
warning and would assume that it would return a int, my question is how
does the linker manage to link the function invocation to the proper
printf function?
and does including a header file having function prototype help the
linker in any way?



机器人会告诉你这是未定义的行为,因为所有的b
通常很糟糕的原因。

The droids will tell you that this is undefined behavior, for all the
usual drecky reasons.


sam _... @ yahoo.co.in写道:
sam_...@yahoo.co.in wrote:

大家好,


int main()

{

printf(" not included stdio.h");

}


是的,我没有包含stdio.h,我的编译器会生成一个

警告,并假设它会返回一个int,我的问题是如何

链接器是否设法将函数调用链接到正确的

printf函数?

并包含一个包含函数原型的头文件帮助

链接器有什么方式?
Hi Everyone,

int main()
{
printf("not included stdio.h");
}

Yes, i haven''t included stdio.h and my compiler would generate a
warning and would assume that it would return a int, my question is how
does the linker manage to link the function invocation to the proper
printf function?
and does including a header file having function prototype help the
linker in any way?



原型主要用于编译器的好处,因为它指定了函数期望的参数的数量和类型,如果
any,以及它返回的

类型的值,如果有的话。它有助于编译器在调用函数时输出

正确的对象代码,并对返回值执行

正确的转换。


链接器将只搜索指定用于

的库存档文件标签''printf''/''_ printf''并链接到目标代码中。它不需要
需要函数的原型。


严格按照标准调用函数的原型

不在范围内导致未定义的行为。在某些实现中,它可能适用于更简单的

函数。像gcc这样的一些编译器通常具有神奇地的能力。认识到某些常见功能和

包含代码内联,但所有这些都是特定于实现的行为。你不能轻易依赖它们。

The prototype is primarily for the compiler''s benifit, as it specifies
the number and type of parameters the function expects, if any, and the
type of value it returns, if any. It helps the compiler to output the
correct object code when the function is called and to perform the
correct conversions on the return value.

The linker will simply search the library archive files specified for
the label ''printf''/''_printf'' and link in the object code. It doesn''t
need the function''s prototype.

Strictly according to the standard calling a function whose prototype
is not in scope leads to undefined behaviour. It might work for simpler
functions on some implementations. Some compilers like gcc, often have
the ability to "magically" recognise certain common functions and
include the code inline, but all these are implementation specific
behaviour. You cannot portably rely on them.


sa ** ***@yahoo.co.in écrit:
sa*****@yahoo.co.in a écrit :

大家好,

int main( )

{

printf(不包括stdio.h);

}


是的,我没有包含stdio.h,我的编译器会产生一个

警告,并假设它会返回一个int,我的问题是

是怎么回事链接器设法将函数调用链接到正确的

printf函数?

并且包含一个具有函数原型的头文件帮助

链接器以任何方式?
Hi Everyone,

int main()
{
printf("not included stdio.h");
}

Yes, i haven''t included stdio.h and my compiler would generate a
warning and would assume that it would return a int, my question is how
does the linker manage to link the function invocation to the proper
printf function?
and does including a header file having function prototype help the
linker in any way?



1)连接器处理目标文件,这与标题

文件无关。

2)头文件描述了函数和模块的接口。这与

无关。目标文件。


请保持分开。


现在,界面描述是针对编译器的,根据这些描述生成

代码。

当(在C中)原型不在范围内时,自动

原型由编译器提供。它假设一个函数,

有一个不确定数量的参数并返回一个int。


在这两种情况下,原型是否在范围内,

编译器发出对函数的外部引用。

在这种情况下,目标代码将包含对函数的外部

引用; printf的" (或_printf,它取决于编译器上的
)。


链接器遍历目标文件并看到:

" Mmmm这是对printf函数的外部引用。

让我看看我是否在某个地方找到它。


然后,取决于在链接器上,它会在它使用BY DEFAULT的

库中找到它。


默认库从编译器系统到编译器系统各不相同。 />
至少启动代码总是自动包含在内,

然后,大多数编译器都认为C库是默认的

库,i; e。您不需要包含在

链接器命令行中的那个。其他编译器可能会有所不同,例如一些

脑卒中编译器系统会强迫您编写数学

库作为额外的命令行选项,而且它不是

默认包含,所以如果你使用sqrt函数来实现

实例,编译将会失败,除非你告诉链接器

添加数学库。


其他人会包含很多默认库,所以你的

代码大部分会链接而不指定任何额外的库

Microsoft Visual C例如将包括所有这些:


comctl32.lib shlwapi.lib kernel32.lib user32.lib gdi32.lib

winspool.lib comdlg32.lib advapi32.lib shell32 .lib ole32.lib

oleaut32.lib uuid.lib odbc32.lib odbccp32.lib


其他编译器系统将不那么臃肿,例如

lcc-win32仅包含6-7个库。


然后,您的问题的答案很简单:

链接器找到printf函数在

的C库中默认包含的编译器。


jacob

1) Linkers deal with object files, this has nothing to do with header
files.
2) Header files describe interfaces of functions and modules. This has
nothing to do with object files.

Please keep those apart.

Now, the interfaces descriptions are for the compiler, that generates
code according to those descriptions.

When (in C ) a prototype is not in scope, an automatic
prototype is provided by the compiler. It assumes a function that
has an undertemined number of arguments and returns an int.

In both cases, whether a prototype is in scope or not,
an external reference to a function is issued by the compiler.
In this case the object code would contain an external
reference to the function "printf" (or "_printf", it depends
on the compiler).

The linker goes through the object files and sees:
"Mmmm this is an external reference to the printf function.
Let''s look if I find it somewhere".

Then, depending on the linker, it will find it or not in the
libraries it uses BY DEFAULT.

Default libraries vary from compiler system to compiler system.
At least the startup code is ALWAYS automatically included,
then, most compilers assume the C library as a default
library, i;e. one that you do not need to include in the
linker command line. Other compilers may differ, for example some
braindead compiler systems force you to write the math
library as an extra command line option, and it is not
included by default, so if you use the sqrt function for
instance, compilation will fail unless you tell the linker to
add the math library.

Others will include a lot of default libraries so that your
code mostly will link without specifying any extra library
Microsoft Visual C for instance will include all these:

comctl32.lib shlwapi.lib kernel32.lib user32.lib gdi32.lib
winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib
oleaut32.lib uuid.lib odbc32.lib odbccp32.lib

Other compiler systems will be less bloated, for instance
lcc-win32 includes just 6-7 libraries.

Then, the answer to your question is simple:
The linker finds the printf function in the C library of
the compiler that is included by default.

jacob

这篇关于具有printf()函数调用的程序,不包括&lt; stdio.h&gt;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆