JDK 构建时 configure 找不到外部依赖库怎么用 --with-freetype、--with-x 等参数指定路径
【免费下载链接】jdkJDK main-line development https://openjdk.org/projects/jdk项目地址: https://gitcode.com/GitHub_Trending/jd/jdk
在 Linux 上从头构建 JDK 时,bash configure是第一个可能卡住的环节:如果 FreeType、X11、CUPS 这类外部依赖没有被自动找到,configure会直接退出并报Could not find ...错误。这篇文章给出 doc/building.md 中"External Library Requirements"和"Running Configure"两节描述的解决路径:用--with-<LIB>=<path>或--with-<LIB>-include/--with-<LIB>-lib形式的参数把依赖库的实际位置指给configure,重新运行后让配置步骤通过,继续执行make。
configure 自动探测失败时的两种指定方式
configure会先尝试自动定位各组件,全部找到时不需要任何额外参数;某个组件没被自动找到时它会退出并说明问题。文档建议:失败时多数情况下它会打印如何在当前平台解决的建议,按提示处理后重新运行bash configure即可。
需要手动指定路径时,doc/building.md 给出了两种等价的写法:
bash configure --with-<LIB>=<path>这种写法更简洁,但要求头文件和库文件都位于该目录下的默认层级结构中。如果依赖不是标准安装布局,用第二种形式分别指向 include 目录和 lib 目录:
bash configure --with-<LIB>-include=<path to include> --with-<LIB>-lib=<path to lib>下面以 FreeType 和 X11 两个最常见的缺失依赖为例说明具体参数。
指定 FreeType 路径:--with-freetype-include 与 --with-freetype-lib
在 FreeType 没有被自动定位时,使用:
bash configure --with-freetype-include=<include 目录> --with-freetype-lib=<lib 目录>两个参数的取值对象(依据 make/autoconf/lib-freetype.m4 中的检查逻辑):
--with-freetype-include:指向直接包含ft2build.h的目录,或者包含freetype2子目录(其内有ft2build.h)的目录;--with-freetype-lib:指向包含 freetype 共享库文件(libfreetype加上平台后缀)的目录。
例如 FreeType 被安装在/opt/freetype(下文中所有示例路径都只是占位,请替换为你机器上 FreeType 的实际位置):
bash configure \ --with-freetype-include=/opt/freetype/include \ --with-freetype-lib=/opt/freetype/lib使用这两个参数时有三条硬性规则,违反时configure会直接报错:
- 必须同时给或同时不给:只指定其中一个会报
Must specify both or neither of --with-freetype-include and --with-freetype-lib; - 与 bundled 互斥:
--with-freetype=bundled不能再搭配--with-freetype-include或--with-freetype-lib; - 指定路径后会被校验:路径下找不到可用的头文件或库时报
Can not find or use freetype at location given by --with-freetype-lib|include。
可选分支:改用 bundled FreeType。如果本机没有可用的系统 FreeType,在 Linux 上也可以改为使用 JDK 仓库自带的副本:bash configure --with-freetype=bundled。doc/building.md 说明 FreeType 在多数平台并非必需,唯一例外是 Unix 平台配置为引用系统安装的库时;按 make/autoconf/lib-freetype.m4 的定义,Linux 默认使用 system,Windows 和 macOS 则只能使用 bundled(在 Mac/Windows 上要求 system 会报Only bundled freetype can be specified on Mac and Windows)。
注意一个文档内部的冲突:doc/building.md 的参数列表和示例(如bash configure --with-freetype=/cygdrive/c/freetype-i586 --with-target-bits=32)把--with-freetype=<path>描述为直接指向 FreeType 目录;但 make/autoconf/lib-freetype.m4 中该参数只接受system和bundled两个取值,传入其他值会报Valid values for --with-freetype are 'system' and 'bundled'。两处说法不一致,因此本文推荐用--with-freetype-include/--with-freetype-lib指定路径;运行前可用bash configure --help查看当前 configure 实际接受的参数定义。
指定 X11 路径:--with-x
某些 X11 库和头文件在 Linux 上是必需的,configure未正确定位时用--with-x=<path>:
bash configure --with-x=/opt/x11依据 make/autoconf/lib-x11.m4,<path>是一个基础目录,configure默认在其下查找:
- 头文件:
<path>/include - 库文件:
<path>/lib
如果头文件和库不在这样的标准子目录下,可以显式覆盖这两个位置:
bash configure \ --with-x=/opt/x11 \ --x-includes=/opt/x11/include \ --x-libraries=/opt/x11/lib一个边界情况:构建 headless JDK 时不需要 X11,此时传入--with-x只会被忽略并打印警告X11 is not used, so --with-x is ignored,不会报错。
其他常用的依赖路径参数
同一节文档还列出了这些外部依赖的指定参数,用法与上面一致(路径均为示例占位,替换为你的实际位置):
| 依赖 | 指定参数 |
|---|---|
| CUPS(除 Windows 外必需) | --with-cups=<path> |
| ALSA(Linux 上必需,含 headless 构建) | --with-alsa=<path> |
| libffi(构建 Hotspot 的 Zero 变体时必需) | --with-libffi=<path> |
| X11(Linux 上必需) | --with-x=<path> |
多个依赖同时缺失时,可以在同一条命令中组合指定,例如:
bash configure \ --with-freetype-include=/opt/freetype/include \ --with-freetype-lib=/opt/freetype/lib \ --with-x=/opt/x11 \ --with-cups=/opt/cups如果缺失的依赖只是没装开发包,文档对每个库都给出了对应的安装命令(如 apt 系的sudo apt-get install libfreetype6-dev、X11 的libx11-dev libxext-dev libxrender-dev libxrandr-dev libxtst-dev libxt-dev),安装后直接重跑bash configure,不需要任何--with-*参数。
验证配置是否通过
configure成功时,会在源码目录下创建一个构建树目录(文档中的典型名称形如build/linux-x64-server-release),并在输出中打印各项检测结果。针对本文的两个依赖,可以对照检查:
- FreeType 被指定路径命中时会打印类似
Found freetype include files at <目录> using --with-freetype,最后输出Using freetype: system(或bundled); - 没有再出现
Could not find ...错误、configure正常结束且生成了构建树目录,即表示配置通过。
随后按常规流程构建并验证产物:
make images ./build/*/images/jdk/bin/java -version如果configure仍然失败,对照以下文档中真实存在的错误信息定位(均来自 make/autoconf/lib-freetype.m4 和 make/autoconf/lib-x11.m4):
| 错误信息 | 含义 |
|---|---|
Must specify both or neither of --with-freetype-include and --with-freetype-lib | 两个 freetype 路径参数只给了一个 |
Can not find or use freetype at location given by --with-freetype-lib|include | 指定位置下找不到ft2build.h或 freetype 共享库 |
Could not find freetype! | 自动探测与指定路径都失败 |
Could not find X11 libraries. | X11 库文件未被找到 |
Could not find all X11 headers (shape.h Xrender.h Xrandr.h XTest.h Intrinsic.h). | X11 头文件不齐 |
Only bundled freetype can be specified on Mac and Windows | 在 Mac/Windows 上要求了 system FreeType |
两个容易踩的边界
- 交叉编译时指向的库必须属于目标平台。文档明确提醒:用
--with-alsa、--with-x等指向的库要是target平台的,不是build平台的,并且不要误覆盖构建机自身的同名库。如果通过--with-sysroot提供了 sysroot,FreeType 的自动探测会跳过 pkg-config,改到 sysroot 下的标准位置查找。 - 重新运行即可,无需清理。指定路径后直接重跑
bash configure;如果之前已经生成过配置而行为异常,可参考文档中"Running make doctor"一节的诊断流程,但针对依赖路径问题,通常修正参数重跑configure就能通过。
【免费下载链接】jdkJDK main-line development https://openjdk.org/projects/jdk项目地址: https://gitcode.com/GitHub_Trending/jd/jdk
创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考