Questions tagged [cgo]

Cgo enables the creation of Go packages that call C code.

Cgo enables the creation of Go packages that call C code.

If a Go source file imports "C", it is using cgo. The Go file will have access to anything appearing in the comment immediately preceding the line import "C", and will be linked against all other cgo comments in other Go files, and all C files included in the build process.

Note that there must be no blank lines in between the cgo comment and the import statement.

To access a symbol originating from the C side, use the package name C. That is, if you want to call the C function printf() from Go code, you write C.printf().

It is possible to call both top-level Go functions and function variables from C code invoked from Go code using cgo.

Go makes its functions available to C code through use of a special //export comment. Note: you can't define any C functions in preamble if you're using exports.

Source:https://github.com/golang/go/wiki/cgo

846 questions
5
votes
1 answer

Threaded CGO using Go 1.2

Edit: This question is moot. I misread the date on the commit, it is included in existing versions of the Go tools. Thanks, James! It seems that the upcoming release of Go (1.3) will allow non-Go threads to call CGO callbacks. I'm wondering what…
laslowh
  • 8,482
  • 5
  • 34
  • 45
5
votes
5 answers

Golang zmq binding, ZMQ4, returns package error not finding file zmq.h

I am trying to include ZMQ sockets in a Go app but both zmq4 and gozmq (the referred ZMQ binding libraries for Go) are giving me problems. I would like to understand why zmq4 specifically isn't importable on my system. I am running a Windows 8…
user2628946
  • 103
  • 2
  • 6
5
votes
1 answer

Is there a safe way to hold on to a reference to a Go variable from C code using CGo?

When using CGo to interface C code with Go, if I keep a reference to a Go variable on the C side, do I run the risk of that object being freed by the garbage collector or will the GC see the pointer in the variables managed by the C side? To…
James Henstridge
  • 42,244
  • 6
  • 132
  • 114
5
votes
1 answer

Use variadic C functions in Go

I use shm_open with cgo. shm_open is defined with 3 arguments on Linux int shm_open(const char *name, int oflag, mode_t mode); whereas on OSX (Darwin) the 3rd mode flag is optional. int shm_open(const char *name, int oflag, ...); This creates a…
user187676
5
votes
2 answers

Golang (cgo) - Support for nested structs with cgo?

I was trying to use cgo to write a little wrapper for the x264 library and came across a problem with nested structs. The library uses a lot of complicated structs where some of the fields are anonymous structs themselves. When trying to use cgo to…
Fredrik
  • 4,161
  • 9
  • 28
  • 31
5
votes
2 answers

How to convert from [][]byte to **char in go

I would like to convert from a go [][]byte to a C **char. In other words, I have a byte matrix in go that I would like to convert to a char double pointer in C. Please assume that I HAVE to have a [][]byte as input and a **char as output. I know it…
John Gilmore
  • 2,775
  • 1
  • 21
  • 18
4
votes
1 answer

execute a callback(via function ptr) from dll

I got a callback from a dll, the function ptr type defined: typedef int32_t (WINAPI *fn) (); if I use cgo, we can do like this: /* #include #include typedef int32_t (WINAPI *fn) (); int32_t call_callback(fn f){ …
lysS
  • 77
  • 3
4
votes
1 answer

Golang distroless Docker exec failed: No such file or directory when CGO is enabled

I was trying to get a minimal example go app running inside a docker container. But I kept getting exec /app: no such file or directory when running the container. I checked and double checked all my paths in the image where I built and copied my…
asturm
  • 69
  • 2
  • 9
4
votes
1 answer

Cgo: undefined reference to [C function]

I'm running a go program in a docker container (golang:1.18-bullseye). I haev tried running it both with go run main.go and go run . My code looks likes this, both header files are located in the Include directory given in the CFLAGS: /* #cgo…
Jessica Chambers
  • 1,246
  • 5
  • 28
  • 56
4
votes
1 answer

Calling C function pointer as callback from Go code

I try to call a C function pointer as a callback from a Go function but get a linker error I don't understand. package main /* typedef void (*int_int)(int, int); void bridge_int_int(int a, int b, int_int f){ f(a, b); } */ import "C" //export…
4
votes
1 answer

Go Build Error "build constraints exclude all Go files" when GOARCH=386 with import "C"

I'm using CGO package to import C code, and I want to build an x86 (386) windows version of it. I found out this should be done by setting GOARCH=386. It builds properly on my default environment settings (GOARCH=amd64), however, when I set the…
Bassem Ramzy
  • 326
  • 3
  • 10
4
votes
1 answer

Proper way to write to C memory from go

I would like to port a go program as a library by exporting some C APIs. In C it's common for a function to accept a buffer (pointer) as parameter (along with its size) and write the output to it. For example (from C standard library) size_t…
lewisxy
  • 137
  • 8
4
votes
1 answer

Is it possible from Go app to detect if CGo enabled?

My Go app can work with MySQL, Postgres, and SQLite. At the first start, it asks what DB should be used. SQLite works only with CGo. Depending on whether it is enabled or not, SQLite should be displayed in a list of supported databases. Is it…
FiftiN
  • 740
  • 1
  • 7
  • 27
4
votes
1 answer

Memory leak when calling C.malloc/C.free in goroutines

I'm seeing a constant memory leak when I look at RSS with the following trivial program: package main /* #include */ import "C" import ( "time" "unsafe" ) func loopStuff() { for { ptrs := make([]unsafe.Pointer,…
bearclaw
  • 91
  • 1
  • 2
4
votes
1 answer

cgo doesn't export functions in imported packages

I'm trying to build existed Go package into C shared library and header using CGO. I built the package with -buildmode c-shared as documented. -buildmode=c-shared Build the listed main package, plus all packages it imports, into a C shared…
phuctm97
  • 136
  • 1
  • 9