Creating and Using Software Libraries

本文详细介绍了在UNIX环境下创建和使用静态及共享库的方法。涵盖了库的概念、静态库与共享库的区别、库文件的识别、静态库与共享库的创建步骤、使用链接静态库与共享库的过程,以及展示库内容的命令。

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

Table of Contents

 

Introduction

About Libraries

Static Versus Shared Libraries in UNIX

Identifying Libraries

Creating a Static Library

Using (Linking to) a Static Library

Creating a Shared Library

Shared Library Names

Steps to Create the Library

Using a Shared Library

Displaying the Contents of a Library


Introduction

这篇文章告诉你如何创建和使用静态和共享(动态)库在UNIX环境下.

About Libraries

软件库是一个包含编译的代码和可能用到的数据的文件,可以被其他文件使用.它不能被运行.

Static Versus Shared Libraries in UNIX

(static library)静态库在程序编译之后可以被程序静态链接,作为可执行文件的一部分.换句话说它合并到可执行程序文件中.共享库(shared library)可以被动态链接,无论是在加载或者运行阶段,依赖于特有的系统.

Identifying Libraries

静态库文件以(.a)结尾.动态库以(.so)作为扩展,可以在后面添加版本号如(librt.so.1).所有类型的库都是以"lib"开头后面添加真实的名字命名的.例如标准的C++静态库是"libstdc++.a".

Creating a Static Library

1.创建timestuff.c和errors.c文件

2.编译c文件

gcc -c timestuff.c gcc -c errors.c

会生成两个.o文件

3.运行GNU archiver,ar,创建.a文件

ar rcs libutils.a timestuff.o errors.o

如果要添加新的文件进去也可用这条命令

4.生成后将libutils.a文件放入项目的./lib目录下,libutils.h文件放入项目的./include目录下.如何想要所有的项目都可以使用可以各自放入~目录下相关的./lib和./include目录下.

5.确保你的LIBRARY_PATH环境中包含你的静态库的路径,你的.bashrc文件中必须包含

LIBRARY_PATH="$LIBRARY_PATH:~/lib:"
export LIBRARY_PATH

保证你的gcc可以找到你常用的库文件地址,如果你想让你的库文件在标准库之前被搜索,可以用到以下命令

LIBRARY_PATH="~/lib:$LIBRARY_PATH"
export LIBRARY_PATH

6.确保包含你的头文件目录,你的.bashrc文件中必须包含

CPATH="~/include"
export CPATH

注意:不要将静态库和共享库放在同一个目录下

Using (Linking to) a Static Library

1.在你的程序中添加头文件

#include "utils.h"

2.去构建可执行程序:

gcc -o myprogram myprogram.c -lutils

如果没有修正CPATH:

gcc -o myprogram myprogram.c -lutils -I~/unix_demos/include

注意:同一个名字同一目录下动态库优先级大

如果没有修改LIBRARY_PATH,可以用以下

gcc -o myprogram myprogram.c -L~/unix_demos/lib -lutils

Creating a Shared Library

Shared Library Names

共享库将会有三个名字:

  • libutils.so.1  -这个是它的soname
  • libutils.so.1.0.1  -这个是文件的名字
  • libutils.so            -这个是编译器用的,也被叫链接名字

这三个文件之间的关系是这样的:

lrwxrwxrwx 1 root root 11 Aug 12 18:52 libacl.so -> liba l.so.1
lrwxrwxrwx 1 root root 15 Aug 12 18:51 libacl.so.1 -> liba l.so.1.1.0
-rwxr-xr-x 1 root root 31380 Aug 3 18:42 libacl.so.1.1.0

依次表示软链接

Steps to Create the Library

1.创建一个文件来创建共享库,名叫stuff.c

gcc -fPIC -g -Wall - stuff.c

-g 显示debugging 信息
-Wall 显示warning
-fPIC 告诉编译器产生与位置无关代码.则产生的代码中,没有绝对地址,全部使用相对地址,故而代码可以被加载器加载到内存的任意位置,都可以正确的执行。这正是共享库所要求的,共享库被加载时,在内存的位置不是固定的。

