库和#includes代码 [英] library and #includes in code

查看:88
本文介绍了库和#includes代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果这是一个微不足道的问题我很抱歉,但是当我必须编译我的代码时,它总是让我想知道




那里有些#includes你不需要在你的

库和编译字符串中的头引用中引用。


例如标准你好.c


#include< stdio.h>

int main()

{

printf(hello world \ n);

}


编译为`cc hello.c -o hello`


这样的事情


#include< stdio.h>

#include< math.h>

int main()

{

int x;

x = pow(2,2);

printf(%d \ n,x);

}


编译这个你需要引用数学库

`cc math.c -lm -o math`


但是,在第3个例子中,我不必引用另一个头文件,

即使它不是ANSI C


#include< stdio.h>

#include< math。 h>

#include< unistd.h>


int main()

{

.........................

................ .....

.....................

}

为什么你不需要为第二个

情况包含对数学库的引用,但是你不需要引用stdio头文件或UNIX

标头?更一般地说,假设-lm是一个本地化的编译器实现,为什么你不会像你一样引用数学头文件

你在你的内部调用的任何其他外部库码?如果我想要包含我自己的专有头文件,我必须给

编译器一个完整的路径,为什么数学库不同?

解决方案

On Tue,2006年7月25日18:40:40 -0700,Eigenvector

< m4 **** ****@yahoo.com在comp.lang.c写道:


如果这是一个微不足道的问题,我道歉,但它总是让我感到奇怪<当我必须编译我的代码时,我需要



这不是一件轻而易举的事,问题在于它是你的特定工具链的一个实现

问题。


有些#includes你不需要在你的

库和编译字符串中的头引用中引用。



C标准定义了一个标准库。所有函数,类型,

宏以及标准库中的外部对象都可用于包含适当标准头文件的任何程序或其他

包含它们的正确声明。


C标准没有定义翻译

源文件和标准库的必要部分的结果如何

组合生成一个可执行程序,它只需要符合实现的b $ b符合实现方法。


例如标准hello.c


#include< stdio.h>

int main()

{

printf(" hello world \ n");

}


编译为`cc hello.c - o你好`


这样的事情


#include< stdio.h>

#include< ; math.h>

int main()

{

int x;

x = pow(2,2);

printf("%d \ nn",x);

}


编译这个你需要引用数学库

`cc math.c -lm -o math`



听起来你正在使用Linux / UNIX实现,或者可能是一个移植到另一个系统的

版本的gcc。这是由于古老的

UNIX大师阴谋让新手在黑暗中正确地向系统管理员敬拜。


在UNIX系统上传统的工具集将

C标准库的一部分放在自动链接的文件中,但要将

浮点函数放入一个单独的库,必须直接指定

。 C标准中没有任何内容需要

这个。


曾几何时,在读取库文件的日子里

打孔纸胶带(好吧,不是那个),它可能有意义并且

节省了构建程序的时间,以减少一些功能不太常用

用完了标准链接搜索。考虑到处理器的速度,并且在今天的平台上,任何这样的节省已经不再是重要的,甚至可能不会显着增加br />
磁盘驱动器和物理内存量。


但是,在第3个例子中,我不必引用另一个头文件,

即使它不是ANSI C


#include< stdio.h>

#include< math.h>

#include< unistd.h>


int main()

{

........................

............... .....

....................

}



再次,这是由您特定的

工具链的实施者做出的决定,基于过去20已经过时的原因

年。根本没有任何合理的理由。


在Windows平台上,从未采用过这种愚蠢的传统,

典型的原生编译器,如Microsoft,Borland,lcc-win32和

其他人,从不需要任何特殊的链接器命令来使用C语言定义的

标准库的任何部分。


为什么你不需要包含对第二个

案例的数学库的引用,但你不需要引用stdio头文件或UNIX

标头?更一般地说,假设-lm是一个本地化的编译器实现,为什么你不会像你一样引用数学头文件

你在你的内部调用的任何其他外部库码?如果我想要包含我自己的专有头文件,我必须给

编译器一个完整的路径,为什么数学库不同?



最后一段中的每一个问题都是由工具实施者完成的决定。我建议你

在其中一个gcc小组或邮件列表中询问为什么他们仍然这样做,

或者可能在新闻中:comp.unix.programmer或

