Graal: [native-image] LINK : fatal error LNK1181: cannot open input file 'stdc++.lib' on Windows

Created on 23 Nov 2019  路  12Comments  路  Source: oracle/graal

Introduction

For simple Java code using some basic HTTPS functionality, building a native-image binary with -H:EnableURLProtocols=https does not succeed on Windows 10 with Graal 19.3.0.

Instead error output including the following is observed:

LINK : fatal error LNK1181: cannot open input file 'stdc++.lib'

Seemingly analogous steps produce a working binary under at least one Linux distribution.

(A repository with details and files is at: https://github.com/sogaiu/stdcpp-link-error)

Description

Consider the following code:

// modified: https://alvinalexander.com/blog/post/java/simple-https-example
import java.net.URL;
import java.io.*;
import javax.net.ssl.HttpsURLConnection;

public class JavaHttpsExample
{
    public static void main(String[] args) throws Exception {
        String httpsURL = "https://www.clojure.org/";
        URL myUrl = new URL(httpsURL);
        HttpsURLConnection conn = (HttpsURLConnection)myUrl.openConnection();
        InputStream is = conn.getInputStream();
        InputStreamReader isr = new InputStreamReader(is);
        BufferedReader br = new BufferedReader(isr);
        String inputLine;
        while ((inputLine = br.readLine()) != null) {
            System.out.println(inputLine);
        }
        br.close();
    }
}

Assuming an appropriate value for GRAALVM_HOME, the following batch file does not successfully produce a native-image binary on Windows 10:

@echo off

if "%GRAALVM_HOME%"=="" (
    echo Please set GRAALVM_HOME
    exit /b
)

%GRAALVM_HOME%\bin\javac JavaHttpsExample.java

call %GRAALVM_HOME%\bin\native-image.cmd ^
  JavaHttpsExample ^
  "-H:Name=stdcpp-link-error" ^
  "-H:EnableURLProtocols=https" ^
  --verbose

Prerequisites

  • Windows 10, preferably as fresh an installation as possible
  • Windows SDK for Windows 7 (7.1)
  • Graal 19.3.0
  • Some Linux distribution to compare results

Windows Steps

  • Prep

    • Clone this repository
    • cd to cloned repository directory
  • Build native-image binary

    • Set GRAALVM_HOME environment variable appropriately
    • From "Windows SDK 7.1 Command Prompt": windows-compile.bat
    • Observe error output (partial output below, see error-output.txt file for full output):
LINK : fatal error LNK1181: cannot open input file 'stdc++.lib'

        at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
        at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
        at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
        at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
        at java.util.concurrent.ForkJoinTask.getThrowableException(ForkJoinTask.java:593)
        at java.util.concurrent.ForkJoinTask.get(ForkJoinTask.java:1005)
        at com.oracle.svm.hosted.NativeImageGenerator.run(NativeImageGenerator.java:462)
        at com.oracle.svm.hosted.NativeImageGeneratorRunner.buildImage(NativeImageGeneratorRunner.java:315)
        at com.oracle.svm.hosted.NativeImageGeneratorRunner.build(NativeImageGeneratorRunner.java:454)
        at com.oracle.svm.hosted.NativeImageGeneratorRunner.main(NativeImageGeneratorRunner.java:115)
Caused by: java.lang.RuntimeException: host C compiler or linker does not seem to work: java.lang.RuntimeException: returned 2

Linux Distribution Steps

  • Prep

    • Clone this repository
    • cd to cloned repository directory
  • Build native-image binary

    • Set GRAALVM_HOME environment variable appropriately
    • From command line: bash nix-compile
    • Observe newly created native-image binary named stdcpp-link-error
native-image platform-windows

Most helpful comment

@pejovica Looks like the fix did not make it into 19.3.0.2 (I was able to reproduce with graalvm-ce-java8-19.3.0.2 just now); or am I missing something?

FYI my options are:

'--no-server',
'--no-fallback',
'--enable-all-security-services',
'-H:EnableURLProtocols=http,https',
'--report-unsupported-elements-at-runtime'

All 12 comments

Not sure if it contributes, but attempting to build a native image of a simple client http call, using Akka-HTTP (Scala) on Win 7 (using https://github.com/vmencik/akka-graal-config ). Produces the same linking error.

Is this with GraalVM 19.3.0 for java 8 or java 11 (or both)?

Java 8 in my case.

Sorry for forgetting to mention that part. I'll have a shot at it with 11.

Hard to call that one better

Fatal error: com.oracle.svm.core.util.VMError$HostedError: class java.net.TwoStacksPlainSocketImpl not found
        at com.oracle.svm.core.util.VMError.guarantee(VMError.java:85)
        at com.oracle.svm.core.jdk.JNIRegistrationUtil.clazz(JNIRegistrationUtil.java:71)
        at com.oracle.svm.core.jdk.JNIRegistrationUtil.fields(JNIRegistrationUtil.java:84)
        at com.oracle.svm.hosted.jdk.JNIRegistrationJavaNet.registerPlainSocketImplInitProto(JNIRegistrationJavaNet.java:229)
        at com.oracle.svm.hosted.ReachabilityHandlerFeature.duringAnalysis(ReachabilityHandlerFeature.java:114)
        at com.oracle.svm.hosted.NativeImageGenerator.lambda$runPointsToAnalysis$8(NativeImageGenerator.java:710)
        at com.oracle.svm.hosted.FeatureHandler.forEachFeature(FeatureHandler.java:63)
        at com.oracle.svm.hosted.NativeImageGenerator.runPointsToAnalysis(NativeImageGenerator.java:710)
        at com.oracle.svm.hosted.NativeImageGenerator.doRun(NativeImageGenerator.java:530)
        at com.oracle.svm.hosted.NativeImageGenerator.lambda$run$0(NativeImageGenerator.java:445)

@sogaiu, thanks for the report. The fix is on the way.

@remkop, both JDK 8 and JDK 11 are affected, though on JDK 11 this is masked by #1870 as @spangaer noticed.

Any updates?

This was fixed in 515662686d2e5458824123d8a483e68610e932f7.

@pejovica Looks like the fix did not make it into 19.3.0.2 (I was able to reproduce with graalvm-ce-java8-19.3.0.2 just now); or am I missing something?

FYI my options are:

'--no-server',
'--no-fallback',
'--enable-all-security-services',
'-H:EnableURLProtocols=http,https',
'--report-unsupported-elements-at-runtime'

@remkop The fix will land in the upcoming GraalVM 20.0 release.

Thanks for the update on the situation.

I just ran into the same issue.
I copied the call to CL and removed the stdc++.lib argument manually:

CL /MD /Zi /PDBSTRIPPED /FeC:\Users\mucmaucm\Workspaces\WN\tempo-tt-sync\tempo_sb_sync.exe C:\Users\mucmaucm\AppData\Local\Temp\SVM-56914988070265978\
tempo_sb_sync.tmp C:\Users\mucmaucm\scoop\apps\graalvm8\current\jre\lib\svm\clibraries\windows-amd64\ffi.lib C:\Users\mucmaucm\scoop\apps\graalvm8\current\jre\lib\svm\clibraries\windows-amd64\libc
helper.lib C:\Users\mucmaucm\scoop\apps\graalvm8\current\jre\lib\svm\clibraries\windows-amd64\strictmath.lib C:\Users\mucmaucm\scoop\apps\graalvm8\current\jre\lib\svm\clibraries\windows-amd64\jvm.
lib C:\Users\mucmaucm\scoop\apps\graalvm8\19.3.0.2\jre\lib\net.lib C:\Users\mucmaucm\scoop\apps\graalvm8\19.3.0.2\jre\lib\zip.lib C:\Users\mucmaucm\scoop\apps\graalvm8\19.3.0.2\jre\lib\java.lib C:
\Users\mucmaucm\scoop\apps\graalvm8\19.3.0.2\jre\lib\nio.lib C:\Users\mucmaucm\scoop\apps\graalvm8\19.3.0.2\jre\lib\sunec.lib C:\Users\mucmaucm\scoop\apps\graalvm8\19.3.0.2\jre\lib\sunec.lib C:\Us
ers\mucmaucm\scoop\apps\graalvm8\current\jre\lib\svm\clibraries\windows-amd64\ffi.lib C:\Users\mucmaucm\scoop\apps\graalvm8\current\jre\lib\svm\clibraries\windows-amd64\libchelper.lib C:\Users\muc
maucm\scoop\apps\graalvm8\current\jre\lib\svm\clibraries\windows-amd64\strictmath.lib C:\Users\mucmaucm\scoop\apps\graalvm8\current\jre\lib\svm\clibraries\windows-amd64\jvm.lib C:\Users\mucmaucm\s
coop\apps\graalvm8\19.3.0.2\jre\lib\net.lib C:\Users\mucmaucm\scoop\apps\graalvm8\19.3.0.2\jre\lib\zip.lib C:\Users\mucmaucm\scoop\apps\graalvm8\19.3.0.2\jre\lib\java.lib C:\Users\mucmaucm\scoop\a
pps\graalvm8\19.3.0.2\jre\lib\nio.lib C:\Users\mucmaucm\scoop\apps\graalvm8\19.3.0.2\jre\lib\sunec.lib C:\Users\mucmaucm\scoop\apps\graalvm8\19.3.0.2\jre\lib\sunec.lib /link /INCREMENTAL:NO /NODEF
AULTLIB:LIBCMT /NODEFAULTLIB:OLDNAMES /LIBPATH:C:\Users\mucmaucm\scoop\apps\graalvm8\19.3.0.2\jre\lib /LIBPATH:C:\Users\mucmaucm\scoop\apps\graalvm8\current\jre\lib\svm\clibraries\windows-amd64 ad
vapi32.lib ws2_32.lib secur32.lib iphlpapi.lib
Microsoft (R) C/C++ Optimizing Compiler Version 16.00.30319.01 for x64
Copyright (C) Microsoft Corporation.  All rights reserved.

cl : Command line warning D9002 : ignoring unknown option '/PDBSTRIPPED'
cl : Command line warning D9024 : unrecognized source file type 'C:\Users\mucmaucm\AppData\Local\Temp\SVM-56914988070265978\tempo_sb_sync.tmp', object file assumed
Microsoft (R) Incremental Linker Version 10.00.30319.01
Copyright (C) Microsoft Corporation.  All rights reserved.

/debug
/out:C:\Users\mucmaucm\Workspaces\WN\tempo-tt-sync\tempo_sb_sync.exe
/INCREMENTAL:NO
/NODEFAULTLIB:LIBCMT
/NODEFAULTLIB:OLDNAMES
/LIBPATH:C:\Users\mucmaucm\scoop\apps\graalvm8\19.3.0.2\jre\lib
/LIBPATH:C:\Users\mucmaucm\scoop\apps\graalvm8\current\jre\lib\svm\clibraries\windows-amd64
advapi32.lib
ws2_32.lib
secur32.lib
iphlpapi.lib
C:\Users\mucmaucm\AppData\Local\Temp\SVM-56914988070265978\tempo_sb_sync.tmp
C:\Users\mucmaucm\scoop\apps\graalvm8\current\jre\lib\svm\clibraries\windows-amd64\ffi.lib
C:\Users\mucmaucm\scoop\apps\graalvm8\current\jre\lib\svm\clibraries\windows-amd64\libchelper.lib
C:\Users\mucmaucm\scoop\apps\graalvm8\current\jre\lib\svm\clibraries\windows-amd64\strictmath.lib
C:\Users\mucmaucm\scoop\apps\graalvm8\current\jre\lib\svm\clibraries\windows-amd64\jvm.lib
C:\Users\mucmaucm\scoop\apps\graalvm8\19.3.0.2\jre\lib\net.lib
C:\Users\mucmaucm\scoop\apps\graalvm8\19.3.0.2\jre\lib\zip.lib
C:\Users\mucmaucm\scoop\apps\graalvm8\19.3.0.2\jre\lib\java.lib
C:\Users\mucmaucm\scoop\apps\graalvm8\19.3.0.2\jre\lib\nio.lib
C:\Users\mucmaucm\scoop\apps\graalvm8\19.3.0.2\jre\lib\sunec.lib
C:\Users\mucmaucm\scoop\apps\graalvm8\19.3.0.2\jre\lib\sunec.lib
C:\Users\mucmaucm\scoop\apps\graalvm8\current\jre\lib\svm\clibraries\windows-amd64\ffi.lib
C:\Users\mucmaucm\scoop\apps\graalvm8\current\jre\lib\svm\clibraries\windows-amd64\libchelper.lib
C:\Users\mucmaucm\scoop\apps\graalvm8\current\jre\lib\svm\clibraries\windows-amd64\strictmath.lib
C:\Users\mucmaucm\scoop\apps\graalvm8\current\jre\lib\svm\clibraries\windows-amd64\jvm.lib
C:\Users\mucmaucm\scoop\apps\graalvm8\19.3.0.2\jre\lib\net.lib
C:\Users\mucmaucm\scoop\apps\graalvm8\19.3.0.2\jre\lib\zip.lib
C:\Users\mucmaucm\scoop\apps\graalvm8\19.3.0.2\jre\lib\java.lib
C:\Users\mucmaucm\scoop\apps\graalvm8\19.3.0.2\jre\lib\nio.lib
C:\Users\mucmaucm\scoop\apps\graalvm8\19.3.0.2\jre\lib\sunec.lib
C:\Users\mucmaucm\scoop\apps\graalvm8\19.3.0.2\jre\lib\sunec.lib
   Creating library C:\Users\mucmaucm\Workspaces\WN\tempo-tt-sync\tempo_sb_sync.lib and object C:\Users\mucmaucm\Workspaces\WN\tempo-tt-sync\tempo_sb_sync.exp
sunec.lib(ecl.obj) : error LNK2019: unresolved external symbol __imp_strdup referenced in function ecgroup_fromNameAndHex
sunec.lib(ecl_curve.obj) : error LNK2001: unresolved external symbol __imp_strdup
C:\Users\mucmaucm\Workspaces\WN\tempo-tt-sync\tempo_sb_sync.exe : fatal error LNK1120: 1 unresolved externals

The LNK2001: unresolved external symbol __imp_strdup error seems to be caused by missing libraries, so I just wanted to ask @pejovica if removing stdc++ from the native libraries as done in https://github.com/oracle/graal/commit/515662686d2e5458824123d8a483e68610e932f7 is sufficient.

Tested this with graal 20.1-dev jdk8 and it seemed to work now!

Was this page helpful?
0 / 5 - 0 ratings