Ubuntu22.04使用GraalVM22.2生成Native Image遇gcc检测异常
问题:GraalVM Native Image编译时检测GCC失败(Ubuntu 22.04)
环境信息
GraalVM安装状态正常,版本信息如下:
# /opt/graalvm/bin/java -version openjdk version "17.0.4" 2022-07-19 OpenJDK Runtime Environment GraalVM CE 22.2.0 (build 17.0.4+8-jvmci-22.2-b06) OpenJDK 64-Bit Server VM GraalVM CE 22.2.0 (build 17.0.4+8-jvmci-22.2-b06, mixed mode, sharing)
执行的Native Image编译命令
/opt/graalvm/bin/native-image \ -H:ReflectionConfigurationFiles=graalvm/reflect.json \ -H:+ReportExceptionStackTraces \ --no-fallback \ --enable-url-protocols=http \ --initialize-at-build-time=org.slf4j.LoggerFactory \ --initialize-at-build-time=org.slf4j.impl.SimpleLogger \ --initialize-at-build-time=org.slf4j.impl.StaticLoggerBinder \ -jar ../app/target/app-0.9.jar
编译错误日志
Error: Collecting native-compiler info with '/usr/bin/gcc -v' failed Error: To prevent native-toolchain checking provide command-line option -H:-CheckToolchain com.oracle.svm.core.util.UserError$UserException: Collecting native-compiler info with '/usr/bin/gcc -v' failed To prevent native-toolchain checking provide command-line option -H:-CheckToolchain at org.graalvm.nativeimage.builder/com.oracle.svm.core.util.UserError.abort(UserError.java:138) at org.graalvm.nativeimage.builder/com.oracle.svm.hosted.c.codegen.CCompilerInvoker.addSkipCheckingInfo(CCompilerInvoker.java:104) at org.graalvm.nativeimage.builder/com.oracle.svm.hosted.c.codegen.CCompilerInvoker.<init>(CCompilerInvoker.java:72) at org.graalvm.nativeimage.builder/com.oracle.svm.hosted.c.codegen.CCompilerInvoker$LinuxCCompilerInvoker.<init>(CCompilerInvoker.java:212) at org.graalvm.nativeimage.builder/com.oracle.svm.hosted.c.codegen.CCompilerInvoker.create(CCompilerInvoker.java:80) at org.graalvm.nativeimage.builder/com.oracle.svm.hosted.NativeImageGenerator.setupNativeImage(NativeImageGenerator.java:891) at org.graalvm.nativeimage.builder/com.oracle.svm.hosted.NativeImageGenerator.doRun(NativeImageGenerator.java:561) at org.graalvm.nativeimage.builder/com.oracle.svm.hosted.NativeImageGenerator.run(NativeImageGenerator.java:521) at org.graalvm.nativeimage.builder/com.oracle.svm.hosted.NativeImageGeneratorRunner.buildImage(NativeImageGeneratorRunner.java:407) at org.graalvm.nativeimage.builder/com.oracle.svm.hosted.NativeImageGeneratorRunner.build(NativeImageGeneratorRunner.java:585) at org.graalvm.nativeimage.builder/com.oracle.svm.hosted.NativeImageGeneratorRunner.main(NativeImageGeneratorRunner.java:128)
GCC版本确认
系统已安装GCC,版本为11.2.0:
# /usr/bin/gcc -v Using built-in specs. COLLECT_GCC=/usr/bin/gcc COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-linux-gnu/11/lto-wrapper OFFLOAD_TARGET_NAMES=nvptx-none:amdgcn-amdhsa OFFLOAD_TARGET_DEFAULT=1 Target: x86_64-linux-gnu Configured with: ../src/configure -v --with-pkgversion='Ubuntu 11.2.0-19ubuntu1' --with-bugurl=file:///usr/share/doc/gcc-11/README.Bugs --enable-languages=c,ada,c++,go,brig,d,fortran,objc,obj-c++,m2 --prefix=/usr --with-gcc-major-version-only --program-suffix=-11 --program-prefix=x86_64-linux-gnu- --enable-shared --enable-linker-build-id --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --libdir=/usr/lib --enable-nls --enable-bootstrap --enable-clocale=gnu --enable-libstdcxx-debug --enable-libstdcxx-time=yes --with-default-libstdcxx-abi=new --enable-gnu-unique-object --disable-vtable-verify --enable-plugin --enable-default-pie --with-system-zlib --enable-libphobos-checking=release --with-target-system-zlib=auto --enable-objc-gc=auto --enable-multiarch --disable-werror --enable-cet --with-arch-32=i686 --with-abi=m64 --with-multilib-list=m32,m64,mx32 --enable-multilib --with-tune=generic --enable-offload-targets=nvptx-none=/build/gcc-11-gBFGDP/gcc-11-11.2.0/debian/tmp-nvptx/usr,amdgcn-amdhsa=/build/gcc-11-gBFGDP/gcc-11-11.2.0/debian/tmp-gcn/usr --without-cuda-driver --enable-checking=release --build=x86_64-linux-gnu --host=x86_64-linux-gnu --target=x86_64-linux-gnu --with-build-config=bootstrap-lto-lean --enable-link-serialization=2 Thread model: posix Supported LTO compression algorithms: zlib zstd gcc version 11.2.0 (Ubuntu 11.2.0-19ubuntu1)
解决方案
1. 临时跳过工具链检查
按照错误提示,添加-H:-CheckToolchain参数跳过GCC检测,修改后的命令如下:
/opt/graalvm/bin/native-image \ -H:ReflectionConfigurationFiles=graalvm/reflect.json \ -H:+ReportExceptionStackTraces \ --no-fallback \ --enable-url-protocols=http \ --initialize-at-build-time=org.slf4j.LoggerFactory \ --initialize-at-build-time=org.slf4j.impl.SimpleLogger \ --initialize-at-build-time=org.slf4j.impl.StaticLoggerBinder \ -H:-CheckToolchain \ -jar ../app/target/app-0.9.jar
2. 安装完整的C工具链依赖
虽然GCC已安装,但可能缺少编译Native Image所需的其他依赖包,执行以下命令安装完整工具链:
sudo apt install build-essential libz-dev zlib1g-dev
3. 手动指定编译器路径
如果工具链检测仍有问题,可以手动指定GCC路径,添加-H:CCompilerPath=/usr/bin/gcc参数到命令中:
/opt/graalvm/bin/native-image \ -H:ReflectionConfigurationFiles=graalvm/reflect.json \ -H:+ReportExceptionStackTraces \ --no-fallback \ --enable-url-protocols=http \ --initialize-at-build-time=org.slf4j.LoggerFactory \ --initialize-at-build-time=org.slf4j.impl.SimpleLogger \ --initialize-at-build-time=org.slf4j.impl.StaticLoggerBinder \ -H:CCompilerPath=/usr/bin/gcc \ -jar ../app/target/app-0.9.jar
内容的提问来源于stack exchange,提问作者Tim Gustafson
相关产品推荐
相关产品推荐