新闻:comp.os.linux.development.apps,至于为什么他们仍然这样做。


为缺乏明智的答案做好准备。 />

-

Jack Klein

主页: http://JK-Technology.Com

常见问题解答

comp.lang.c http://c-faq.com/

comp.lang.c ++ http://www.parashift.com/c++-faq-lite/

alt.comp.lang.learn.c-c ++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html


< Wayyyyyy在底部>


" Jack Klein" < ja ******* @ spamcop.netwrote in message

news:km *********************** ********* @ 4ax.com ...


On Tue,2006年7月25日18:40:40 -0700,Eigenvector

< m4 ******** @ yahoo.comwrote in comp.lang.c:


>我道歉如果这是一个微不足道的问题,但是当我必须编译我的代码时,它总是让我想知道。



这不是那么微不足道,问题在于它是你的特定工具链的实施问题。


>有些#includes你不需要在你的
库中引用和编译字符串中的头引用。



C标准定义了一个标准库。所有函数,类型,

宏以及标准库中的外部对象都可用于包含适当标准头文件的任何程序或其他

包含它们的正确声明。


C标准没有定义翻译

源文件和标准库的必要部分的结果如何

组合生成一个可执行程序,它只需要符合实现的b $ b符合实现方法。


>例如标准hello.c
int main()
{
printf(" hello编译为cc hello.c -o hello







> #include< stdio.h>
#include< math.h>
int main()
{
int x;
x = pow(2 ,2);
printf("%d \ n",x);
}
编译这个你需要引用数学库
`cc math.c -lm -o math`



听起来就像你正在使用Linux / UNIX实现,或者可能是一个移植到另一个系统的g / b
版本的gcc。这是由于古老的

UNIX大师阴谋让新手在黑暗中正确地向系统管理员敬拜。


在UNIX系统上传统的工具集将

C标准库的一部分放在自动链接的文件中,但要将

浮点函数放入一个单独的库,必须直接指定

。 C标准中没有任何内容需要

这个。


曾几何时,在读取库文件的日子里

打孔纸胶带(好吧,不是那个),它可能有意义并且

节省了构建程序的时间,以减少一些功能不太常用

用完了标准链接搜索。考虑到处理器的速度,并且在今天的平台上,任何这样的节省已经不再是重要的,甚至可能不会显着增加br />
磁盘驱动器和物理内存量。


>但是,在第3个例子中,我没有必要引用另一个标题
文件,
即使它不是ANSI C

#include< stdio.h>
#include< math.h>
#include< unistd.h>

int main()
{
................... .....
....................
................. ...
}



再次,这是由您的特定

工具链的实施者做出的决定,基于在过去的20年中过时的原因已经过时了。根本没有任何合理的理由。


在Windows平台上,从未采用过这种愚蠢的传统,

典型的原生编译器,如Microsoft,Borland,lcc-win32和

其他人,从不需要任何特殊的链接器命令来使用C语言定义的

标准库的任何部分。


>为什么你不需要包含对
第二
案例的数学库的引用,但你不需要引用stdio头文件或UNIX
标头?更一般地说,假设-lm是一个本地化的编译器实现,你为什么不引用像你在你的代码中调用的任何其他外部库那样的数学头文件?
如果我想要包含我自己的专有头文件,我必须给
编译器一个完整的路径,为什么数学库不同?



最后一段中的每一个问题都是由工具实施者完成的决定。我建议你

在其中一个gcc小组或邮件列表中询问为什么他们仍然这样做,

或者可能在新闻中:comp.unix.programmer或

新闻:comp.os.linux.development.apps,至于为什么他们仍然这样做。


为缺乏明智的答案做好准备。 />

-

Jack Klein



我想我理解你的例子。让HAD可以使用

数学库是有意义的,但只有在请求时才能缩小被调用的可执行文件的内存大小。在你做出观察之前,我没有想过用这些术语




我不认为-lm标志是完全通用的再也没有,但你永远都不知道,

FEA编码员非常保守。可能是为什么我仍然需要为我的Linux集群搜索F77编译器的原因之一,因为程序员仍然认为Fortran比C更快。

数学运算和可扩展性。但是我不想在这个话题上开始一场战争,因为我不确定是否已经做过适当的基准测试

对阵Fortran 77和C现代使用相同的FEA代码。


> On Tue,2006年7月25日18:40:40 -0700,Eigenvector


>< m4 ******** @ yahoo.comwrote in comp.lang.c:


>编译[某些程序]你需要引用数学库
`cc math.c -lm -o math` [但其他
不需要额外的东西



扩展名< unistd.h>]


文章< km ********** **********************@4ax.com> ;,

杰克克莱恩< ja ******* @clampscop .netwrote:


>听起来你正在使用Linux / UNIX实现,或者可能是一个移植到另一个系统的gcc版本的gcc。这是由于古老的UNIX大师阴谋将新手保持在黑暗中并且正确地崇拜系统管理员。



或者只是简单的固执。 :-)


>在UNIX系统上传统的工具集将
C标准库的一部分放在自动链接的文件中,但要将
浮点函数放在必须直接指定的单独库中。 C标准中没有任何内容需要



另一方面,C标准中有*的东西需要

一个ANSI / ISO C编译器来编译像:


int asm;

char * typeof;


当你只使用cc时会失败;在这些系统上。


这里的诀窍是cc。是*不是ANSI / ISO C编译器!它是
是not-quite-C-Compiler。这些系统应该(但通常

不会)附带拼写类似于acc的命令。或者isocc

或者最好只是c89,它调用实际的C编译器。


c89命令不仅编译C代码,而且自动添加

" -lm"需要时。理想情况下:


c89 ./foo.c


生成./foo(不是./a.out),并处理使用的代码pow()和

log()等等,并正确接受使用asm的代码。和

" typeof"作为普通标识符,并正确诊断

使用非标准扩展(无论如何都需要诊断)的代码。


>每一个您在最后一段中的问题完全由工具实施者做出决定。我建议你在其中一个gcc小组或邮件列表中询问为什么他们仍然这样做,
或者也许是新闻:comp.unix.programmer或
news:comp.os .linux.development.apps,为什么他们仍然这样做。

为缺乏明智的答案做好准备。



或者,如果你幸运的话,你可能会得到一个c89。命令。失败

,你可以使用cc -ansi -pedantic file.c -o file -lm每个

时间(虽然我建议添加更多-W选项,并且

至少-O1,如果不是-O2)。

-

In-Real-Life:风河系统Chris Torek

美国犹他州盐湖城(40°39.22''N,111°50.29''W) +1 801 277 2603

电子邮件:忘了它 http://web.torek.net/torek/index.html

由于垃圾邮件发送者,阅读电子邮件就像在垃圾中搜索食物一样。


I apologize if this is a trivial question, but it''s always made me wonder
when I have to compile my code.

There are some #includes that you don''t really need to reference in your
library and header references in the compilation string.

For instance the standard hello.c

#include <stdio.h>
int main()
{
printf("hello world\n");
}

compiles as `cc hello.c -o hello`

Whereas something like this

#include <stdio.h>
#include <math.h>
int main()
{
int x;
x=pow(2,2);
printf("%d\n",x);
}

to compile this you need to reference the math library
`cc math.c -lm -o math`

However, in this 3rd example, I don''t have to reference another header file,
even though it isn''t ANSI C

#include <stdio.h>
#include <math.h>
#include <unistd.h>

int main()
{
.........................
.....................
.....................
}
Why do you not need to include the reference to the math library for the 2nd
case, but you don''t need to reference the stdio header files or the UNIX
headers? More generally, assuming the -lm is a localized compiler
implementation, why would you not reference the math header files like you
would any other external library that you call within your code? If I
wanted to include my own proprietary header files I''d have to give the
compiler a full path, why is the math library different?

解决方案

On Tue, 25 Jul 2006 18:40:40 -0700, "Eigenvector"
<m4********@yahoo.comwrote in comp.lang.c:

I apologize if this is a trivial question, but it''s always made me wonder
when I have to compile my code.

It''s not so much trivial, the problem is that it''s an implementation
issue with your particular tool chain.

There are some #includes that you don''t really need to reference in your
library and header references in the compilation string.

The C standard defines a standard library. All functions, types,
macros, and external objects in the standard library are available to
any program that includes the proper standard headers or otherwise
contains proper declarations of them.

The C standard does not define how the results of translating your
source files and the necessary pieces of the standard library are
combined to produce an executable program, it just requires that a
conforming implementation provide some method of doing so.

For instance the standard hello.c

#include <stdio.h>
int main()
{
printf("hello world\n");
}

compiles as `cc hello.c -o hello`

Whereas something like this

#include <stdio.h>
#include <math.h>
int main()
{
int x;
x=pow(2,2);
printf("%d\n",x);
}

to compile this you need to reference the math library
`cc math.c -lm -o math`

It sounds like you are using a Linux/UNIX implementation, or perhaps a
version of gcc ported to another system. This is due to the ancient
UNIX guru conspiracy to keep newbies in the dark and properly
worshipful to the sys admins.

It is traditional on UNIX systems for the tool set to put part of the
C standard library in on file that is linked automatically, but to put
floating point functions in a separate library that has to be
specified directly. There is nothing in the C standard that requires
this.

Once upon a time, in the days when the library files were read from
punched paper tape (OK, not quite that), it might have made sense and
saved time building a program to leave some functions less commonly
used out of the standard link search. Any such savings that existed
30 plus years ago is no longer significant, and probably not even
noticeable, on today''s platforms, given the speed of processors and
disk drives and the amount of physical memory.

However, in this 3rd example, I don''t have to reference another header file,
even though it isn''t ANSI C

#include <stdio.h>
#include <math.h>
#include <unistd.h>

int main()
{
........................
....................
....................
}

Again, this is a decision made by the implementers of your particular
tool chain, based on reasons that have been obsolete for the past 20
years. And there is no longer any good reason for it at all.

On the Windows platform, which never adopted this silly tradition,
typical native compilers such as Microsoft, Borland, lcc-win32, and
others, never need any special linker commands to use any part of the
standard library defined by the C language.

Why do you not need to include the reference to the math library for the 2nd
case, but you don''t need to reference the stdio header files or the UNIX
headers? More generally, assuming the -lm is a localized compiler
implementation, why would you not reference the math header files like you
would any other external library that you call within your code? If I
wanted to include my own proprietary header files I''d have to give the
compiler a full path, why is the math library different?

Every single one of your questions in the final paragraph are
completely decisions made by the tool implementers. I''d suggest you
ask in one of the gcc groups or mailing lists why they still do this,
or perhaps on news:comp.unix.programmer or
news:comp.os.linux.development.apps, as to why they still do this.

Be prepared for a lack of sensible answers.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html


<Wayyyyyy down at the bottom>

"Jack Klein" <ja*******@spamcop.netwrote in message
news:km********************************@4ax.com...

On Tue, 25 Jul 2006 18:40:40 -0700, "Eigenvector"
<m4********@yahoo.comwrote in comp.lang.c:

>I apologize if this is a trivial question, but it''s always made me wonder
when I have to compile my code.


It''s not so much trivial, the problem is that it''s an implementation
issue with your particular tool chain.

>There are some #includes that you don''t really need to reference in your
library and header references in the compilation string.


The C standard defines a standard library. All functions, types,
macros, and external objects in the standard library are available to
any program that includes the proper standard headers or otherwise
contains proper declarations of them.

The C standard does not define how the results of translating your
source files and the necessary pieces of the standard library are
combined to produce an executable program, it just requires that a
conforming implementation provide some method of doing so.

>For instance the standard hello.c

#include <stdio.h>
int main()
{
printf("hello world\n");
}

compiles as `cc hello.c -o hello`

Whereas something like this

#include <stdio.h>
#include <math.h>
int main()
{
int x;
x=pow(2,2);
printf("%d\n",x);
}

to compile this you need to reference the math library
`cc math.c -lm -o math`


It sounds like you are using a Linux/UNIX implementation, or perhaps a
version of gcc ported to another system. This is due to the ancient
UNIX guru conspiracy to keep newbies in the dark and properly
worshipful to the sys admins.

It is traditional on UNIX systems for the tool set to put part of the
C standard library in on file that is linked automatically, but to put
floating point functions in a separate library that has to be
specified directly. There is nothing in the C standard that requires
this.

Once upon a time, in the days when the library files were read from
punched paper tape (OK, not quite that), it might have made sense and
saved time building a program to leave some functions less commonly
used out of the standard link search. Any such savings that existed
30 plus years ago is no longer significant, and probably not even
noticeable, on today''s platforms, given the speed of processors and
disk drives and the amount of physical memory.

>However, in this 3rd example, I don''t have to reference another header
file,
even though it isn''t ANSI C

#include <stdio.h>
#include <math.h>
#include <unistd.h>

int main()
{
........................
....................
....................
}


Again, this is a decision made by the implementers of your particular
tool chain, based on reasons that have been obsolete for the past 20
years. And there is no longer any good reason for it at all.

On the Windows platform, which never adopted this silly tradition,
typical native compilers such as Microsoft, Borland, lcc-win32, and
others, never need any special linker commands to use any part of the
standard library defined by the C language.

>Why do you not need to include the reference to the math library for the
2nd
case, but you don''t need to reference the stdio header files or the UNIX
headers? More generally, assuming the -lm is a localized compiler
implementation, why would you not reference the math header files like
you
would any other external library that you call within your code? If I
wanted to include my own proprietary header files I''d have to give the
compiler a full path, why is the math library different?


Every single one of your questions in the final paragraph are
completely decisions made by the tool implementers. I''d suggest you
ask in one of the gcc groups or mailing lists why they still do this,
or perhaps on news:comp.unix.programmer or
news:comp.os.linux.development.apps, as to why they still do this.

Be prepared for a lack of sensible answers.

--
Jack Klein

I think I understand given your examples. It makes sense to have HAD the
math libraries available but only upon request in order to shrink the memory
size of the executable being called. I hadn''t thought of it in those terms
until you made your observation.

I don''t think the -lm flag is totally universal anymore, but you never know,
FEA coders are pretty damn conservative. Probably one of the reasons why
I''m still having to scrounge up F77 compilers for my Linux clusters because
the programmers still think Fortran is faster than C when it comes to
mathematical operations and scalability. But I don''t want to start a war on
that topic, because I''m not sure a proper benchmark has ever been done
against Fortran 77 and C modern using the same FEA code.


>On Tue, 25 Jul 2006 18:40:40 -0700, "Eigenvector"

><m4********@yahoo.comwrote in comp.lang.c:

>to compile [some programs] you need to reference the math library
`cc math.c -lm -o math` [yet nothing extra is needed for other

extensions like <unistd.h>]

In article <km********************************@4ax.com>,
Jack Klein <ja*******@spamcop.netwrote:

>It sounds like you are using a Linux/UNIX implementation, or perhaps a
version of gcc ported to another system. This is due to the ancient
UNIX guru conspiracy to keep newbies in the dark and properly
worshipful to the sys admins.

Or just plain stubbornness. :-)

