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
7
votes
3 answers

compile gopacket on windows 64bit

I am trying to use gopacket on my windows 10. I'm using it to sniff and inject packets directly to/from the NIC. I can easily compile and run my code with GOARCH=386 but can't in GOARCH=amd64. Worth noticing: I am NOT trying to cross-compile. I'm…
J. Dow
  • 351
  • 3
  • 5
7
votes
2 answers

Golang Cgo: panic: runtime error: cgo argument has Go pointer to Go pointer

I am working with a C library that, unlike below, I do not control. I need to pass to a C function a pointer to an array that also contains pointers. package main /* #include typedef int* pInt; void foo(pInt p[]) { …
Chris
  • 1,090
  • 2
  • 11
  • 22
7
votes
1 answer

cgo: How to pass struct array from c to go

The C part: struct Person {...} struct Person * get_team(int * n) The Go part: n := C.int(0) var team *C.struct_Person = C.get_team(&n) defer C.free(unsafe.Pointer(team)) I can get the first element of the array in this way. But how to get the…
crackcell
  • 293
  • 2
  • 8
7
votes
1 answer

Why won't my cross-compiled CGO binary run on the Raspberry Pi (Raspbian)?

When I compile the following snippet of code (playground link): package main /* #cgo LDFLAGS: -lbluetooth #include #include #include */ import "C" func main() { …
Attila O.
  • 15,659
  • 11
  • 54
  • 84
7
votes
2 answers

Conditional compilation in Go

I'm trying to write a Go wrapper using CGo for ENet. When I tried to compile my wrapper on a Mac the library was older and had a slightly different interface. 99% of the code is the same just a few C calls need to change. What is the best practice…
deft_code
  • 57,255
  • 29
  • 141
  • 224
6
votes
2 answers

How can I check whether my golang app uses boringcrypto instead of the native golang crypto?

Context: I was reading multiple articles about making my golang app FIPS compliant (in other words, making my app use boringcrypto instead of the native golang…
Kostya Linou
  • 113
  • 7
6
votes
0 answers

Build time slow with cgo dependencies

I have a Go program that uses the Qt wrapper library https://github.com/therecipe/qt. Unfortunately, the build time gets extremely high with it (assuming it's the go part) go build -i . // takes about 14 seconds go run . // takes about 8…
wasp256
  • 5,943
  • 12
  • 72
  • 119
6
votes
1 answer

How to distribute a Go module with C dependencies

I have a Go package—call it foo—I've built around some existing C code, and I'm trying to determine how best to distribute it. A bit of background... Here's a simplified version of my directory structure: foo/ |_ include/ |_
TayTay
  • 6,882
  • 4
  • 44
  • 65
6
votes
1 answer

pkg-config errors on windows compiling v8

I am trying to build ry/v8worker2 on windows. Everything goes fine until the point where I run go test which then complains about the pkg-config setup. This is the pkg-config file which is generated: Name: v8 Description: v8 Version: xxx Cflags:…
REA_ANDREW
  • 10,666
  • 8
  • 48
  • 71
6
votes
2 answers

Go compile returns duplicate symbols for architecture x86_64 error when I import 2 different packages which use C package via Cgo

Here is my code: package main import ( kusb "github.com/karalabe/usb" tusb "github.com/trezor/trezord-go/usb" ) func main() { kusb.Enumerate(0, 0) tusb.InitHIDAPI(nil) } When I compile (I'm using go mod to manage the packages), it…
vutran
  • 2,145
  • 21
  • 34
6
votes
1 answer

too few values in struct initializer when initialize C struct in golang

I have tried the following program, but it told me "too few values in struct initializer" when compiling it. package main /* #include struct Person { char *name; int age; int height; int weight; }; */ import "C" import…
Wyatt
  • 141
  • 1
  • 2
  • 9
6
votes
1 answer

Linking C++ with Go

I am trying to bind C++ OpenCV programs with Go. I am using the concept from How do you statically link a c library in go using cgo?[1]. My myimgcom.cpp file is: /** * @file SURF_FlannMatcher * @brief SURF detector + descriptor + FLANN Matcher *…
annjohn
  • 61
  • 3
6
votes
1 answer

How to add C files in a subdirectory as part of go build by using pseudo CGO directives?

Per docs, go build with cgo will add any C/C++ files in the root of the package as part of the compilation. Is there a way to make C/C++ files in a given subdirectory to also be part of the compilation as well as the ones in the root by using CGO…
oblitum
  • 11,380
  • 6
  • 54
  • 120
6
votes
3 answers

How to use std::vector or other container in cgo of golang?

I want to malloc large number of objects in to memory.(about 100 million objects) because the gc of golang is not effective enough,so i need to use c/c++ to malloc memory and use std::vector to hold objects. this is my code,i want use std container…
习明昊
  • 621
  • 3
  • 7
  • 11
6
votes
2 answers

How to convert [1024]C.char to [1024]byte

How do I convert this C (array) type: char my_buf[BUF_SIZE]; to this Go (array) type: type buffer [C.BUF_SIZE]byte ? Trying to do an interface conversion gives me this error: cannot convert (*_Cvar_my_buf) (type [1024]C.char) to type [1024]byte
Attila O.
  • 15,659
  • 11
  • 54
  • 84