Skip to content

Commit

Permalink
Merge pull request #243 from graalvm/ls/native-shared-library
Browse files Browse the repository at this point in the history
Upgrade Build a Shared Library with GraalVM Native Image Demo
  • Loading branch information
LesiaChaban committed Dec 20, 2023
2 parents 27a2f40 + a7052c7 commit bc00992
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 14 deletions.
5 changes: 2 additions & 3 deletions .github/workflows/native-shared-library.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,20 +20,19 @@ jobs:
timeout-minutes: 15
strategy:
matrix:
java-version: ['17', 'dev']
java-version: ['21', 'dev']
steps:
- uses: actions/checkout@v3
- uses: graalvm/setup-graalvm@v1
with:
java-version: ${{ matrix.java-version }}
distribution: 'graalvm'
components: 'llvm-toolchain'
github-token: ${{ secrets.GITHUB_TOKEN }}
native-image-job-reports: 'true'
- name: Run 'native-shared-library'
run: |
cd native-shared-library
javac LibEnvMap.java
native-image -H:Name=libenvmap --shared
native-image -o libenvmap --shared
clang -I ./ -L ./ -l envmap -Wl,-rpath ./ -o main main.c
./main USER
22 changes: 11 additions & 11 deletions native-shared-library/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,15 @@ There are two primary mechanisms for calling a method (function) embedded in a n
This example demonstrates how to use the **Native Image C API**. We will:

1. Create and compile a Java class library containing at least one entrypoint method.
2. Use the `native-image` tool to create a shared library from the Java class library.
2. Use the `native-image` technology to create a shared library from the Java class library.
3. Create and compile a C application that calls an entrypoint method in the shared library.

## Preparation

1. Download and install the latest GraalVM JDK with Native Image and LLVM toolchain using the [GraalVM JDK Downloader](https://github.com/graalvm/graalvm-jdk-downloader). With the LLVM toolchain, you can compile C/C++ code to bitcode using `clang` shipped with GraalVM:
1. Download and install the latest GraalVM JDK using [SDKMAN!](https://sdkman.io/).
```bash
bash <(curl -sL https://get.graalvm.org/jdk) -c 'native-image,llvm-toolchain'
sdk install java 21.0.1-graal
```
> Note: The `llvm-toolchain` GraalVM component is not available on Microsoft Windows.

2. Download or clone GraalVM demos repository and navigate into the `native-shared-library` directory:
```bash
Expand All @@ -31,7 +30,7 @@ This example demonstrates how to use the **Native Image C API**. We will:
$JAVA_HOME/bin/javac LibEnvMap.java
```
```bash
$JAVA_HOME/bin/native-image -H:Name=libenvmap --shared
$JAVA_HOME/bin/native-image -o libenvmap --shared
```

It will produce the following artifacts:
Expand All @@ -49,20 +48,21 @@ This example demonstrates how to use the **Native Image C API**. We will:

In the result of this process the native shared library will have the `main()` method of the given Java class as its **entrypoint** method.

If your library does not include a `main()` method, use the `-H:Name=` command-line option to specify the library name, as follows:
If your library does not include a `main()` method, use the `-o` command-line option to specify the library name, as follows:

```bash
native-image --shared -H:Name=<libraryname> <class name>
native-image --shared -o <libraryname> <class name>
```
```bash
native-image --shared -jar <jarfile> -H:Name=<libraryname>
native-image --shared -jar <jarfile> -o <libraryname>
```

4. Compile the _main.c_ using `clang`.
4. Compile the _main.c_ using the `clang` compiler on your system:
```bash
$JAVA_HOME/languages/llvm/native/bin/clang -I ./ -L ./ -l envmap -Wl,-rpath ./ -o main main.c
clang -I ./ -L ./ -l envmap -Wl,-rpath ./ -o main main.c
```
5. And finally, run the C application by passing a string as an argument. For example:

5. Finally, run the C application by passing a string as an argument. For example:
```bash
./main USER
```
Expand Down

0 comments on commit bc00992

Please sign in to comment.