>It is traditional on UNIX systems for the tool set to put part of the
C standard library in on file that is linked automatically, but to put
floating point functions in a separate library that has to be
specified directly. There is nothing in the C standard that requires
this.

On the other hand, there *is* stuff in the C standard that requires
an ANSI/ISO C compiler to compile code like:

int asm;
char *typeof;

which will fail when you just use "cc" on those systems.

The trick here is that "cc" is *not* the ANSI/ISO C compiler! It
is the "not-quite-C-Compiler". These systems should (but usually
do not) come with a command spelled something like "acc" or "isocc"
or, perhaps best, just "c89", which invokes the actual C compiler.

The "c89" command not only compiles C code, but automatically adds
"-lm" whenever needed. Ideally:

c89 ./foo.c

produces ./foo (not ./a.out), and handles code that uses pow() and
log() and so on, and correctly accepts code that uses "asm" and
"typeof" as ordinary identifiers, and correctly diagnoses code that
uses nonstandard extensions (those that require diagnostics anyway).

>Every single one of your questions in the final paragraph are
completely decisions made by the tool implementers. I''d suggest you
ask in one of the gcc groups or mailing lists why they still do this,
or perhaps on news:comp.unix.programmer or
news:comp.os.linux.development.apps, as to why they still do this.

Be prepared for a lack of sensible answers.

Or, if you are lucky, you may actually get a "c89" command. Failing
that, you can use "cc -ansi -pedantic file.c -o file -lm" every
time (although I would suggest adding more -W options, and at
least -O1 if not -O2).
--
In-Real-Life: Chris Torek, Wind River Systems
Salt Lake City, UT, USA (40°39.22''N, 111°50.29''W) +1 801 277 2603
email: forget about it http://web.torek.net/torek/index.html
Reading email is like searching for food in the garbage, thanks to spammers.


这篇关于库和#includes代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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