linux下.a/.so/.la目标库区别-转

本文详细介绍了Linux平台下常见的编译结果文件类型,包括.o、.a、.so、.la和.ko文件的区别及创建实例,帮助开发者更好地理解和使用这些文件。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在linux平台上编译时,常会遇到目标库的疑问,有静态库也有动态库,单个理解都不太难,但是对复杂的工程而言,一旦混合到一起去,对整个工程的理解和调用,将会造成很大困扰,本文就汇总这几种常见编译结果文件的区别。

一、格式说明

linux下编译,常会遇到后缀为:.o .so .a .la .ko等格式文件,尽管linux并不以扩展名作为识别文件格式的唯一依据,但规范约定还是有的,如下:

  • .o 是目标对象文件,相当于windows中的.obj文件

  • .a 为静态库,可以是一个或多个.o合在一起,用于静态连接

  • .la 为libtool生成的共享库,其实是个配置文档。可以用$file *.la查看*.la文件,或用vi来查看。

  • .so 为共享库,类似windows平台的dll文件

补充: 还有一种扩展名为.ko 文件,不过它是Linux内核使用的动态链接文件后缀,属于模块文件,用来在Linux系统启动时加载内核模块。

二、创建实例1、创建.o对象文件

$ gcc -c test.c

生成test.o,跳过链接对象,所以不是可执行文件。

2、创建.a静态库文件

$ ar -r libtest.a test1.o test2.o

3、创建动态库.so

$ gcc -Wall -fpic -shared test1.c test2.c -o libtest.so

上一句执行,将test1.c和test2.c编译生成动态库文件libtest.so

4、链接库文件

$ gcc -Wall -fpic -shared -Ltest test3.c -o libtest.so

编译test3.c后并与静态libtest.a链接(默认会到/usr/lib下找该文件)生成libtest.so动态库。

5、生成.la库

.la库一般通过makefile进行,当然也可以通过命令行进行,参考命令:

$libtool --mode=link gcc -o libmylib.la -rpath /usr/lib –L/usr/lib –la

libtool将会搜索libmylib.a文件,并传家libmylib.la。更多libtool帮助如下:

view sourceprint?

01Tst@Tst-PC /cygdrive/d/

02$ libtool --help

03Usage: libtool [OPTION]... [MODE-ARG]...

04 

05Provide generalized library-building support services.

06 

07--config show all configuration variables

08--debug enable verbose shell tracing

09-n, --dry-run display commands without modifying any files

10--features display basic configuration information and exit

11--mode=MODE use operation mode MODE

12--preserve-dup-deps don't remove duplicate dependency libraries

13--quiet, --silent don't print informational messages

14--no-quiet, --no-silent

15print informational messages (default)

16--tag=TAG use configuration variables from tag TAG

17-v, --verbose print more informational messages than default

18--no-verbose don't print the extra informational messages

19--version print version information

20-h, --help, --help-all print short, long, or detailed help message

21 

22MODE must be one of the following:

23 

24clean remove files from the build directory

25compile compile a source file into a libtool object

26execute automatically set library path, then run a program

27finish complete the installation of libtool libraries

28install install libraries or executables

29link create a library or an executable

30uninstall remove libraries from an installed directory

31 

32MODE-ARGS vary depending on the MODE. When passed as first option,

33`--mode=MODE' may be abbreviated as `MODE' or a unique abbreviation of that.

