我在编译中遇到的最大的问题就是gcc版本问题,因manjaro是滚动更新,所以gcc也是很新的版本,导致无法编译jdk11
网上根据关键词查找jdk源码,查找出来很多可以下载源码的链接,这里我们使用github去官方仓库,openjdk是托管在github的OpenJDK组织下,该组织下有各个版本的openjdk源码,不要直接使用jdk仓库,这个仓库存放的是当前正在开发的最新版本代码,我们要用的是jdk11,因此我们搜索jdk11仓库,我这里选择的是jdk11u这个库。
git clone https://github.com/openjdk/jdk11u.git
按照readme的文档进行编译
首先执行进入源码目录,执行bash configure
此命令会检查编译需要的环境,如果报错,根据错误提示安装必要的编译工具。
注意事项:
bash configure
#……省略前边若干日志
#……
config.status: creating /home/chenkun/IdeaProjects/jdk11u/build/linux-x86_64-normal-server-release/buildjdk-spec.gmk
config.status: creating /home/chenkun/IdeaProjects/jdk11u/build/linux-x86_64-normal-server-release/compare.sh
config.status: creating /home/chenkun/IdeaProjects/jdk11u/build/linux-x86_64-normal-server-release/Makefile
====================================================
The existing configuration has been successfully updated in
/home/chenkun/IdeaProjects/jdk11u/build/linux-x86_64-normal-server-release
using default settings.
Configuration summary:
* Debug level: release
* HS debug level: product
* JVM variants: server
* JVM features: server: 'aot cds cmsgc compiler1 compiler2 epsilongc g1gc graal jfr jni-check jvmci jvmti management nmt parallelgc serialgc services vm-structs zgc'
* OpenJDK target: OS: linux, CPU architecture: x86, address length: 64
* Version string: 11.0.18-internal+0-adhoc.chenkun.jdk11u (11.0.18-internal)
Tools summary:
* Boot JDK: openjdk version "11.0.17" 2022-10-18 OpenJDK Runtime Environment (build 11.0.17+1) OpenJDK 64-Bit Server VM (build 11.0.17+1, mixed mode) (at /usr/lib/jvm/java-11-openjdk)
* Toolchain: gcc (GNU Compiler Collection)
* C Compiler: Version 12.2.0 (at /usr/bin/gcc)
* C++ Compiler: Version 12.2.0 (at /usr/bin/g++)
Build performance summary:
* Cores to use: 16
* Memory limit: 31876 MB
如上,执行bash configure
后,检查了环境包括Boot JDK,Toochain,C Compiler,C++ Compiler,这里c和c++编译器版本要一致,可以看到上面我的版本是12.2.0,这个版本太新会导致编译有问题,在building文档中也有说明,最新支持到7.4,更新的版本没有测试,可能兼容可能不兼容。
make images
##省略中间若干日志
/home/chenkun/IdeaProjects/jdk11u/src/hotspot/os/linux/cgroupV1Subsystem_linux.hpp:105:31: error: non-static data member initializers only available with '-std=c++11' or '-std=gnu++11' [-Werror=c++11-extensions]
105 | CgroupV1Controller* _pids = NULL;
... (rest of output omitted)
* For target hotspot_variant-server_libjvm_objs_cgroupV2Subsystem_linux.o:
In file included from /home/chenkun/IdeaProjects/jdk11u/src/hotspot/os/linux/cgroupV2Subsystem_linux.cpp:25:
/home/chenkun/IdeaProjects/jdk11u/src/hotspot/os/linux/cgroupV2Subsystem_linux.hpp:54:32: error: non-static data member initializers only available with '-std=c++11' or '-std=gnu++11' [-Werror=c++11-extensions]
54 | CgroupController* _unified = NULL;
| ^
/home/chenkun/IdeaProjects/jdk11u/src/hotspot/os/linux/cgroupV2Subsystem_linux.hpp:56:38: error: non-static data member initializers only available with '-std=c++11' or '-std=gnu++11' [-Werror=c++11-extensions]
56 | CachingCgroupController* _memory = NULL;
| ^
/home/chenkun/IdeaProjects/jdk11u/src/hotspot/os/linux/cgroupV2Subsystem_linux.hpp:57:35: error: non-static data member initializers only available with '-std=c++11' or '-std=gnu++11' [-Werror=c++11-extensions]
57 | CachingCgroupController* _cpu = NULL;
| ^
cc1plus: all warnings being treated as errors
* All command lines available in /home/chenkun/IdeaProjects/jdk11u/build/linux-x86_64-normal-server-release/make-support/failure-logs.
=== End of repeated output ===
No indication of failed target found.
Hint: Try searching the build log for '] Error'.
Hint: See doc/building.html#troubleshooting for assistance.
make[1]: *** [/home/chenkun/IdeaProjects/jdk11u/make/Init.gmk:305:main] 错误 2
make: *** [/home/chenkun/IdeaProjects/jdk11u/make/Init.gmk:186:images] 错误 2
以上编译报错了,根据搞错大概也看出编译器的版本最大只能支持到11,所以我这里需要安装另一个版本的编译器,这里就体现arch系linux的缺点了,滚动版本太激进会导致有的老软件存在各种版本不兼容。
使用yay安装老版本的gcc,这里我和文档保持一致,文档说目前最新的已测试通过的版本是gcc7,这里我就安装gcc7:
$ yay gcc7
8 aur/armeb-none-eabi-gcc75-linaro-bin 7.5_2019.12-1 (+0 0.00)
The GNU Compiler Collection - cross compiler for ARM EABI (bare-metal) big-endian target.
7 aur/arm-none-eabi-gcc73-linaro 7.3_2018.05-1 (+0 0.00) (过时的: 2021-11-02)
The GNU Compiler Collection - cross compiler for ARM EABI (bare-metal) target.
6 aur/aarch64-linux-gnu-gcc75-linaro-bin 2019.12-1 (+0 0.00)
The GNU Compiler Collection - cross compiler for ARM64 target
5 aur/arm-linux-gnueabi-gcc75-linaro-bin 7.5.0-1 (+1 0.00)
The GNU Compiler Collection - cross compiler for ARM target
4 aur/arm-linux-gnueabihf-gcc75-linaro-bin 7.5-0 (+2 0.57)
The GNU Compiler Collection- cross compiler for ARMv7 EABI hard float target. (Linaro)
3 aur/gcc7-libs 7.5.0-4 (+15 0.02) (已安装)
Runtime libraries shipped by GCC (7.x.x)
2 aur/gcc7-fortran 7.5.0-4 (+15 0.02)
Fortran front-end for GCC (7.x.x)
1 aur/gcc7 7.5.0-4 (+15 0.02) (已安装)
The GNU Compiler Collection - C and C++ frontends (7.x.x)
==> 要安装的包 (示例: 1 2 3, 1-3 或 ^4)
==>
如上,选择第一个就行了,因为我已经安装过了,所以显示已安装。
安装好了以后,需要把默认的gcc改成7版本的。
##1. 查看当前默认的gcc在哪里
which gcc
/usr/bin/gcc
which g++
/usr/bin/g++
##2. 备份原来的12.2.0版本,编译完jdk后还原,防止系统其他软件出现问题
cd /usr/bin
sudo cp gcc gcc.bak
sudo cp g++ g++.bak
## 3. 把gcc7设为默认
sudo cp gcc-7 gcc
sudo cp g++-7 g++
设置默认gcc后,重新执行bash configure
,然后make clean
,然后再make images
就成功了,编译好的jdk默认在./build/linux-x86_64-normal-server-release/images/jdk