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
6
votes
1 answer

Having issues in linking objective-c with golang 1.1 - was working with golang 1.1beta

Need help with linking objective-c in go. Following objective-c code was working with golang 1.1beta but now it doesn't work with the latest go 1.1 release. /* #cgo CFLAGS: -x objective-c #cgo LDFLAGS: -framework Cocoa #import…
vorak
  • 221
  • 2
  • 6
6
votes
2 answers

go + cgo and linking

i want to use the following c as Go's cgo: #include main() { XScreenSaverInfo *info = XScreenSaverAllocInfo(); Display *display = XOpenDisplay(0); XScreenSaverQueryInfo(display, DefaultRootWindow(display),…
brian_j
  • 61
  • 1
  • 5
6
votes
2 answers

Is is possible to build Android games in Go using the NDK with cgo and/or SWIG or similar?

Is it possible to use Go to build Android games at all? I'm not wedded to the technologies mentioned in the subject line. I know that some people have built some Android programs in Go, but they may have been headless.
Curyous
  • 8,716
  • 15
  • 58
  • 83
5
votes
1 answer

standard_init_linux.go:228: exec user process caused: no such file or directory on CGO project

I have a Go service that references a C library, and I am getting the following error when trying to run my docker image: standard_init_linux.go:228: exec user process caused: no such file or directory This is not a duplicate of…
robbieperry22
  • 1,753
  • 1
  • 18
  • 49
5
votes
1 answer

CGO known implementation bug in pointer checks

According to the documentation of CGO (https://pkg.go.dev/cmd/cgo), there is a known bug in the implementation: Note: the current implementation has a bug. While Go code is permitted to write nil or a C pointer (but not a Go pointer) to C memory,…
antnio96
  • 53
  • 3
5
votes
1 answer

Go/Cgo: produce static library without definitions of Go runtime functions

Is there a way to produce a C static library from Go code, but without Go runtime function definitions? Rationale: Project A creates a C static library with go build -buildmode=c-archive, libA.a . Works well: project B uses pure C and is able to…
Alexander
  • 2,761
  • 1
  • 28
  • 33
5
votes
2 answers

Sanitizing memory management in Go + SWIG + C++ code

I'm using a C++ library from Go via SWIG. SWIG does not take care of memory management, so the Go side looks something like this: f := NewFoo() defer DeleteFoo(f) It's easy enough to call DeleteFoo(f) when I created f, but it's easy to omit it for…
Daniel Darabos
  • 26,991
  • 10
  • 102
  • 114
5
votes
0 answers

call go from c++ using swig

I am using swig to link c++ with go, but I want to use go functions in my c++ code. I have previously used cgo, and know something like this would work: //bind.h extern void GoFunc(*C.char); void CFunc(); ////////////////// //bind.cc void CFunc()…
Ank i zle
  • 2,089
  • 3
  • 14
  • 36
5
votes
0 answers

Attempt to use dylib dynamic library in cgo fails, report “dyld: Library not loaded”

I tried to import the dylib dynamic library in cgo, but it failed. Here is my code. package main //#cgo CFLAGS: -I./yun2txt/include //#cgo LDFLAGS: -L./yun2txt/lib -ltotxt // //#include import "C" func main() { C.hello() } There…
李逍遥
  • 51
  • 1
5
votes
4 answers

go/pkg/tool/linux_amd64/link: running gcc failed: exit status 1 /usr/bin/ld: cannot find -lgdal

I am trying to run go build on my sources. go/pkg/tool/linux_amd64/link: running gcc failed: exit status 1 /usr/bin/ld: cannot find -lgdal collect2: error: ld returned 1 exit status My LD_LIBRARY_PATH variable contains /home/fzd/project/lib64, the…
fzd
  • 765
  • 1
  • 6
  • 19
5
votes
1 answer

How to access C bitfield in Go

I have a struct like so: typedef struct st_MASK_SETTINGS { uint32_t foo : 1; uint32_t bar : 7; } MASK_SETTINGS Now through cgo I would like to access foo - but cannot find any documentation how to do so. Naive v := ms.foo complains has no…
abergmeier
  • 13,224
  • 13
  • 64
  • 120
5
votes
1 answer

Golang: multiple definition of CGO ported package

I have 2 project, the first, name as A, there is a submodule a imported sqlite3(github.com/mattn/go-sqlite3). Another B project import A's submodule a, and in another submodule b, it also import the same sqlite3. Both A and B put there imports under…
coanor
  • 3,746
  • 4
  • 50
  • 67
5
votes
1 answer

How to access global variable in cgo?

The memory of the structure is already allocated. I would like to approach C struct in golang. I want to access a struct variable in golang without the c code, what should I do? package main /* #include #include #include…
botob
  • 81
  • 4
5
votes
1 answer

golang struct with C struct in CGO

I will use cgo to wrap one c library as go library for the project usage. I read the document, it seems there are lots of rules while using cgo. I don't know whether this is legal or not. Both LibCtx and Client is a struct in C. Is this a legal way…
bpl
  • 51
  • 1
  • 2
5
votes
1 answer

cgo how to suppress warnings

I have a cgo application that uses a C library. During the build process, the compiler shows some warnigns: In file included from ./libsolv-sys/src/qsort_r.c:40:0, from ./libsolv-sys/src/util.c:181, from…
meruminal
  • 73
  • 5