Java calls Golang generated dynamic library (DLL,SO)

Source: Internet
Author: User

0x01. Environment Preparation

A. GCC

Enter in console

gcc -v

If the prompt command is not found, then there is no GCC on your computer, go to install one, GCC official website: https://gcc.gnu.org/
If a friend who has never installed GCC can install win-build directly, it will help you install it quickly.
Official website: http://mingw-w64.org/doku.php/download/win-builds

0x02. Writing Go Programs

Here we just write a simple output string of the program, accept a string argument, then stitch it into a new string and return it as the return value, here we name the file Libhello.go

package mainimport "C"func helloLemonITCN(msg string) string {    return "LemonIT.CN : Hello! " + msg}func main() {}

Note that even if you want to compile into a dynamic library, there must be a main function, the above import "C" must have

0x03. Compiling the Go Program

First, switch the directory where the console is located to the directory where the Go program is located, which is the Libhello.go directory

A. Windows Dynamic Library

Execute the following command to generate the DLL dynamic-link library:

go build -buildmode=c-shared -o libhello.dll .\libhello.go

If the console does not have an error, the Libhello.dll file is generated under the current path

B. Linux/unix/macos Dynamic Library

Execute the following command to generate the so dynamic library:

go build -buildmode=c-shared -o libhello.so .\libhello.go

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.