Home > other >  Java calls Golang generated dynamic library (DLL, so)
Java calls Golang generated dynamic library (DLL, so)

Time:05-09

# # 0 x01. Environment to
# # a. GCC
In the console input
` ` ` bash
GCC - v
` ` `
If prompt command is not found, it means that your computer is not in the GCC, go to install a, GCC's official website: https://gcc.gnu.org/
If never install GCC can be installed directly win friends - build, can help you quickly install
The official website: http://mingw-w64.org/doku.php/download/win-builds

# # 0 x02. Write the go program
We here just write a simple program, an addition to accept two integers, and then compute their, and returns,
Here, we will file named libhello. Go
` ` ` go
Package the main

The import "C"

//export Sum
Int func Sum (a, b int) int {
The return of a + b
}

Func main () {
}
` ` `
Note that even if is to compile into dynamic library, also should have the main function, the above ` import "C" ` must have
And be sure to have comments
` ` ` go
//export Sum
` ` `
After the test, if there is no this exported DLL library can't find the corresponding function

# # 0 x03. Compile the go program
First of all, the console directory to switch to go program directory, namely libhello. Go directory
# # # # a. Windows dynamic library
Perform the following command to generate DLL dynamic link library:
` ` ` bash
Go build - buildmode=c -shared -o libhello. DLL. \ libhello go
` ` `
If there is no error, the console is under current path generating libhello. DLL file

# # # # b. Linux/Unix/macOS dynamic library
Perform the following command to generate SO dynamic library:
` ` ` bash
Go build - buildmode=c -shared -o libhello. So. \ libhello go
` ` `
# # 0 x04. In Java called

# # # # a. JNA reference
Java calls Native there are two ways to get the dynamic library, JNI and JNA JNA is new way to interact with Native Oracle, I will not say in detail, reference the connection baidu encyclopedia: https://baike.baidu.com/item/JNA/8637274? Fr=Aladdin, friends in need can go and see,
Here, the way we use JNA, basic abandoned JNI way, unless you have special needs, here not much said, have need can contact me to discuss,
A new Java project, I do package management using the Maven, so direct reference JNA depends on:
` ` ` XML

Net, Java dev. Jna
Jna
4.5.2 & lt;/version>

` ` `
If you don't use the package management tool, you can directly download the Jar file into, download address and paste it, and 4.5.2 version:
http://central.maven.org/maven2/net/java/dev/jna/jna/4.5.2/jna-4.5.2.jar

# # # # b. create the interface
We need to create an interface to map the function in the DLL, and then we can through the instance of the interface to access the DLL function,
` ` ` javascript
Package cn. Lemonit. Robot. The runner. The executor;

The import com. Sun. Jna. Library;
The import com. Sun. Jna. Native;

Public interface LibHello extends Library {
LibHello INSTANCE=(LibHello) Native. LoadLibrary (" E:/workspace/LibHello LibHello. Class);

Int the Sum (int a, int b);
}
` ` `
Note that the Sum is the function name, must be consistent with the prior written the function name in the Go
Native loadLibrary () of the first parameter is a string, to load the dynamic library name or the full path, don't need to put the. DLL or. So the suffix, the second parameter for the interface class name,

# # # # c. call
We create a new App class, as the main method of entry class, in the main method does not need to be redundant operations, you just need to call, here, we call the Sum method, at the same time, such as 222, 333, you can see the console output: 555
` ` ` javascript
Package cn. Lemonit. Robot. The runner. The executor;

Public class App {

Public static void main (String [] args) {
System. The out. Println (LibHello. The INSTANCE. The Sum (222, 333));
}
}
` ` `
You're done, I finally played through the Java calls Go program!!!!!!!!!!
????? Wrong, a little too schadenfreude, continue down & gt;>

# # 0 x05. The parameters included in the string
# # # # a. accomplished I really?
Our program will not only pass the numeric parameters, we take the GO program change, instead of a string as a parameter to a function, takes a string parameter, and then from the console output: hello, XXX, is as follows:
` ` ` go
Package the main

The import "FMT"

//export Hello
Func Hello (MSG string) {
FMT. Print (" hello, "+ MSG)
}

Func main () {
}
` ` `
In accordance with the above 0 x02. B steps in the writing, we will LibHello Java interfaces to look like this:
` ` ` javascript
Package cn. Lemonit. Robot. The runner. The executor;

The import com. Sun. Jna. Library;
The import com. Sun. Jna. Native;

Public interface LibHello extends Library {
LibHello INSTANCE=(LibHello) Native. LoadLibrary (" E:/workspace/LibHello LibHello. Class);

Void Hello (String MSG);
}
` ` `
Next, we call this interface will be 0 x02. Start the entrance into this kind of App code in the C:
` ` ` javascript
Package cn. Lemonit. Robot. The runner. The executor;

Public class App {

Public static void main (String [] args) {
LibHello. INSTANCE. Hello (" LemonIT. CN ");
}
}
` ` `
Run up, yi? Error???
` ` ` javascript
Fatal error: string concatenation too long

Goroutine 17 [running, locked to thread] :
X1d x644c1d4f runtime. Throw (0, 0)
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX (runtime error, no meaning, posted)
` ` `
What's going on, I was a very standard String ah, how can an error?
In a blind, found in the call just now ` go build - buildmode=c - Shared - o libhello. DLL. \ libhello go libhello. ` command of time in the folder, in addition to the DLL is generated, also generates a libhello. H file!!!!!! This is not a C header file? Out of curiosity, open and see what's on the tall, the open is scaring me:
` ` ` c
/* Created by "go tool cgo" - DO NOT EDIT. */

/* package command - line - the arguments */

Line # 1 "cgo - builtin - prolog"

#include /* for ptrdiff_t below */

# # ifndef GO_CGO_EXPORT_PROLOGUE_H
# define GO_CGO_EXPORT_PROLOGUE_H

Typedef struct {const char * p; Ptrdiff_t n; } _GoString_;

# endif

/* the Start of the preamble from import "C" comments. */
/* End of preamble from import "C" comments. */

/* Start of boilerplate cgo prologue. */
Line # 1 "cgo - GCC - export - the header - prolog"

# # ifndef GO_CGO_PROLOGUE_H
# define GO_CGO_PROLOGUE_H

Typedef signed char GoInt8;
Typedef unsigned char GoUint8;
Typedef short GoInt16;
Typedef unsigned short GoUint16;
Typedef int GoInt32;
Typedef unsigned int GoUint32;
Typedef long long GoInt64;
nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull