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
12
votes
2 answers

Override an external package's cgo compiler and linker flags?

Let's say I want to use some awesome go package. I can include it by: import "github.com/really-awesome/project/foobar" And inside that project's foobar.go file, it defines some cgo instructions like: #cgo windows CFLAGS: -I…
jCuga
  • 1,523
  • 3
  • 16
  • 28
12
votes
1 answer

Calling setns from Go returns EINVAL for mnt namespace

The C code works fine and correctly enters the namespace, but the Go code always seems to return EINVAL from the setns call to enter the mnt namespace. I've tried a number of permutations (including embedded C code with cgo and external .so) on Go…
Iain Lowe
  • 311
  • 3
  • 8
12
votes
2 answers

Is it possible to write C functions that modify structs of types defined in Go code?

This is a follow-up to this question. I made an assumption there that might not be true, which is why I am explicitly asking about it. Because I forgot to ask if this is actually possible, I have already filed issue #8114 about this. With cgo, it…
fuz
  • 88,405
  • 25
  • 200
  • 352
12
votes
1 answer

Is there a way to release unmanaged resources when a Go struct is collected?

I have a pointer to a C type wrapped by a Go struct, like so: type Wrapper struct { unmanaged *C.my_c_type } The C type, in turn, has the following functions: my_c_type* make_c_type(); void free_c_type(my_c_type *ct); Is there a way that I can…
TSL
  • 916
  • 6
  • 18
11
votes
1 answer

Garbage collection and cgo

Is it possible to make the garbage collector in Go handle and release memory allocated through C code? I apologize, I haven't used C and cgo before so my examples may need some clarification. Lets say you've got some C library that you'd like to use…
hannson
  • 4,465
  • 8
  • 38
  • 46
11
votes
4 answers

Cross compiling "Hello World" on Mac for Android

I'm trying to build a standard "Hello, World!" command-line executable for Android. The executable is to be run via adb shell. 0. The Go (Golang) Source package main import ( "fmt" ) func main() { fmt.Println("Hello, world!") } 1A. The…
11
votes
2 answers

How to use a relative path for LDFLAGS in golang

I am trying to build a golang program which uses a static lib (.a file) the directory struct for my project as below └─testserver ├─bin ├─pkg └─src ├─logging └─testserver ├─libtest.a └─test.go the…
catric mia
  • 1,137
  • 2
  • 9
  • 8
11
votes
3 answers

Pass struct and array of structs to C function from Go

Stuck with this problem. Able to get only the first member of passed structure... What I do wrong? And what is the right way to pass the structure from Go to C? This is my example of how it doesn't work: package main /* #include…
tacobot
  • 915
  • 2
  • 9
  • 15
11
votes
1 answer

Using OpenGL from Go

I am trying to use OpenGL from within a Go program. I think I have all of the pieces in place, but I am still not quite able to get it running. My C compiler is the 64-bit version of mingw. It is in my %PATH% variable, and I have verified it working…
Corey Larson
  • 1,577
  • 18
  • 39
10
votes
0 answers

SetEvent state from golang to c (cgo)

I wrote DLL to handle my VSCAN communication in golang. I'm facing a really hard problem now. Is there any way to signal state from cgo to c/cpp program? I'm using a large CPP project, inside it calls external golang DLL (compiled with GCC). Program…
Frnk Wdl
  • 146
  • 2
  • 11
10
votes
1 answer

How do I convert a Go array of strings to a C array of strings?

I am using cgo in a project, and I want to export a function for use. Here's an example of what I want to achieve: package csplit import ( "C" "strings" ) //export Split /* The Split function takes two C strings, the second of which…
1ijk
  • 1,417
  • 2
  • 19
  • 31
10
votes
3 answers

passing function pointer to the C code using cgo

Starting from Go v1.6 cgo changed the rules of passing pointers to the C code golang/go#12416. The example of invoking a dynamic Go callback from C code from the wiki doesn't work anymore. package main import ( "fmt" "unsafe" ) /* …
Iakov Davydov
  • 818
  • 2
  • 7
  • 21
10
votes
1 answer

Why Go use cgo on Windows for a simple File.Write?

Rewriting a simple program from C# to Go, I found the resulting executable 3 to 4 times slower. Expecialy the Go version use 3 to 4 times more CPU. It's surprising because the code does many I/O and is not supposed to consume significant amount of…
samonzeweb
  • 141
  • 6
10
votes
2 answers

go 1.5 cross compile using cgo on OS X to linux and windows

I'm having trouble compiling the git2go library on OS X to linux amd64 after upgrading go 1.4.2 to go 1.5. I think this is about cross compiling any go app that uses C code with go 1.5. Using CGO_ENABLED=1, I get: $ CGO_ENABLED=1 GOOS=linux…
Calin
  • 2,110
  • 1
  • 21
  • 36
10
votes
1 answer

CGO, how to pass NULL parameter to C function

Sometimes, a NULL pointer may be needed for a C API. is that possible in CGO? For example, I want to pass a null argument to strcmp() in a Go language program: package strutil /* #include #include */ import ( "C" …
tanbro
  • 101
  • 1
  • 4
1
2
3
56 57