
《深入理解 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