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

Calling a C function with a double pointer output parameter using CGo

I am trying to figure out th right way to call this function: size_t fz_buffer_storage(fz_context *ctx, fz_buffer *buf, unsigned char **datap) { if (datap) *datap = (buf ? buf->data : NULL); return (buf ? buf->len : 0); } using CGo…
Mihai Todor
  • 8,014
  • 9
  • 49
  • 86
5
votes
0 answers

How do I include one header file in multiple .go files?

I've got one header foo.h and two source files main.go and bar.go. foo.hcontains two functions: foo(), bar() In main.go I am calling C.foo(), in bar.go I am calling C.bar(). Both import pseudo-package "C" and #include "foo.h" Although I added…
5
votes
0 answers

size_t in Go without using cgo

This is a C struct that I want to port into Go struct: struct InputBuffer_t { char* buffer; size_t buffer_length; ssize_t input_length; }; But there is a way to declare buffer_length variable without using C.size_t cgo pointer in Go. This…
Shajal Ahamed
  • 141
  • 2
  • 16
5
votes
0 answers

How to wrap a library(with c++11 feature) in CGO

My OS is Mac and the compiler is clang I want to wrap a C++ library (openfst , which uses C++11 feature ) in Go I follow the method of Scott Wales in How to use C++ in Go? , and the demo given by him works well( and no need to write…
chao.yang
  • 51
  • 1
  • 3
5
votes
0 answers

Any method to catch a fatal error triggered within cgo code?

Our applications are using the odbc driver to access an Impala database. We've discovered that in certain difficult-to-replicate situations, the driver will trigger a segfault within its cgo code, which manifests as a fatal error once it propagates…
Kaedys
  • 9,600
  • 1
  • 33
  • 40
5
votes
2 answers

How can I get syntax highlighting for cgo (golang)

cgo is written in "comments" in go which means by default it's given comment syntax highlighting. It would be nice have proper golang and C syntax highlighting but within cgo files. package main // ... C code or #include here ... import "C" ... Go…
Graham
  • 13,165
  • 2
  • 16
  • 14
5
votes
1 answer

How to pass pointer to slice to C function in go

Background: using cgo to call C functions from Golang. I want to use a C function which has this signature: int f(int *count, char ***strs). It will modify the data of count and strs, which is the reason why it uses pointer to them. The value of…
renyuneyun
  • 608
  • 5
  • 18
5
votes
2 answers

Call Go function with string parameter from C?

I can call a Go function without parameters from C, per below. This compiles via go build and prints Hello from Golang main function! CFunction says: Hello World from CFunction! Hello from GoFunction! main.go package main //extern int…
Joe Perks
  • 63
  • 1
  • 6
5
votes
1 answer

How can I fill out void* C pointer in Go?

I am trying to interface with some C code from Go. Using cgo, this has been relatively straight-forward until I hit this (fairly common) case: needing to pass a pointer to a structure that itself contains a pointer to some data. I cannot seem to…
Richard Wilkes
  • 309
  • 2
  • 10
5
votes
1 answer

Pass Go function to C as callback

Suppose I have a C library with code like this: typedef int (callback_t)(int); void register_callback(callback_t cb); I want to write go bindings for this function and pass arbitrary go callbacks. I found an excellent answer here. However, it is a…
kaspersky
  • 3,959
  • 4
  • 33
  • 50
5
votes
1 answer

Cross-compilation of cgo (for darwin) fails

I am fairly new to go and even Linux in general. I have built an app in a Linux environment which makes use of a gtk lib based on cgo (https://github.com/mattn/go-gtk/). The application builds fine in its native environment (linux 64bit) but when I…
Nicolas
  • 403
  • 1
  • 6
  • 18
5
votes
2 answers

Cgo: can't find way to use callbacks with const char* argument

I'm using a C library from Go using Cgo, and all is good except for callbacks. The library has a callback setter, which takes a pointer to the callback function. The callback function is written in Go and exported using Cgo syntax. Problem: I can…
sisoft
  • 953
  • 10
  • 22
5
votes
1 answer

linking 3rd party static libs in cgo library

I have a Go library that provides bindings for the C++ OpenImageIO library (OpenImageiGO). I've been happily building my bindings via the standard dynamic linking to libOpenImageIO, but am now trying to link statically. I'm running into an issue…
jdi
  • 90,542
  • 19
  • 167
  • 203
5
votes
2 answers

Troubles cross compiling with cgo enabled using go 1.3 on Mac OS X

I tried to cross compile cgo code to linux/arm on my darwin/amd64 MBP with Go 1.3, but ended with no luck :( My environment: go version go1.3 darwin/amd64 My goal: Cross compiling cgo code targeting linux/arm. Step 1: Building gcc/g++ cross compiler…
Kevin Yuan
  • 1,008
  • 8
  • 20
5
votes
1 answer

Marshalling C objects that cannot be accessed from Go

There are some C objects like unions, structs that contain bitfields and structs whose alignment differs from Go's ABI, that cannot be accessed from Go. Some of these structures cannot be changed to be accessible from Go code as they are part of the…
fuz
  • 88,405
  • 25
  • 200
  • 352