Apache APR 配置参数详解
Apache APR 配置参数详解:APR 为编译期配置的 C 库,无运行时配置文件,本文分类详解 apr-1.7.6 与 apr-util-1.6.5 的 configure 编译选项,逐项实测 apr-1-config/apu-1-config 输出参数与 pkg-config 用法,梳理 /usr/local/apr 安装产物布局与静态、动态链接选择,附常见问题,全程实测。
Apache APR 配置参数详解
目标:详解 Apache APR 的两类「配置参数」——编译期的
./configure选项(决定库的能力与安装布局)与开发期的 apr-1-config / apu-1-config 输出参数(决定你的工程如何编译链接 APR)。APR 为编译期配置的 C 库,安装后没有运行时配置文件,改能力只能重新 configure 编译。本文按类别逐项解释 apr-1.7.6 与 apr-util-1.6.5 的 configure 选项、逐项给出配置工具的真实输出,并梳理安装产物布局与静态/动态链接选择。所有命令与回显均在 Ubuntu 22.04.5 实测。
一、环境信息
- 操作系统:Ubuntu 22.04.5 LTS(VERSION_CODENAME=jammy)
- 架构:x86_64(内核 6.8.0-138-generic)
- 编译工具链:gcc 11.4.0、GNU Make 4.3、GNU libtool 2.4.6、pkg-config 0.29.2
- APR 版本:apr-1.7.6 + apr-util-1.6.5(安装前缀
/usr/local/apr) - 依赖库:libexpat1-dev 2.4.7-1ubuntu0.9(apr-util XML 支持)
- 演示账号:root
二、前置条件
已按《Apache APR 安装指南》完成两个组件的源码编译安装。验证配置工具可用:
/usr/local/apr/bin/apr-1-config --version
/usr/local/apr/bin/apu-1-config --version
实测回显:
1.7.6
1.6.5
💡 说明:APR 没有
/etc/apr.conf之类的运行时配置文件;「配置」发生在两个时刻——安装前用 configure 决定库形态与能力,开发时用配置工具把编译参数喂给 gcc。改任何能力(如开关 IPv6、启用 crypto)都意味着重新 configure、make、make install。
三、参数查看与修改方式
| 层面 | 查看方式 | 修改方式 |
|---|---|---|
| configure 编译选项 | 源码目录执行 ./configure --help | 重新 configure + make + make install |
| 编译/链接参数 | apr-1-config --help、apu-1-config --help | 由安装结果决定,只读 |
| pkg-config 元数据 | pkg-config --list-all(配 PKG_CONFIG_PATH) | 只读(随 .pc 文件生成) |
| 安装布局 | ls /usr/local/apr | configure --prefix 等路径类选项 |
查看两个组件全部 configure 选项:
cd /tmp/apr-1.7.6 && ./configure --help
cd /tmp/apr-util-1.6.5 && ./configure --help
查看配置工具全部参数:
/usr/local/apr/bin/apr-1-config --help
/usr/local/apr/bin/apu-1-config --help
四、APR(apr-1.7.6)configure 选项分类详解
以下选项均摘自 ./configure --help 实测输出,按用途分类。最常用的组合形式:
./configure --prefix=/usr/local/apr
4.1 安装路径与布局
| 选项 | 说明 |
|---|---|
--prefix=PREFIX | 安装前缀,本文实测取 /usr/local/apr,决定 bin/include/lib 的落点 |
--enable-layout=LAYOUT | 启用预定义目录布局(Apache 风格布局等) |
--with-installbuilddir=DIR | 指定 APR build 辅助文件目录(默认随 prefix 为 build-1) |
4.2 库形态与调试选项
| 选项 | 默认 | 说明 |
|---|---|---|
--enable-shared[=PKGS] | yes | 构建共享库(libapr-1.so) |
--enable-static[=PKGS] | yes | 构建静态库(libapr-1.a) |
--enable-debug | 关 | 打开调试与编译期告警 |
--enable-maintainer-mode | 关 | 打开调试与编译期告警(维护者模式) |
--enable-profile | 关 | 为构建启用 GCC 性能剖析 |
--enable-pool-debug[=yes|no|verbose|verbose-alloc|lifetime|owner|all] | 关 | 打开内存池调试(排查池误用) |
实测默认配置下 lib/ 同时产出 .a 与 .so(见「八、安装产物布局」),即 shared 与 static 默认全开。
4.3 平台能力开关
| 选项 | 默认 | 说明 |
|---|---|---|
--enable-threads | 开 | 启用 APR 线程支持 |
--disable-ipv6 | 不禁用 | 禁用则去掉 IPv6 支持;本文实测探测结果为 checking if APR supports IPv6... yes |
--disable-dso | 不禁用 | 禁用动态加载(apr_dso)支持;apr-util configure 会探测 whether APR has DSO support... yes |
--disable-lfs | 不禁用 | 32 位平台上关闭大文件支持(64 位平台无实际影响) |
--enable-nonportable-atomics | 关 | 使用优化的原子操作(产物可能不可移植) |
--enable-posix-shm | 关 | 优先使用 POSIX 共享内存(shm_open) |
--enable-sysv-shm | 关 | 优先使用 SysV 共享内存(shmget) |
--disable-sctp | 不禁用 | 禁用 SCTP 协议支持 |
--disable-timedlocks | 不禁用 | 禁用带超时的锁 |
--enable-other-child | 关 | 启用可靠子进程(other child)机制 |
configure 阶段与本分类相关的实测探测回显:
Checking for Threads...
Checking for IPv6 Networking support...
checking if APR supports IPv6... yes
线程支持落实为编译宏 -D_REENTRANT(见「六、apr-1-config 参数逐项详解」的 --cppflags 输出)。
4.4 内存分配器选项
| 选项 | 说明 |
|---|---|
--enable-allocator-uses-mmap | apr_allocator 改用 mmap 而非 malloc 申请内存块 |
--enable-allocator-guard-pages | 分配器加保护页(越界写立即崩溃,便于定位) |
--enable-pool-concurrency-check | 检查内存池的并发误用 |
4.5 工具链与其他选项
| 选项 | 说明 |
|---|---|
--without-libtool | 避免用 libtool 链接库 |
--with-efence[=DIR] | 链接 Electric Fence 内存调试库 |
--with-valgrind[=DIR] | 让 APR 内存池向 valgrind 报告内存状态(memcheck 更准) |
--with-sendfile | 强制启用/关闭 sendfile 优化 |
--with-egd[=DIR] | 使用 EGD 兼容套接字作为熵源 |
--with-devrandom[=DEV] | 指定随机数设备(默认自动搜索 /dev/random 类设备) |
五、APR-util(apr-util-1.6.5)configure 选项分类详解
apr-util 的 configure 选项围绕「为 APR 补充能力」展开:关联 APR、加密、LDAP、DBM/DBD 数据库、XML 与编码。最常用组合:
./configure --prefix=/usr/local/apr --with-apr=/usr/local/apr/bin/apr-1-config
5.1 关联 APR 与模块形态
| 选项 | 说明 |
|---|---|
--with-apr=PATH | 指向已安装 APR 的前缀或 apr-1-config 全路径;不带该参数且 PATH 中找不到 apr-1-config 时直接报错(见「十、常见问题」) |
--disable-util-dso | 禁用 crypto/dbd/dbm/ldap 等模块化组件的 DSO(动态加载)构建,改为编进主库 |
--with-apr-iconv=DIR | 指定 apr-iconv 源码相对路径(apr-iconv 为独立组件,本文未安装) |
实测探测回显:
checking for APR... yes
checking whether APR has DSO support... yes
5.2 加密支持
| 选项 | 说明 |
|---|---|
--with-crypto | 启用加密框架(apr_crypto)总开关 |
--with-openssl=DIR | 指定 OpenSSL 位置(crypto 驱动之一) |
--with-nss=DIR | 指定 NSS 位置(crypto 驱动之一) |
--with-commoncrypto=DIR | 指定 CommonCrypto 位置(macOS 平台) |
💡 说明:本文实测未启用 crypto,
apu-1-config --libs输出仅-lexpat;启用后链接参数会追加对应加密库。
5.3 LDAP 支持
| 选项 | 说明 |
|---|---|
--with-ldap=library | 指定使用的 ldap 库 |
--with-lber=library | 指定 lber 库 |
--with-ldap-include=path | ldap 头文件路径(结尾带斜杠) |
--with-ldap-lib=path | ldap 库文件路径 |
5.4 DBM 数据库(键值文件)
| 选项 | 说明 |
|---|---|
--with-dbm=DBM | 选择默认 DBM 类型 |
--with-gdbm=DIR | 启用 GDBM 支持 |
--with-ndbm=PATH | 在 PATH/include 下查找 NDBM |
--with-berkeley-db=PATH | 查找 Berkeley DB 头文件与库 |
未启用任何 DBM 时 apu-1-config --db-version 输出 0(实测)。
5.5 DBD 数据库驱动(SQL 后端)
| 选项 | 说明 |
|---|---|
--with-pgsql=DIR | 指定 PostgreSQL 位置,启用 DBD pgsql 驱动 |
--with-mysql=DIR | 启用 MySQL DBD 驱动 |
--with-sqlite3=DIR | 启用 sqlite3 DBD 驱动 |
--with-sqlite2=DIR | 启用 sqlite2 DBD 驱动 |
--with-oracle=DIR | 启用 Oracle DBD 驱动(DIR 传 ORACLE_HOME) |
--with-oracle-include=DIR | 单独指定 Oracle 头文件目录 |
--with-odbc=DIR | 指定 ODBC 位置,启用 DBD odbc 驱动 |
5.6 XML 与编码
| 选项 | 说明 |
|---|---|
--with-expat=DIR | 指定 Expat 位置;apr-util 的 XML 解析(apr_xml)依赖 expat,本文通过系统包 libexpat1-dev 2.4.7-1ubuntu0.9 提供 |
--with-iconv=DIR | 指定 iconv 安装位置(apr_iconv 编码转换) |
六、apr-1-config 参数逐项详解
apr-1-config 是编译期参数的权威来源:编译命令里的 gcc 参数不手工拼写,全部由它查询。完整帮助输出(实测):
Usage: apr-1-config [OPTION]
Known values for OPTION are:
--prefix[=DIR] change prefix to DIR
--bindir print location where binaries are installed
--includedir print location where headers are installed
--cc print C compiler name
--cpp print C preprocessor name and any required options
--cflags print C compiler flags
--cppflags print C preprocessor flags
--includes print include information
--ldflags print linker flags
--libs print additional libraries to link against
--srcdir print APR source directory
--installbuilddir print APR build helper directory
--link-ld print link switch(es) for linking to APR
--link-libtool print the libtool inputs for linking to APR
--shlib-path-var print the name of the shared library path env var
--apr-la-file print the path to the .la file, if available
--apr-so-ext print the extensions of shared objects on this platform
--apr-lib-target print the libtool target information
--apr-libtool print the path to APR's libtool
--version print the APR's version as a dotted triple
--help print this help
When linking with libtool, an application should do something like:
APR_LIBS="`apr-1-config --link-libtool --libs`"
or when linking directly:
APR_LIBS="`apr-1-config --link-ld --libs`"
An application should use the results of --cflags, --cppflags, --includes,
and --ldflags in their build process.
逐参数真实输出与本机取值:
| 参数 | 实测输出 | 说明 |
|---|---|---|
--version | 1.7.6 | APR 版本(点分三元组) |
--cc | gcc | configure 阶段选定的 C 编译器 |
--cpp | gcc -E | C 预处理器及所需选项 |
--cflags | -g -O2 | 编译器优化/调试参数 |
--cppflags | -DLINUX -D_REENTRANT -D_GNU_SOURCE | 预处理宏(平台识别 + 线程安全) |
--includes | -I/usr/local/apr/include/apr-1 | 头文件搜索路径 |
--includedir | /usr/local/apr/include/apr-1 | 头文件目录本身 |
--ldflags | (空) | 无额外链接标志 |
--libs | -lcrypt -lpthread | 链接 APR 时需追加的系统库 |
--link-ld | -L/usr/local/apr/lib -lapr-1 | 直接链接参数(配合 gcc 使用) |
--link-libtool | /usr/local/apr/lib/libapr-1.la | libtool 链接输入(配合 libtool 使用) |
--prefix | /usr/local/apr | 安装前缀 |
--bindir | /usr/local/apr/bin | 可执行文件目录 |
--installbuilddir | /usr/local/apr/build-1 | build 辅助目录 |
--srcdir | /tmp/apr-1.7.6 | configure 时的源码目录 |
--shlib-path-var | LD_LIBRARY_PATH | 本平台共享库路径环境变量名 |
--apr-la-file | /usr/local/apr/lib/libapr-1.la | libtool 库描述文件路径 |
--apr-so-ext | lo | 本平台共享对象扩展名(libtool 对象扩展,非 so) |
--apr-lib-target | -rpath $(libdir) $(OBJECTS) | libtool 目标描述 |
💡 说明:
--cflags --cppflags --includes三者构成完整编译参数;--link-ld --libs构成完整链接参数。最简编译模板:gcc app.c -o app $(/usr/local/apr/bin/apr-1-config --cflags --cppflags --includes --link-ld --libs)
七、apu-1-config 参数逐项详解
apu-1-config 服务于 apr-util(UUID、DBD、DBM、XML 等 API)。完整帮助输出(实测):
Usage: apu-1-config [OPTION]
Known values for OPTION are:
--prefix[=DIR] change prefix to DIR
--bindir print location where binaries are installed
--includes print include information
--includedir print location where headers are installed
--ldflags print linker flags
--libs print library information
--avoid-ldap do not include ldap library information with --libs
--ldap-libs print library information to link with ldap
--avoid-dbm do not include DBM library information with --libs
--dbm-libs print additional library information to link with DBM
--srcdir print APR-util source directory
--link-ld print link switch(es) for linking to APR-util
--link-libtool print the libtool inputs for linking to APR-util
--apu-la-file print the path to the .la file, if available
--old-expat indicate if APR-util was built against an old expat
--db-version print the DB version
--version print APR-util's version as a dotted triple
--help print this help
When linking with libtool, an application should do something like:
APU_LIBS="`apu-1-config --link-libtool --libs`"
or when linking directly:
APU_LIBS="`apu-1-config --link-ld --libs`"
An application should use the results of --includes, and --ldflags in
their build process.
逐参数真实输出与本机取值:
| 参数 | 实测输出 | 说明 |
|---|---|---|
--version | 1.6.5 | apr-util 版本 |
--includes | -I/usr/local/apr/include/apr-1 | 头文件路径(apr-util 头文件与 APR 同目录) |
--includedir | /usr/local/apr/include/apr-1 | 头文件目录本身 |
--libs | -lexpat | 链接 apr-util 需追加的库(XML 依赖) |
--link-ld | -L/usr/local/apr/lib -laprutil-1 | 直接链接参数 |
--link-libtool | /usr/local/apr/lib/libaprutil-1.la | libtool 链接输入 |
--prefix | /usr/local/apr | 安装前缀(与 APR 共用) |
--bindir | /usr/local/apr/bin | 可执行文件目录 |
--db-version | 0 | DB 版本(未启用任何 DBM) |
--old-expat | @APR_XML_EXPAT_OLD@ | 输出为未替换的构建模板值,表示本机未走旧 expat 兼容路径(apr-util 1.6.5 默认对接系统 expat) |
💡 说明:
--avoid-ldap/--avoid-dbm用于在启用过对应能力时,让--libs输出剔除 ldap/DBM 库信息;本文未启用 ldap 与 DBM,故--libs只有-lexpat。
八、/usr/local/apr 安装产物布局
# ls /usr/local/apr
bin
build-1
include
lib
# ls /usr/local/apr/bin
apr-1-config
apu-1-config
# ls /usr/local/apr/lib
apr.exp
aprutil.exp
libapr-1.a
libapr-1.la
libapr-1.so
libapr-1.so.0
libapr-1.so.0.7.6
libaprutil-1.a
libaprutil-1.la
libaprutil-1.so
libaprutil-1.so.0
libaprutil-1.so.0.6.5
pkgconfig
# ls /usr/local/apr/build-1
apr_rules.mk
libtool
make_exports.awk
make_var_export.awk
mkdir.sh
# ls /usr/local/apr/lib/pkgconfig
apr-1.pc
apr-util-1.pc
# ls /usr/local/apr/include/apr-1 | head -12
apr.h
apr_allocator.h
apr_anylock.h
apr_atomic.h
apr_base64.h
apr_buckets.h
apr_crypto.h
apr_cstr.h
apr_date.h
apr_dbd.h
apr_dbm.h
apr_dso.h
头文件目录 include/apr-1 共 76 个头文件。布局与参数对应关系:
| 路径 | 产生方式 | 对应查询参数 |
|---|---|---|
/usr/local/apr/bin/ | --prefix + 默认布局 | apr-1-config --bindir |
/usr/local/apr/include/apr-1/ | 默认布局 | apr-1-config --includedir |
/usr/local/apr/lib/ | 默认布局 | --link-ld 的 -L 部分 |
/usr/local/apr/build-1/ | --with-installbuilddir 可改 | apr-1-config --installbuilddir |
/usr/local/apr/lib/pkgconfig/ | 随安装生成 | PKG_CONFIG_PATH 需指向此目录 |
pkg-config 交叉验证(实测):
export PKG_CONFIG_PATH=/usr/local/apr/lib/pkgconfig
pkg-config --modversion apr-1
pkg-config --modversion apr-util-1
pkg-config --cflags --libs apr-util-1
# pkg-config --modversion apr-1
1.7.6
# pkg-config --modversion apr-util-1
1.6.5
# pkg-config --cflags --libs apr-util-1
-DLINUX -D_REENTRANT -D_GNU_SOURCE -g -O2 -I/usr/local/apr/include/apr-1 -L/usr/local/apr/lib -laprutil-1 -lexpat -lapr-1 -lcrypt -lpthread
九、静态链接与动态链接选择
configure 默认同时产出动态库(libapr-1.so.0.7.6)与静态库(libapr-1.a),两种链接方式实测如下。
9.1 动态链接(默认推荐)
链接参数取 --link-ld --libs,产物运行时依赖 libapr-1.so.0:
gcc demo_pool.c -o demo_pool $(/usr/local/apr/bin/apr-1-config --cflags --cppflags --includes --link-ld --libs)
前提是 /usr/local/apr/lib 已注册进 ld.so 缓存(/etc/ld.so.conf.d/apr.conf + ldconfig),否则运行报 error while loading shared libraries: libapr-1.so.0(排障实录见《Apache APR 构建并运行示例应用指南》)。
优点:可执行文件小;APR 升级(替换 .so)后无需重编应用。适用绝大多数场景,包括与 Apache httpd 同机共享一份 APR。
9.2 静态链接(免运行期依赖)
直接把 libapr-1.a 链进可执行文件(头文件路径与系统库手工给出):
cd /root/apr-demo
gcc demo_pool.c -o demo_pool_static -g -O2 -DLINUX -D_REENTRANT -D_GNU_SOURCE -I/usr/local/apr/include/apr-1 /usr/local/apr/lib/libapr-1.a -lcrypt -lpthread
./demo_pool_static
ldd demo_pool_static
实测回显:
# ./demo_pool_static
Hello, APR pool!
psprintf demo: 2 + 3 = 5
pstrdup equals msg: yes
apr_palloc(128) OK, pool addr=0x5d9fa328c3b8
# ldd demo_pool_static
linux-vdso.so.1 (0x00007fff46144000)
libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007bc9ae58f000)
/lib64/ld-linux-x86-64.so.2 (0x00007bc9ae7d9000)
静态链接后 ldd 不再出现 libapr-1 依赖,运行不再依赖 ld.so 注册;代价是产物变大、APR 升级后必须重编应用。
9.3 选择建议
| 维度 | 动态链接 | 静态链接 |
|---|---|---|
| 运行期依赖 | 需 ld.so 注册(或 LD_LIBRARY_PATH / rpath) | 无 APR .so 依赖 |
| APR 升级 | 替换 .so 即生效 | 必须重编应用 |
| 产物体积 | 小 | 大 |
| 适用场景 | 常规服务端开发、多程序共享 APR | 单文件分发、免部署环境的工具程序 |
十、常见问题(FAQ)
10.1 apr-util configure 报 APR could not be located
Q: ./configure --prefix=/usr/local/apr 编译 apr-util 时报 configure: error: APR could not be located. Please use the --with-apr option.?
A: --with-apr 是 apr-util 最关键的参数。/usr/local/apr/bin 不在 PATH 时 apr-util 找不到 APR,实测复现:
# ./configure --prefix=/usr/local/apr
Applying apr-util hints file rules for x86_64-pc-linux-gnu
checking for APR... no
configure: error: APR could not be located. Please use the --with-apr option.
(退出码 1)
解决:显式指定 apr-1-config 绝对路径:
./configure --prefix=/usr/local/apr --with-apr=/usr/local/apr/bin/apr-1-config
成功标志是探测行 checking for APR... yes。
10.2 configure 打印 rm: cannot remove 'libtoolT'
Q: APR 的 configure 尾部出现 rm: cannot remove 'libtoolT': No such file or directory,是否需要修复?
A: 不需要。这是 1.7.6 configure 脚本的已知无害提示(rm 变量为空),configure 退出码 0,make / make install 均正常,直接继续即可。老版本 1.7.0 的同类问题需改 configure 文件,1.7.6 实测无需处理。
10.3 --cppflags 里的 -D_REENTRANT 是什么
Q: apr-1-config --cppflags 输出 -DLINUX -D_REENTRANT -D_GNU_SOURCE,各是什么含义?
A: -DLINUX 标识平台(configure 探测结果),-D_REENTRANT 由线程支持探测产生(要求 libc 暴露可重入接口,对应 Checking for Threads... 探测),-D_GNU_SOURCE 启用 GNU 扩展。三者都是 configure 阶段探测生成的编译宏,必须随 --cppflags 原样传给 gcc,缺省可能引发编译告警或行为差异。
10.4 --apr-so-ext 为什么输出 lo 而不是 so
Q: apr-1-config --apr-so-ext 输出 lo,共享库扩展名不是 .so 吗?
A: 该参数返回的是 libtool 的「可重定位共享对象」扩展名(libtool 对象文件 .lo),不是系统共享库扩展名。系统共享库以 ls /usr/local/apr/lib 中的 libapr-1.so.0.7.6 为准,属正常现象,无需处理。
10.5 pkg-config 找不到 apr-1.pc
Q: pkg-config --modversion apr-1 报 Package apr-1 not found?
A: APR 的 .pc 文件装在 /usr/local/apr/lib/pkgconfig,不在 pkg-config 默认搜索路径。先导出:
export PKG_CONFIG_PATH=/usr/local/apr/lib/pkgconfig
之后再执行 pkg-config --modversion apr-1 即输出 1.7.6(实测)。
十一、参考链接
- APR configure 官方说明:https://apr.apache.org/compiling_unix.html
- apr-1-config 在线文档:https://apr.apache.org/docs/apr/1.7/apr__config_8h.html
- APR 官方下载目录:https://dlcdn.apache.org/apr/
- 安装步骤与产物来源:《Apache APR 安装指南》
- 编译参数在示例中的应用:《Apache APR 构建并运行示例应用指南》