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

What package should I install instead of libpcre++-dev to use C code in Alpine Golang?

I have a Golang program inside a docker container (I use Ubuntu 18). Also I use github.com/glenn-brown/golang-pkg-pcre/src/pkg/pcre for regex in my Golang app. Before using this library I should install libpcre++-dev this way: sudo apt-get install…
Klimbo
  • 1,308
  • 3
  • 15
  • 34
4
votes
1 answer

How to convert *_Ctype_char to *_Ctype_uchar

I'm using cgo to call a function in a dynamic library whose signature looks like this: int decompress(int, const uint8_t *, size_t, uint8_t *, size_t); Here is my go code: // #include statements here import "C" import ( "unsafe" ) func…
Peter Wang
  • 1,808
  • 1
  • 11
  • 28
4
votes
1 answer

warning: unused variable ‘_cgo_a’

What is the '_cgo_a' variable? I'm trying to link a c++ static lib. greeter.cpp #include "greeter.h" #include void greet() { std::cout << "Greetings\n"; } greeter.h #ifndef GREETER_H_ #define GREETER_H_ #ifdef __cplusplus extern…
Adham Zahran
  • 1,973
  • 2
  • 18
  • 35
4
votes
2 answers

Building a small Go program for MIPS with no hardware floating point (softfloat required)

I have small program written in go that I'm trying to cross compile to get working on a MIPS architecture machine. I've been trying to cross compile using the XGO cross compilation library but have had limited success getting my program to run…
MattLock
  • 174
  • 3
  • 17
4
votes
1 answer

How to use identical C types from different packages?

I'm trying to use cairo bindings (Go package), which defines a wrapper structure with a C type in it, with a C function, but can't make it work. The cairo package defines a Context: package cairo ... type Context struct { Ptr…
Jan Synáček
  • 355
  • 2
  • 9
4
votes
1 answer

Goroutines (cgo): unexplained OS thread spawns when using goroutines

I am using go to parallelize 2d convolutions where the convolution (implemented in go) is happening in a c-archive included in a C binary (where the go code is called). No calls are made from the go code to any c function Before spawning goroutines,…
Thor
  • 459
  • 4
  • 15
4
votes
1 answer

Is it safe to convert *C.uint8_t to []uint8?

I'm trying to pass uint8_t array from C code to Go, then read some bytes from file and store them into this array in Go code. Sample code is here. main.c : #define BUFFER_SIZE 16384 int read_go(uint8_t* buffer, int bufferSize); void read_buf() { …
Hoon
  • 133
  • 1
  • 5
4
votes
1 answer

proper way to change *C.char from go

I am new with go and cgo and after browsing the internet for some time I have not figured out a good and fast way to change a char* from go. What is the fastest way to change *C.char from go& Here is my code and my attempt to change the string(It…
4
votes
1 answer

How to access a C pointer array from Golang

I'm writing an app for the windows platform using FFmpeg and it's golang wrapper goav, but I'm having trouble understanding how to use the C pointers to gain access to an array. I'm trying to get the streams stored in the AVFormatContext class to…
nevernew
  • 650
  • 10
  • 23
4
votes
1 answer

How to pass GoLang's struct's method as C callback

In Go source I have type T struct { // some data } func (t *T)M(arg0 SomeType1) { // some computations } var Obj *T In C sources I have // SomeType1C is equivalent to SomeType1. typedef void (*CallbackFunc)(SomeType1C); // callback will…
Unsacrificed
  • 189
  • 8
4
votes
1 answer

How to solve "bad pointer in write barrier" panic in cgo when C library uses opaque struct pointers

I'm currently writing a Go wrapper around a C library. That C library uses opaque struct pointers to hide information across the interface. However, the underlying implementation stores size_t values in there. This leads to runtime errors in the…
Johannes P
  • 888
  • 8
  • 16
4
votes
1 answer

How do I release the memory allocated by C.CString?

Here is my code: helloworld.go: package main import "C" import "unsafe" //export HelloWorld func HelloWorld() *C.char { cs := C.CString("Hello World!") C.free(unsafe.Pointer(cs)) return cs } func main() {} One of the errors I…
jnbdz
  • 4,863
  • 9
  • 51
  • 93
4
votes
1 answer

how to swig omit forward declaration

I am trying to run swig on vendor header so I do not need to redefine the implementation in a separate header file swig -go -cgo -intgosize 64 -module qt -o $WORK/qt/_obj/qt_wrap.cxx -outdir $WORK/qt/_obj/ \ -I$HOME/Qt5.9.1/5.9.1/gcc_64/include…
user642318
  • 387
  • 1
  • 6
  • 17
4
votes
1 answer

cgo how to set a C union value

Given I have a C structure as below. I am able to read the data union values using unsafe pointers but am unable to work out how I can set the union value data? typedef struct val { char *var1; type type; union { char…
Westy10101
  • 861
  • 2
  • 12
  • 25
4
votes
2 answers

Exporting functions with C type as a parameter [cannot use x (type *C.ctype) as type *package.C.ctype in argument to package.Func]

Library Code (Simplified Version): // package1.go package package1 import "C" func Play(s *C.char) { } Client Code: // main.go package main import "C" import ( "path/to/package1" ) func PlayMore(s *C.char) { package1.Play(s) } func…