I have to create a DLL with variable and functions exposed.
The C code that do it is:
#include <stdio.h>
double In = 1.0;
double Out = 0.0;
double G = 2;
void step() {
Out = G * In;
}
and compile command: gcc -shared -o root.dll example.c
Where other system get value based on memory address.
I want to create the same using Golang, but //export didnt generate readable dll method. Can someone give me a tip how can i do that?
try go build -buildmode=c-shared -o example.dll .
thanks MIMO431.
Its create the DLL but not exported method or variable is available.
Check the following comparative:
Golang can only export functions. To it, is required to use CGO, captalized function and comments to export, like:
//export <Name_of_function>
func <Name_of_function>(...){...}
Sample code:
package main
import "C"
// Global variables
var (
In float64 = 1.0
Out float64 = 0.0
G float64 = 2.0
)
//export Step
func Step() {
Out = G * In
}
//export GetIn
func GetIn() float64 {
return In
}
//export SetIn
func SetIn(value float64) {
In = value
}
//export GetOut
func GetOut() float64 {
return Out
}
//export GetG
func GetG() float64 {
return G
}
//export SetG
func SetG(value float64) {
G = value
}
func main() {
// This is required for main in shared libraries, but won’t be executed when called from C
}
Which will result in:
It should be generated by @Modestying sample command:
go build -buildmode=c-shared -o example.dll .