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