Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Go build shared-c is not outputting a header file

Tags:

go

I am trying to test build a shared C lib in GoLang, and the output does not create a header file (.h)

test.go:

package main

import "C"
import "fmt"

func ExportedFun(s string) {
    fmt.Printf("C gave us %s string", s)
}

func main() {}

and the command I run is:

go build -buildmode=c-shared -o test.so test.go

I get the .so file but no header file. Is there something I am missing?

like image 225
Daniel Toebe Avatar asked Jan 21 '16 19:01

Daniel Toebe


1 Answers

From the go command documentation:

The only callable symbols will be those functions exported using a cgo //export comment.

Th syntax for exporting a function via cgo can be found in the cgo documentation

Go functions can be exported for use by C code in the following way:

//export MyFunction
func MyFunction(arg1, arg2 int, arg3 string) int64 {...}

//export MyFunction2
func MyFunction2(arg1, arg2 int, arg3 string) (int64, *C.char) {...}

Marking your function as exported will generate the header.

like image 80
JimB Avatar answered Nov 11 '22 13:11

JimB