34Try `libtool --help --mode=MODE' for a more detailed description of MODE.

35 

36When reporting a bug, please describe a test case to reproduce it and

37include the following information:

38 

39host-triplet: i686-pc-cygwin

40shell: /bin/sh

41compiler: gcc

42compiler flags: -g -O2 -pipe

43linker: /usr/i686-pc-cygwin/bin/ld.exe (gnu? yes)

44libtool: (GNU libtool) 2.4

45automake: automake (GNU automake) 1.11.1

46autoconf: autoconf (GNU Autoconf) 2.68

47 

48Report bugs to <bug-libtool@gnu.org>.

49GNU libtool home page: <http://www.gnu.org/software/libtool/>.

50General help using GNU software: <http://www.gnu.org/gethelp/>.

参考资料:

http://wiki.ubuntu.org.cn/index.php?title=Compiling_C&variant=zh-hans

http://afreez.blog.51cto.com/59057/7338

http://blog.youkuaiyun.com/lhf_tiger/article/details/7065289

http://topic.youkuaiyun.com/u/20080124/13/c34ece2c-81a0-4818-bec4-d705aa4aecb1.html
 


/opt/gcc-linaro-6.3.1-2017.05-x86_64_arm-linux-gnueabihf/bin/../lib/gcc/arm-linux-gnueabihf/6.3.1/../../../../arm-linux-gnueabihf/bin/ld: Dwarf Error: found dwarf version '5', this reader only handles version 2, 3 and 4 information. ./gpio/source/lib/libgpiod.a(libgpiod_la-core.o): In function `gpiod_chip_open': core.c:(.text+0x60): undefined reference to `__lstat64_time64' core.c:(.text+0x82): undefined reference to `__stat64_time64' /opt/gcc-linaro-6.3.1-2017.05-x86_64_arm-linux-gnueabihf/bin/../lib/gcc/arm-linux-gnueabihf/6.3.1/../../../../arm-linux-gnueabihf/bin/ld: Dwarf Error: found dwarf version '256', this reader only handles version 2, 3 and 4 information. core.c:(.text+0x1c6): undefined reference to `__ioctl_time64' ./gpio/source/lib/libgpiod.a(libgpiod_la-core.o): In function `gpiod_line_update': core.c:(.text+0x2ea): undefined reference to `__ioctl_time64' ./gpio/source/lib/libgpiod.a(libgpiod_la-core.o): In function `line_request_values': core.c:(.text+0x504): undefined reference to `__ioctl_time64' /opt/gcc-linaro-6.3.1-2017.05-x86_64_arm-linux-gnueabihf/bin/../lib/gcc/arm-linux-gnueabihf/6.3.1/../../../../arm-linux-gnueabihf/bin/ld: Dwarf Error: found dwarf version '5', this reader only handles version 2, 3 and 4 information. ./gpio/source/lib/libgpiod.a(libgpiod_la-core.o): In function `line_request_event_single': core.c:(.text+0x6e0): undefined reference to `__ioctl_time64' ./gpio/source/lib/libgpiod.a(libgpiod_la-core.o): In function `gpiod_line_get_value_bulk': core.c:(.text+0x942): undefined reference to `__ioctl_time64' /opt/gcc-linaro-6.3.1-2017.05-x86_64_arm-linux-gnueabihf/bin/../lib/gcc/arm-linux-gnueabihf/6.3.1/../../../../arm-linux-gnueabihf/bin/ld: Dwarf Error: found dwarf version '55303', this reader only handles version 2, 3 and 4 information. ./gpio/source/lib/libgpiod.a(libgpiod_la-core.o):core.c:(.text+0x968): more undefined references to `__ioctl_time64' follow /opt/gcc-linaro-6.3.1-2017.05-x86_64_arm-linux-gnueabihf/bin/../lib/gcc/arm-linux-gnueabihf/6.3.1/../../../../arm-linux-gnueabihf/bin/ld: Dwarf Error: found dwarf version '6656', this reader only handles version 2, 3 and 4 information. ./gpio/source/lib/libgpiod.a(libgpiod_la-core.o): In function `gpiod_line_event_wait_bulk': core.c:(.text+0xf30): undefined reference to `__ppoll64' /opt/gcc-linaro-6.3.1-2017.05-x86_64_arm-linux-gnueabihf/bin/../lib/gcc/arm-linux-gnueabihf/6.3.1/../../../../arm-linux-gnueabihf/bin/ld: Dwarf Error: found dwarf version '5', this reader only handles version 2, 3 and 4 information. ./gpio/source/lib/libgpiod.a(libgpiod_la-helpers.o): In function `gpiod_chip_open_lookup': helpers.c:(.text+0x178): undefined reference to `__isoc23_strtoul' collect2: 错误: ld 返回 1 make: *** [Makefile:63:build/bin/demo] 错误 1
06-02
/opt/gcc-linaro-7.5.0-2019.12-x86_64_arm-linux-gnueabihf/bin/../lib/gcc/arm-linux-gnueabihf/7.5.0/../../../../arm-linux-gnueabihf/bin/ld: Dwarf Error: found dwarf version '5', this reader only handles version 2, 3 and 4 information. ./gpio/source/lib/libgpiod.a(libgpiod_la-core.o): In function `gpiod_chip_open': core.c:(.text+0x60): undefined reference to `__lstat64_time64' core.c:(.text+0x82): undefined reference to `__stat64_time64' /opt/gcc-linaro-7.5.0-2019.12-x86_64_arm-linux-gnueabihf/bin/../lib/gcc/arm-linux-gnueabihf/7.5.0/../../../../arm-linux-gnueabihf/bin/ld: Dwarf Error: found dwarf version '256', this reader only handles version 2, 3 and 4 information. core.c:(.text+0x1c6): undefined reference to `__ioctl_time64' ./gpio/source/lib/libgpiod.a(libgpiod_la-core.o): In function `gpiod_line_update': core.c:(.text+0x2ea): undefined reference to `__ioctl_time64' ./gpio/source/lib/libgpiod.a(libgpiod_la-core.o): In function `line_request_values': core.c:(.text+0x504): undefined reference to `__ioctl_time64' /opt/gcc-linaro-7.5.0-2019.12-x86_64_arm-linux-gnueabihf/bin/../lib/gcc/arm-linux-gnueabihf/7.5.0/../../../../arm-linux-gnueabihf/bin/ld: Dwarf Error: found dwarf version '5', this reader only handles version 2, 3 and 4 information. ./gpio/source/lib/libgpiod.a(libgpiod_la-core.o): In function `line_request_event_single': core.c:(.text+0x6e0): undefined reference to `__ioctl_time64' ./gpio/source/lib/libgpiod.a(libgpiod_la-core.o): In function `gpiod_line_get_value_bulk': core.c:(.text+0x942): undefined reference to `__ioctl_time64' /opt/gcc-linaro-7.5.0-2019.12-x86_64_arm-linux-gnueabihf/bin/../lib/gcc/arm-linux-gnueabihf/7.5.0/../../../../arm-linux-gnueabihf/bin/ld: Dwarf Error: found dwarf version '55303', this reader only handles version 2, 3 and 4 information. ./gpio/source/lib/libgpiod.a(libgpiod_la-core.o):core.c:(.text+0x968): more undefined references to `__ioctl_time64' follow /opt/gcc-linaro-7.5.0-2019.12-x86_64_arm-linux-gnueabihf/bin/../lib/gcc/arm-linux-gnueabihf/7.5.0/../../../../arm-linux-gnueabihf/bin/ld: Dwarf Error: found dwarf version '6656', this reader only handles version 2, 3 and 4 information. ./gpio/source/lib/libgpiod.a(libgpiod_la-core.o): In function `gpiod_line_event_wait_bulk': core.c:(.text+0xf30): undefined reference to `__ppoll64' /opt/gcc-linaro-7.5.0-2019.12-x86_64_arm-linux-gnueabihf/bin/../lib/gcc/arm-linux-gnueabihf/7.5.0/../../../../arm-linux-gnueabihf/bin/ld: Dwarf Error: found dwarf version '5', this reader only handles version 2, 3 and 4 information. ./gpio/source/lib/libgpiod.a(libgpiod_la-helpers.o): In function `gpiod_chip_open_lookup': helpers.c:(.text+0x178): undefined reference to `__isoc23_strtoul' collect2: 错误: ld 返回 1 make: *** [Makefile:63:build/bin/demo] 错误 1 sed@sed-VMware-Virtual-Platform:~/gui/linux$ 如何解决?
06-02
<think>我们正在解决libjpeg安装过程中出现的Permissiondenied错误。这种错误通常发生在尝试向系统目录写入文件而没有足够权限时。根据提供的引用,虽然没有直接关于libjpeg的安装错误,但我们可以从类似错误中推断解决方案。引用[3]中提到了编译错误,引用[4]展示了一个安装脚本,其中使用了`makeinstall`等命令。这些操作通常需要root权限。常见的解决方案:1.使用sudo:在安装命令前加上sudo,例如:`sudomakeinstall`2.更改安装目录权限:如果不想每次使用sudo,可以更改安装目录的权限(但需谨慎,避免安全风险)3.指定用户有权限的安装路径:在配置时使用`--prefix`指定一个用户有写入权限的目录具体到libjpeg安装:步骤1:解压源码包并进入目录步骤2:配置。如果希望安装到系统目录(如/usr/local),则需要root权限;或者指定一个用户目录(如$HOME/local)则不需要root权限。步骤3:编译和安装方案一:使用root权限安装(推荐用于系统目录)```./configuremakesudomakeinstall```方案二:安装到用户目录```./configure--prefix=$HOME/localmakemakeinstall```然后需要将$HOME/local/bin添加到PATH环境变量,将$HOME/local/lib添加到LD_LIBRARY_PATH环境变量。如果遇到权限问题,请检查:-安装目标目录的所有权:使用`ls-ld/目标目录`查看-当前用户是否有写入权限另外,引用[1]中提到了使用rosdep安装时遇到的权限问题,它使用了`--as-rootpip:no`选项来避免使用root权限,但最终失败,因为需要链接。这提醒我们,有些安装步骤确实需要root权限。根据引用[4]的安装脚本,它使用了`makeinstall`,并且前面有`[$?-eq0]&&makeinstall`,这意味着每一步都要检查前一步是否成功。我们可以借鉴这种严谨的做法。因此,针对libjpeg的安装,如果选择系统目录安装,请使用sudo。如果选择用户目录,则不需要,但需要配置环境变量。如果已经尝试过安装并失败,可能需要清理之前的安装尝试(如删除编译目录重新解压)再重试。问题:如何避免在安装过程中出现Permissiondenied错误?答案:确保对安装目标目录有写入权限。如果没有,则使用sudo(针对系统目录)或更改安装目录(用户目录)。生成相关问题:1.如何将用户目录下的添加到系统路径?2.安装软件时,使用系统目录和用户目录有什么区别?3.如何清理失败的安装尝试?4.为什么安装软件需要root权限?</think>### libjpeg安装过程中"Permission denied"错误的解决方案 "Permission denied"错误通常发生在尝试向系统目录写入文件时权限不足。以下是系统级解决方案: #### 解决方案1:使用sudo权限安装 ```bash # 解压源码 tar -xzvf libjpeg.tar.gz cd libjpeg-* # 配置、编译和安装 ./configure make sudo make install # 关键:使用sudo获取root权限 ``` `sudo`命令临时提升权限,允许向`/usr/local`等系统目录写入文件[^3]。 #### 解决方案2:指定用户可写目录 ```bash ./configure --prefix=$HOME/local # 设置安装到用户目录 make make install # 无需sudo ``` 将路径添加到环境变量: ```bash echo 'export LD_LIBRARY_PATH=$HOME/local/lib:$LD_LIBRARY_PATH' >> ~/.bashrc source ~/.bashrc ``` #### 解决方案3:修复目录权限 ```bash # 查看目标目录权限 ls -ld /usr/local/lib # 修改目录所有者 sudo chown -R $USER /usr/local/lib ``` ⚠️ 警告:仅推荐用于个人开发环境,生产环境存在安全风险[^4]。 #### 验证安装 ```bash # 检查文件 ls -l /usr/local/lib | grep jpeg # 测试链接 gcc -o test test.c -ljpeg ``` ### 常见原因分析 1. **权限不足**:普通用户无权写入`/usr/lib`、`/usr/local/lib`等系统目录 2. **残留锁文件**:之前安装失败残留的锁文件导致 3. **SELinux限制**:安全增强Linux的策略限制(使用`getenforce`检查) ### 进阶调试 ```bash # 查看详细错误日志 strace make install 2>&1 | grep EACCES # 检查文件系统权限 df -Th /usr/local mount | grep noexec # 检查noexec挂载选项 ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值