《深入理解 Java 虚拟机》第一章 OpenJDK 12 环境搭建 - MacOS 26 版一、环境准备OpenJDK 12 源码下载安装 JDK 11编译时作为 Boot JDK安装 Xcode 和 Command Line Tools安装 Homebrew安装依赖库二、开始编译 JDK三、逐项修复问题 1SDK does not contain libarclite at the path问题 2redeclaration of C built-in type bool问题 3too many arguments to function call,问题 4too many arguments to function call问题 5gif_lib.h - redeclaration of C built-in type bool问题 6fp.h file not found一、环境准备编译环境JDK 12 源码 macOS 26Darwin 26.5.2 新版 Xcode 26.4/clangOpenJDK 12 源码下载浏览器打开地址https://hg.openjdk.org/jdk/jdk12/ macOS 下载 gz 格式安装 JDK 11编译时作为 Boot JDK官网地址https://www.oracle.com/java/technologies/downloads/#java11安装 Xcode 和 Command Line ToolsmacOS 26 无法安装低版本 Xcode。官网地址https://developer.apple.com/download/all/?qxcode安装验证192 JDK12% xcode-select--installxcode-select: note: Command line tools are already installed. UseSoftware UpdateinSystem Settings or the softwareupdatecommandline interface toinstallupdates提示已安装证明安装成功。在终端中执行gcc -v命令查看是否安装成功192 JDK12% gcc-vApple clang version21.0.0(clang-2100.0.123.102)Target: x86_64-apple-darwin25.5.0 Thread model: posix InstalledDir: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin安装 Homebrew官网https://brew.sh//bin/bash-c$(curl-fsSLhttps://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)终端运行如下命令可查看安装的版本信息~192 JDK12% brew-vHomebrew6.0.13安装依赖库打开终端执行如下命令brewinstallccache brewinstallfreetype brewinstallautoconf至此编译环境就准备好了。二、开始编译 JDK首先终端先切到下载的 JDK 目录中下面的命令可以查看编译参数bashconfigure--help好了开始编译编译命令注意 JDK 要换成自己的地址bashconfigure\--enable-debug\--with-jvm-variantsserver\--with-sysroot/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk\--with-boot-jdk/Library/Java/JavaVirtualMachines/jdk-11.jdk/Contents/Home\--disable-warnings-as-errors\--with-extra-cflags-Wno-incompatible-function-pointer-types -Wno-cast-function-type-mismatch -F/Library/Frameworks\--with-extra-cxxflags-stdgnu11 -F/Library/Frameworks输出下面的内容证明这一步没问题。电脑配置显示的不完全一样Tools summary: * Boot JDK:javaversion11.0.322026-07-21 LTS Java(TM)SE Runtime Environment18.9(build11.0.327-LTS-196)Java HotSpot(TM)64-Bit Server VM18.9(build11.0.327-LTS-196, mixed mode)(at /Library/Java/JavaVirtualMachines/jdk-11.jdk/Contents/Home)* Toolchain: clang(clang/LLVM from Xcode26.4)* C Compiler: Version21.0.0(at /usr/bin/clang-stdgnu23)* C Compiler: Version21.0.0(at /usr/bin/clang)Build performance summary: * Cores to use:16* Memory limit:65536MB然后执行编译命令makeimages编译报错可以参考一下第三章。编译遇到问题排查思路根据提示看构建目录底下的报错去修复问题。build/macosx-x86_64-server-fastdebug/make-support/failure-logs然后每次重新编译要注意清理上一次编译的脏数据依次执行makeclean然后执行makedist-clean看到下面的命令就证明编译成功了Finished building targetimagesinconfigurationmacosx-x86_64-server-fastdebug三、逐项修复核心矛盾JDK 12 代码基于 C99 时代写法新版 clang 默认启用 C23 标准bool 变为内置关键字、() 无原型声明被移除导致一系列编译错误。问题 1SDK does not contain ‘libarclite’ at the pathGitHub 上下载对应的依赖https://github.com/chenZai90/ios-Rescorce 把 arc 文件放到你提示的目录底下即可问题 2redeclaration of C built-in type ‘bool’Infileincluded from.../src/jdk.hotspot.agent/macosx/native/libsaproc/MacosxDebuggerLocal.m:45: Infileincluded from.../src/jdk.hotspot.agent/macosx/native/libsaproc/libproc_impl.h:30:.../src/jdk.hotspot.agent/macosx/native/libsaproc/libproc.h:47:13: error: redeclaration of C built-intypebool47|typedef int bool;|^1error generated.文件src/jdk.hotspot.agent/macosx/native/libsaproc/libproc.h原因C23 下 bool 是内置类型非宏#ifndef bool/#ifdef bool均无法拦截Objective-C 模式下 bool 同样是内置关键字。修复删除 macOS 分支的 bool 定义添加stdbool.h当前文件最终状态#ifdef__APPLE__#includestdbool.htypedefenumps_err_e{PS_OK,PS_ERR,PS_BADPID,PS_BADLID,PS_BADADDR,PS_NOSYM,PS_NOFREGS}ps_err_e;#ifndefpsaddr_t#definepsaddr_tuintptr_t#endif/* 删除以下整个块 #ifndef __cplusplus #ifndef bool typedef int bool; #define true 1 #define false 0 #endif // bool #endif // __cplusplus */#ifndeflwpid_t#definelwpid_tuintptr_t#endif#includemach/thread_status.h问题 3too many arguments to function call,.../awt_ImagingLib.c:430:44: error: too many arguments tofunctioncall, expected0, have6430|if((*sMlibFns[MLIB_CONVKERNCVT].fptr)(kdata,scale,dkern,w,h,|~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~431|mlib_ImageGetType(src))!MLIB_SUCCESS){|~~~~~~~~~~~~~~~~~~~~~~.../awt_ImagingLib.c:458:45: error: too many arguments tofunctioncall, expected0, have10458|status(*sMlibFns[MLIB_CONVMxN].fptr)(dst, src, kdata, w, h,|~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ^~~~~~~~~~~~~~~~~~~~~~...8errors generated.原因mlibFnS_t.fptr 声明为旧式 mlib_status (*fptr)()C23 将其视为严格零参数原型 (void)。修复改为变参函数指针 (…)C17/C23/C 均合法共 3 个文件① src/java.desktop/share/native/libawt/awt/medialib/awt_ImagingLib.h 第 35 行// 修改前mlib_status(*fptr)();// 修改后mlib_status(*fptr)(...);② src/java.desktop/unix/native/libawt/awt/awt_Mlib.c// 第 53 行// 修改前mlib_status(*fPtr)();// 修改后mlib_status(*fPtr)(...);// 第 126 行// 修改前fPtr(mlib_status(*)())dlsym(handle,mptr[i].fname);// 修改后fPtr(mlib_status(*)(...))dlsym(handle,mptr[i].fname);③ src/java.desktop/windows/native/libawt/windows/awt_Mlib.cpp// 第 84 行// 修改前mlib_status(*fPtr)();// 修改后mlib_status(*fPtr)(...);// 第 88-89 行// 修改前fPtr(mlib_status(*)())::GetProcAddress(hDLL,pMlibFns[i].fname);// 修改后fPtr(mlib_status(*)(...))::GetProcAddress(hDLL,pMlibFns[i].fname);原则类型声明 / 强转中的 () → (…)调用处如 (*sMlibFns[…].fptr)(参数)不用动。问题 4too many arguments to function call.../CPrinterJob.m:69:21: error: conflicting typesforcreateDefaultNSPrintInfo69|static NSPrintInfo* createDefaultNSPrintInfo(JNIEnv* env, jstring printer)|^.../CPrinterJob.m:47:21: note: previous declaration is here47|static NSPrintInfo* createDefaultNSPrintInfo();|^.../CPrinterJob.m:453:55: error: too many arguments tofunctioncall, expected0, have2453|NSPrintInfo* printInfocreateDefaultNSPrintInfo(env, NULL);|~~~~~~~~~~~~~~~~~~~~~~~~ ^~~~~~~~~.../CPrinterJob.m:47:21: note:createDefaultNSPrintInfodeclared here47|static NSPrintInfo* createDefaultNSPrintInfo();|^...(rest of output omitted)文件src/java.desktop/macosx/native/libawt_lwawt/awt/CPrinterJob.m原因第 47 行前向声明 static NSPrintInfo* createDefaultNSPrintInfo(); 与第 69 行带参定义冲突C23 下 () (void)。修复第 47 行补全参数与定义一致// 修改前staticNSPrintInfo*createDefaultNSPrintInfo();// 修改后staticNSPrintInfo*createDefaultNSPrintInfo(JNIEnv*env,jstring printer);问题 5gif_lib.h - redeclaration of C built-in type ‘bool’* For target support_native_java.desktop_libsplashscreen_dgif_lib.o: Infileincluded from.../giflib/dgif_lib.c:48:.../giflib/gif_lib.h:50:13: error: redeclaration of C built-intypebool50|typedef int bool;|^1error generated. * For target support_native_java.desktop_libsplashscreen_gif_err.o: Infileincluded from.../giflib/gif_err.c:33:.../giflib/gif_lib.h:50:13: error: redeclaration of C built-intypebool文件src/java.desktop/share/native/libsplashscreen/giflib/gif_lib.h原因giflib 主动 #undef bool 后 typedef int bool;C23 下 bool 是内置关键字。修复加条件保护——仅非 C 且 C 标准低于 C23 时定义保留旧环境行为#includestddef.h#if!defined(__cplusplus)(!defined(__STDC_VERSION__)||__STDC_VERSION__202311L)#ifdefbool#undefbool#endiftypedefintbool;#definefalse0#definetrue1#endif问题 6fp.h’ file not found* For target support_native_java.desktop_libsplashscreen_pngerror.o: Infileincluded from.../libpng/pngerror.c:47:.../libpng/pngpriv.h:551:16: fatal error:fp.hfilenot found551|# include fp.h|^~~~~~1error generated. * For target support_native_java.desktop_libsplashscreen_pngget.o: Infileincluded from.../libpng/pngget.c:43:.../libpng/pngpriv.h:551:16: fatal error:fp.hfilenot found551|# include fp.h|^~~~~~1error generated. * For target support_native_java.desktop_libsplashscreen_pngmem.o: Infileincluded from.../libpng/pngmem.c:48:.../libpng/pngpriv.h:551:16: fatal error:fp.hfilenot found551|# include fp.h文件src/java.desktop/share/native/libsplashscreen/libpng/pngpriv.h第 544-555 行原因libpng 用 defined(TARGET_OS_MAC) 判断老式 Mac 环境但该宏在现代 macOS SDK 中恒为 1误入包含 Solaris/老 Mac 专属 fp.h 的分支。修复加APPLE分流现代苹果系统直接走 math.h#ifdefined(__APPLE__)#includemath.h#elif(defined(__MWERKS__)defined(macintosh))||defined(applec)||\defined(THINK_C)||defined(__SC__)||defined(TARGET_OS_MAC)/* We need to check that math.h hasnt already been included earlier * as it seems it doesnt agree with fp.h, yet we should really use * fp.h if possible. */#if!defined(__MATH_H__)!defined(__MATH_H)!defined(__cmath__)#includefp.h#endif#else#includemath.h#endif