2.创建共享库libgoodstuff.so.1.0.1(真实名字),libgoodstuff.so.1(soname)

gcc -shared -Wl,-soname,libgoodstuff.so.1 -o libgoodstuff.so.1.0.1 stuff.o tools.o


-Wl  告诉链接编译器以逗号为分割

3.如果你是superuser,可以把文件放入/usr/lo al/lib,一般我们放入~/lib文件下,仅copy就行.

4.copy后执行下一条指令

ldconfig -n ~/lib

ldconfig是一个动态链接库管理命令,为了让动态链接库为系统所共享,还需运行动态链接库的管理命令–ldconfig。 
-n ldconfig仅扫描命令行指定的目录,不扫描默认目录(/lib,/usr/lib),也不扫描配置文件/etc/ld.so.conf所列的目录.

然后建立实际名字文件和soname文件的软链接

ln -s libgoodstuff.so.1 libgoodstuff.so

5.如果之后库文件代码修改的话只需要将libutils.so.1.0.2复制到目录下,再次idconfig 后完成,文件链接如下.

libutils.so -> libutils.so.1
libutils.so.1 -> libutils.so.0.2
libutils.so.1.0.1
libutils.so.1.0.2

Using a Shared Library

可以使用以下命令去链接

gcc -o myprogram myprogram.c -L~/lib -lgoodstuff
gcc -o myprogram myprogram.c ~/lib/libgoodstuff.so

如果LIBRARY_PATH环境是有效的,则可执行

gcc -o myprogram myprogram.c -lgoodstuff

t通过ldd命令可以查看程序对共享库的依赖以及所在的文件,如果没有出现应有的共享库,则说明未找到相应共享库的位置.

如以下成功找到的例子

linux-gate.so.1 => (0x00a31000)
libgoodstuff.so.1 => ~/lib/libgoodstuff.so.1 (0x00 aa000)
libc.so.6 => /lib/libc.so.6 (0x00110000)
/lib/ld-linux.so.2 (0x00bd5000)

如果没有

linux-gate.so.1 => (0x00a31000)
libgoodstuff.so.1 => not found
libc.so.6 => /lib/libc.so.6 (0x00110000)
/lib/ld-linux.so.2 (0x00bd5000)

修改LD_LIBRARY_PATH环境变量

LD_LIBRARY_PATH="~/lib"
export LD_LIBRARY_PATH

或者

LD_RUN_PATH="~/lib"
export LD_RUN_PATH

Displaying the Contents of a Library

静态库可以用:

ar t <libraryname>
nm -s <libraryname>

动态库

nm -s --dynamic <libraryname>

也可以用readelf命令相关查看https://blog.youkuaiyun.com/yfldyxl/article/details/81566279

 

 

