I want to build a .so library using Go/Cgo with options go build -buildmode=c-shared
. Functions exports well, but I cannot export variables. I need to realize an API, which works by calling a void function, which sets up values of various global properties. Something like this:
var (
Gval1 int
Gval2 string
//GvalN
)
func f(){
Gval1 = 1
Gval2 = "qwerty"
}
The client of .so lib will run f(); and after that, it can get variables by addressing their names. How can I export them? I had tried to do a trick like this: golang cgo can't export variables by build mode c-shared, but there was no success (example returns always 0, not 42). How can I export variables (numbers and strings)?
I don't think you can export variables, only functions.
The go build documentation says:
-buildmode=c-shared
Build the listed main package, plus all packages it imports,
into a C shared library. The only callable symbols will
be those functions exported using a cgo //export comment.
Requires exactly one main package to be listed
Where the cgo docs says
Go functions can be exported for use by C code in the following way:
I guess you can write a function that returns the variable value.