gcc中的-l选项 [英] The -l option in gcc

查看:211
本文介绍了gcc中的-l选项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚开始阅读这本书: Unix环境下的高级编程并尝试编译第一个示例代码,与此主题中的代码相同。
虽然编译问题可以通过命令来解决:

 

gcc - o myls myls.c -I SCADDRESS / include / -L SCADDRESS / lib / -lapue



<我在GCC手册中查了一下,找不到gcc选项-lxxx是什么意思,其中xxx代表头文件的基本名称(在这种情况下,它是apue.h)。根据手册,xxx应该是一些库文件,以.so作为共享目标文件结尾,或者使用.a作为静态库。


任何人都可以解释一下吗? Thx提前:)

这是记录在§ 2.13GCC手册中的链接选项:


-l <​​i>


在链接时搜索名为 library 的库。

它在您编写此选项的命令中有所不同,
链接程序按
指定的顺序搜索进程库和目标文件。因此,`foo.o -lz bar.o'搜索库`z'
文件`foo.o'但在`bar.o'之前。如果`bar.o'引用
`z'中的函数,那些函数可能不会被加载。

$ b

链接器搜索库的标准目录列表
,它实际上是一个名为`LIB的文库的.A'。链接器然后使用这个文件,就好像它已经通过名字精确指定一样。

搜索的目录包括几个标准系统目录
加上你用`-L'



通常这样找到的文件是库文件 - 归档文件
成员是对象文件。链接器通过
扫描存档文件来处理定义符号的成员,该符号迄今已引用但尚未定义
。但是,如果找到的文件是一个
普通对象文件,它将以通常的方式链接。使用`-l'选项和指定文件名称之间唯一的
差异是`-l'`lib'`.a'
来环绕并搜索多个目录。



I have just began reading the book: Advanced Programming in Unix Enviornment and try to compile the first example code, just the same as in this thread. Although the problem for the compilation is solved using the command:


gcc -o myls myls.c -I SCADDRESS/include/ -L SCADDRESS/lib/ -lapue

I looked it up in the GCC manual and can't find what does the gcc option -lxxx mean, where xxx stands for the base name of a header file(in this case, it's apue.h). According to the manual, xxx should be some library files, either end with .so for shared object files, or with .a for static libraries.

Can anyone explain it a little bit? Thx in advance:)

解决方案

This is documented in §2.13 "Options for Linking" in the GCC manual:

-llibrary

Search the library named library when linking.

It makes a difference where in the command you write this option; the linker searches processes libraries and object files in the order they are specified. Thus, `foo.o -lz bar.o' searches library `z' after file `foo.o' but before `bar.o'. If `bar.o' refers to functions in `z', those functions may not be loaded.

The linker searches a standard list of directories for the library, which is actually a file named `liblibrary.a'. The linker then uses this file as if it had been specified precisely by name.

The directories searched include several standard system directories plus any that you specify with `-L'.

Normally the files found this way are library files--archive files whose members are object files. The linker handles an archive file by scanning through it for members which define symbols that have so far been referenced but not defined. But if the file that is found is an ordinary object file, it is linked in the usual fashion. The only difference between using an `-l' option and specifying a file name is that `-l' surrounds library with `lib' and `.a' and searches several directories.

这篇关于gcc中的-l选项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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