ann@ann:~$ dpkg -l | grep python3 ii libpython3-dev:amd64 3.12.3-0ubuntu2 amd64 header files and a static library for Python (default) ii libpython3-stdlib:amd64 3.12.3-0ubuntu2 amd64 interactive high-level object-oriented language (default python3 version) ii libpython3.10-minimal:amd64 3.10.4-3 amd64 Minimal subset of the Python language (version 3.10) ii libpython3.10-stdlib:amd64 3.10.4-3 amd64 Interactive high-level object-oriented language (standard library, version 3.10) ii libpython3.12-dev:amd64 3.12.3-1ubuntu0.7 amd64 Header files and a static library for Python (v3.12) ii libpython3.12-minimal:amd64 3.12.3-1ubuntu0.7 amd64 Minimal subset of the Python language (version 3.12) ii libpython3.12-stdlib:amd64 3.12.3-1ubuntu0.7 amd64 Interactive high-level object-oriented language (standard library, version 3.12) ii libpython3.12t64:amd64 3.12.3-1ubuntu0.7 amd64 Shared Python runtime library (version 3.12) ii python3 3.12.3-0ubuntu2 amd64 interactive high-level object-oriented language (default python3 version) ii python3-apport 2.28.1-0ubuntu3.7 all Python 3 library for Apport crash report handling ii python3-apt 2.7.7ubuntu4 amd64 Python 3 interface to libapt-pkg ii python3-aptdaemon 1.1.1+bzr982-0ubuntu44 all Python 3 module for the server and client of aptdaemon ii python3-aptdaemon.gtk3widgets 1.1.1+bzr982-0ubuntu44 all Python 3 GTK+ 3 widgets to run an aptdaemon client ii python3-attr 23.2.0-2 all Attributes without boilerplate (Python 3) ii python3-babel 2.10.3-3build1 all tools for internationalizing Python applications - Python 3.x ii python3-bcrypt 3.2.2-1build1 amd64 password hashing library for Python 3 ii python3-blinker 1.7.0-1 all Fast, simple object-to-object and broadcast signaling (Python3) ii python3-bpfcc 0.29.1+ds-1ubuntu7 all Python 3 wrappers for BPF Compiler Collection (BCC) ii python3-brlapi:amd64 6.6-4ubuntu5 amd64 Braille display access via BRLTTY - Python3 bindings ii python3-cairo 1.25.1-2build2 amd64 Python3 bindings for the Cairo vector graphics library ii python3-certifi 2023.11.17-1 all root certificates for validating SSL certs and verifying TLS hosts (python3) ii python3-cffi-backend:amd64 1.16.0-2build1 amd64 Foreign Function Interface for Python 3 calling C code - runtime ii python3-chardet 5.2.0+dfsg-1 all Universal Character Encoding Detector (Python3) ii python3-click 8.1.6-2 all Wrapper around optparse for command line utilities - Python 3.x ii python3-colorama 0.4.6-4 all Cross-platform colored terminal text in Python - Python 3.x ii python3-commandnotfound 23.04.0 all Python 3 bindings for command-not-found. ii python3-configobj 5.0.8-3 all simple but powerful config file reader and writer for Python 3 ii python3-cryptography 41.0.7-4ubuntu0.1 amd64 Python library exposing cryptographic recipes and primitives (Python 3) ii python3-cups:amd64 2.0.1-5build6 amd64 Python3 bindings for CUPS ii python3-cupshelpers 1.5.18-1ubuntu9 all Python utility modules around the CUPS printing system ii python3-dateutil 2.8.2-3ubuntu1 all powerful extensions to the standard Python 3 datetime module ii python3-dbus 1.3.2-5build3 amd64 simple interprocess messaging system (Python 3 interface) ii python3-debconf 1.5.86ubuntu1 all interact with debconf from Python 3 ii python3-debian 0.1.49ubuntu2 all Python 3 modules to work with Debian-related data formats ii python3-defer 1.0.6-2.1ubuntu1 all Small framework for asynchronous programming (Python 3) ii python3-dev 3.12.3-0ubuntu2 amd64 header files and a static library for Python (default) ii python3-distro 1.9.0-1 all Linux OS platform information API ii python3-distro-info 1.7build1 all information about distributions' releases (Python 3 module) ii python3-distupgrade 1:24.04.26 all manage release upgrades ii python3-fasteners 0.18-2 all provides useful locks - Python 3.x ii python3-gdbm:amd64 3.12.3-0ubuntu1 amd64 GNU dbm database support for Python 3.x ii python3-gi 3.48.2-1 amd64 Python 3 bindings for gobject-introspection libraries ii python3-httplib2 0.20.4-3 all comprehensive HTTP client library written for Python3 ii python3-ibus-1.0 1.5.29-2 all Intelligent Input Bus - introspection overrides for Python (Python 3) ii python3-idna 3.6-2ubuntu0.1 all Python IDNA2008 (RFC 5891) handling (Python 3) ii python3-jinja2 3.1.2-1ubuntu1.3 all small but fast and easy to use stand-alone template engine ii python3-json-pointer 2.0-0ubuntu1 all resolve JSON pointers - Python 3.x ii python3-jsonpatch 1.32-3 all library to apply JSON patches - Python 3.x ii python3-jsonschema 4.10.3-2ubuntu1 all An(other) implementation of JSON Schema (Draft 3, 4, 6, 7) ii python3-jwt 2.7.0-1 all Python 3 implementation of JSON Web Token ii python3-launchpadlib 1.11.0-6 all Launchpad web services client library (Python 3) ii python3-lazr.restfulclient 0.14.6-1 all client for lazr.restful-based web services (Python 3) ii python3-lazr.uri 1.0.6-3 all library for parsing, manipulating, and generating URIs ii python3-louis 3.29.0-1build1 all Python bindings for liblouis ii python3-mako 1.3.2-1 all fast and lightweight templating for the Python 3 platform ii python3-markdown-it 3.0.0-2 all Python port of markdown-it and some its associated plugins ii python3-markupsafe 2.1.5-1build2 amd64 HTML/XHTML/XML string library ii python3-mdurl 0.1.2-1 all Python port of the JavaScript mdurl package rF python3-minimal 3.12.3-0ubuntu2 amd64 minimal subset of the Python language (default python3 version) ii python3-monotonic 1.6-2 all implementation of time.monotonic() - Python 3.x ii python3-nacl 1.5.0-4build1 amd64 Python bindings to libsodium (Python 3) ii python3-netaddr 0.8.0-2ubuntu1 all manipulation of various common network address notations (Python 3) ii python3-netifaces:amd64 0.11.0-2build3 amd64 portable network interface information - Python 3.x ii python3-netplan 1.1.2-2~ubuntu24.04.1 amd64 Declarative network configuration Python bindings ii python3-oauthlib 3.2.2-1 all generic, spec-compliant implementation of OAuth for Python3 ii python3-olefile 0.46-3 all Python module to read/write MS OLE2 files ii python3-paramiko 2.12.0-2ubuntu4.1 all Make ssh v2 connections (Python 3) ii python3-pexpect 4.9-2 all Python 3 module for automating interactive applications ii python3-pil:amd64 10.2.0-1ubuntu1 amd64 Python Imaging Library (Python3) ii python3-pip 24.0+dfsg-1ubuntu1.2 all Python package installer ii python3-pip-whl 24.0+dfsg-1ubuntu1.2 all Python package installer (pip wheel) ii python3-pkg-resources 68.1.2-2ubuntu1.2 all Package Discovery and Resource Access using pkg_resources ii python3-problem-report 2.28.1-0ubuntu3.7 all Python 3 library to handle problem reports ii python3-ptyprocess 0.7.0-5 all Run a subprocess in a pseudo terminal from Python 3 ii python3-pygments 2.17.2+dfsg-1 all syntax highlighting package written in Python 3 ii python3-pyparsing 3.1.1-1 all alternative to creating and executing simple grammars - Python 3.x ii python3-pyrsistent:amd64 0.20.0-1build2 amd64 persistent/functional/immutable data structures for Python ii python3-requests 2.31.0+dfsg-1ubuntu1.1 all elegant and simple HTTP library for Python3, built for human beings ii python3-rich 13.7.1-1 all render rich text, tables, progress bars, syntax highlighting, markdown and more ii python3-serial 3.5-2 all pyserial - module encapsulating access for the serial port ii python3-setuptools 68.1.2-2ubuntu1.2 all Python3 Distutils Enhancements ii python3-setuptools-whl 68.1.2-2ubuntu1.2 all Python Distutils Enhancements (wheel package) ii python3-six 1.16.0-4 all Python 2 and 3 compatibility library ii python3-software-properties 0.99.49.2 all manage the repositories that you install software from ii python3-speechd 0.12.0~rc2-2build3 all Python interface to Speech Dispatcher ii python3-sss 2.9.4-1.1ubuntu6.2 amd64 Python3 module for the System Security Services Daemon ii python3-systemd 235-1build4 amd64 Python 3 bindings for systemd ii python3-typing-extensions 4.10.0-1 all Backported and Experimental Type Hints for Python ii python3-tz 2024.1-2 all Python3 version of the Olson timezone database ii python3-uno 4:24.2.7-0ubuntu0.24.04.4 amd64 Python-UNO bridge ii python3-update-manager 1:24.04.12 all Python 3.x module for update-manager ii python3-urllib3 2.0.7-1ubuntu0.2 all HTTP library with thread-safe connection pooling for Python3 ii python3-wadllib 1.3.6-5 all Python 3 library for navigating WADL files ii python3-wheel 0.42.0-2 all built-package format for Python ii python3-xdg 0.28-2 all Python 3 library to access freedesktop.org standards ii python3-xkit 0.5.0ubuntu6 all library for the manipulation of xorg.conf files (Python 3) ii python3-yaml 6.0.1-2build2 amd64 YAML parser and emitter for Python3 ii python3.10-minimal 3.10.4-3 amd64 Minimal subset of the Python language (version 3.10) ii python3.12 3.12.3-1ubuntu0.7 amd64 Interactive high-level object-oriented language (version 3.12) ii python3.12-dev 3.12.3-1ubuntu0.7 amd64 Header files and a static library for Python (v3.12) ii python3.12-minimal 3.12.3-1ubuntu0.7 amd64 Minimal subset of the Python language (version 3.12) ii python3.12-venv 3.12.3-1ubuntu0.7 amd64 Interactive high-level object-oriented language (pyvenv binary, version 3.12)
07-09
<think>我们正在处理一个dpkg错误,具体是在处理ubuntu-drivers-common包时,预删除脚本(prerm)和安装后脚本(postinst)中分别调用了`py3clean`和`py3compile`,但系统找不到这两个命令,导致错误退出状态127(命令未找到)。 根据引用[2]和引用[3],这个问题通常是因为`python3-minimal`包没有正确安装或损坏,导致`py3clean`和`py3compile`这两个脚本不可用。这两个工具由`python3-minimal`提供,用于清理和编译Python3的字节码缓存。 解决步骤: 1. **尝试重新安装`python3-minimal`包**:这是最直接的解决方法,因为`py3clean`和`py3compile`由这个包提供。使用命令: ```bash sudo apt install --reinstall python3-minimal ``` 如果这个命令执行过程中遇到依赖问题,可以尝试先修复损坏的包: ```bash sudo apt --fix-broken install ``` 2. **如果上述步骤不能解决问题,尝试重新安装`python3`和`python3-apt`**:因为错误信息中涉及到`python3-apt`(引用[1])以及`ubuntu-drivers-common`可能依赖的Python环境,我们可以扩展重新安装的包范围: ```bash sudo apt install --reinstall python3 python3-minimal python3-apt ``` 3. **清理并重新配置dpkg状态**:如果重新安装Python相关包后问题仍然存在,可以尝试清理dpkg的缓存和状态文件,然后重新配置: ```bash sudo dpkg --configure -a sudo apt update sudo apt upgrade ``` 4. **手动创建符号链接(备选)**:在极少数情况下,可能`py3clean`和`py3compile`的符号链接丢失。我们可以检查它们是否存在,并手动创建: ```bash # 检查文件是否存在 ls /usr/share/python3/py3clean.py ls /usr/share/python3/py3compile.py # 如果存在,创建符号链接到/usr/bin sudo ln -s /usr/share/python3/py3clean.py /usr/bin/py3clean sudo ln -s /usr/share/python3/py3compile.py /usr/bin/py3compile ``` 注意:在Ubuntu 22.04中,这两个工具通常位于`/usr/bin/`下,由`python3-minimal`包安装。如果重新安装包后它们应该被恢复,所以这一步通常不需要。 5. **最后,再次尝试安装有问题的包**: ```bash sudo apt install ubuntu-drivers-common ``` 注意:在执行上述操作时,确保系统是最新的,运行`sudo apt update`更新软件包列表。 引用[3]提供了直接重新安装`python3-minimal`的解决方案,这也是我们第一步推荐的操作。引用[2]解释了错误的原因,即prerm和postinst脚本调用了找不到的命令。 根据这些信息,我们给出以下具体步骤: ### 解决步骤详解: **步骤1:修复损坏的包(如果存在)** ```bash sudo apt --fix-broken install ``` **步骤2:重新安装python3-minimal** ```bash sudo apt install --reinstall python3-minimal ``` **步骤3:重新安装相关的Python包** ```bash sudo apt install --reinstall python3 python3-apt ``` **步骤4:清理dpkg的状态并更新** ```bash sudo dpkg --configure -a sudo apt update sudo apt upgrade ``` **步骤5:再次尝试安装或升级包** ```bash sudo apt install ubuntu-drivers-common ``` 如果上述步骤成功,问题应该被解决。 ### 注意事项: - 在执行这些命令时,可能需要管理员权限(sudo)。 - 如果系统中有多个Python版本,请确保默认的Python3指向正确的版本(通常通过`update-alternatives`配置)。 ### 相关问题: 1. 如何修复Ubuntu系统中其他与Python相关的dpkg错误? 2. 在修复Python环境时,如何避免破坏系统依赖? 3. 如果`apt --fix-broken install`失败,有哪些替代方法可以恢复系统包管理?</think>### 解决 dpkg 错误:py3clean 和 py3compile 未找到 #### 问题分析 错误信息显示在安装 `ubuntu-drivers-common` 包时,系统无法找到 `py3clean` 和 `py3compile` 命令: ```bash /var/lib/dpkg/info/ubuntu-drivers-common.prerm: 6: py3clean: not found ... /var/lib/dpkg/info/ubuntu-drivers-common.postinst: 23: py3compile: not found ``` 这些关键工具由 `python3-minimal` 包提供[^2][^3],主要用于: 1. `py3clean`:清理 Python 字节码缓存(`.pyc` 文件) 2. `py3compile`:编译 Python 字节码缓存 它们的缺失表明 Python 环境损坏或关键组件未正确安装。 --- ### 修复步骤 #### 1. 重新安装 Python 核心组件 ```bash sudo apt install --reinstall python3-minimal python3 python3-apt ``` 此命令将恢复 `py3clean` 和 `py3compile` 工具(位于 `/usr/bin/`)[^3]。 #### 2. 修复损坏的依赖关系 ```bash sudo apt --fix-broken install ``` 此操作会自动检测并修复包依赖问题[^1]。 #### 3. 强制完成中断的安装 ```bash sudo dpkg --configure -a sudo apt update sudo apt upgrade ``` #### 4. 手动验证工具恢复 检查工具是否已存在: ```bash ls /usr/bin/py3clean ls /usr/bin/py3compile ``` 正常应显示: ``` /usr/bin/py3clean /usr/bin/py3compile ``` #### 5. 重新尝试安装问题包 ```bash sudo apt install --reinstall ubuntu-drivers-common ``` --- ### 预防措施 1. **避免手动删除 Python 文件** 系统级 Python 组件(如 `python3-minimal`)由 APT 管理,手动删除会导致依赖链断裂。 2. **谨慎使用第三方 Python 安装** 若使用 Anaconda 或手动编译 Python(如引用[4]的 Python 3.7.4),避免替换系统默认的 `/usr/bin/python3`。 3. **关键组件保护列表** 以下包不应强制卸载: ```bash python3-minimal # 提供 py3clean/py3compile python3-apt # APT 的 Python 接口 python3-distutils # Python 工具链基础 ``` --- ### 技术说明 当执行 `sudo apt install package` 时: 1. **pre-removal 脚本** (`prerm`) 调用 `py3clean` 清理旧版本 2. **post-install 脚本** (`postinst`) 调用 `py3compile` 编译新版本 3. 若这些工具缺失,dpkg 会返回错误码 127(命令未找到)[^2] 修复后验证 Python 环境: ```bash $ python3 -c "import apt; print(apt.__version__)" # 应输出类似 2.6.0 的版本号 ``` ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值