为什么链接到图书馆? [英] why link against a library?

查看:52
本文介绍了为什么链接到图书馆?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一个愚蠢的问题。


链接程序时,

包含文件和库文件的路径已经给出(-I,-L) ,

为什么仍然需要通过-l给出一些库的名称,比如说-lm用于

数学库。否则,将不会得到参考错误


谢谢

a silly question.

when linking a program,
the path to include files and library files are given already (-I, -L),
why still need to give the names of some library by -l , say -lm for
math library. otherwise, will get no reference error

thanks

推荐答案

happyvalley写道:
happyvalley wrote:
一个愚蠢的问题。

链接程序时,已经给出了包含文件和库文件的路径(-I,-L),
为什么还需要通过-l给出一些库的名称,比如说-lm用于数学库。否则,将不会出现引用错误
a silly question.

when linking a program,
the path to include files and library files are given already (-I, -L),
why still need to give the names of some library by -l , say -lm for
math library. otherwise, will get no reference error



-I和-L指定可以找到分别头文件和

库的目录。他们是不同的东西,所以他们不必b $ b必然住在同一个地方。在典型的Linux系统上,

实例头文件可以在/ usr / include中找到,而库是

in / lib。

- l switch告诉链接器链接哪些特定库。

就像你使用#include来告诉哪些头文件用于翻译单元的编译。

头文件通常只保存声明。它只是告诉某些结构看起来是什么以及哪些函数存在于某处,所以

编译器知道如何编译翻译单元。之后你仍然需要这些函数的实际代码来创建一个工作程序。


参见
http://groups.google.com/group/comp....616babfa4cbc97


-

Dirk


(PGP keyID:0x448BC5DD - http://www.gnupg.org - http://www.pgp.com



-I and -L specify directories where respectively header files and
libraries can be found. They are different things so they don''t
necessarily live in the same place. On a typical Linux system for
instance header files can be found in /usr/include while libraries are
in /lib.
The -l switch tells the linker which specific libraries to link against.
Just like you used #include to tell which header files to use for
compilation of a translation unit.
Header files typically only hold declarations. It just tells what
certain structures look like and which functions exist somewhere so the
compiler knows how to compile a translation unit. After that you still
need the actual code of those functions to create a working program.

See also
http://groups.google.com/group/comp....616babfa4cbc97

--
Dirk

(PGP keyID: 0x448BC5DD - http://www.gnupg.org - http://www.pgp.com)


2006年6月12日21:57:56 -0700,happyvalley

< ni **** *********@gmail.com>在comp.lang.c ++中写道:
On 12 Jun 2006 21:57:56 -0700, "happyvalley"
<ni*************@gmail.com> wrote in comp.lang.c++:
一个愚蠢的问题。

链接程序时,包含文件和库文件的路径是已经给出(-I,-L),
为什么还需要用-l给出一些库的名称,比如说-lm用于
数学库。否则,将不会得到参考错误

感谢
a silly question.

when linking a program,
the path to include files and library files are given already (-I, -L),
why still need to give the names of some library by -l , say -lm for
math library. otherwise, will get no reference error

thanks




请注意,链接器及其操作在这里并不是真正的话题,

因为它们没有被语言定义,并且不同于一个

编译器到另一个。


但一般来说,路径或路径集合你指定

链接器搜索库可能包含数十或数百个
。链接器可以被设计为搜索它们中的每一个

以查看它是否包含它需要的任何东西,但是有两个问题

这种方法。


第一个是它可以使构建一个可执行文件花费更多时间




第二个是符号可能是在多个库中定义,

导致多个定义,或者构建

而没有错误但使用错误的函数或对象的可执行文件。


-

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



Note that linkers and their operation are not really topical here,
since they are not defined by the language, and differ from one
compilers to another.

But in general, the path or set of paths that you specify to the
linker to search for libraries might contain dozens or hundreds of
them. A linker could be designed to search every single one of them
to see if it contains anything it needs, but there are two problems
with that approach.

The first is that it could make building an executable take much
longer.

The second is that a symbol could be defined in more than one library,
leading either to multiple definitions, or an executable that builds
without error but uses the wrong function or object.

--
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


一个愚蠢的问题。

链接程序时,
已经给出了包含文件和库文件的路径(-I,-L),
为什么还需要通过-l给出一些库的名称,比如说-lm用于
数学库。否则,将没有引用错误

when linking a program,
the path to include files and library files are given already (-I, -L),
why still need to give the names of some library by -l , say -lm for
math library. otherwise, will get no reference error




这只是一个平台问题,而不是C ++问题。


在典型的编译中 - 链接工具'' - ''用于指定

包含路径,' - L''查找库的路径

和''-l' '图书馆的名称。所以这三个都很有用。

HTH



This is merely a platform question than a C++ question.

In typical compile-link tools ''-I'' is used to specify
an include path, ''-L'' the path where to find libraries
and ''-l'' the name of the library. So all three are useful.
HTH


这篇关于为什么链接到图书馆?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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