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

Golang cross compiling with CGO inside docker image

Requirement: An application has to be containerised as a docker image and needs to support arm64 and amd64 architectures. Codebase: It is a golang application that needs to make use of git2go library and must have CGO_ENABLED=1 to build the project.…
ravi kumar
  • 1,548
  • 1
  • 13
  • 47
9
votes
2 answers

Can I change default compiler used by cgo?

I am trying to execute cgo code under ubuntu 14.04, it seems like cgo assume CC/CXX to be gcc/g++. And I need to explicitly specify CC/CXX in order to use, say, clang. Can I configure default compiler used through go's build constraints? Thanks!
Xiaoqin Zhu
  • 101
  • 2
  • 4
9
votes
2 answers

cgo error unrecognized relocation (0x2a) in section `.text'

While build Go program with use of Cgo I get error /usr/lib/go/pkg/tool/linux_amd64/link: running gcc failed: exit status 1 /usr/bin/ld: /tmp/go-link-373355991/000002.o: unrecognized relocation (0x2a) in section `.text' /usr/bin/ld: final link…
Igor
  • 93
  • 1
  • 5
9
votes
1 answer

Why do I get "duplicate symbol reference" when including two cgo based libraries?

I'm trying to build (go build) the following: package main import ( "fmt" _ "github.com/d2g/unqlitego" _ "github.com/mattn/go-sqlite3" ) func main() { fmt.Println("Erm Compile?") } and I get an error: duplicate symbol reference:…
DanG
  • 323
  • 1
  • 9
9
votes
3 answers

Go/CGo - how do you use a C array passed as a pointer

I'm posting this as a question/answer, as it took me a while to work out, and I wouldn't mind some feedback on my solution. In Go/CGo, how do you work with a C array passed as a pointer? For example, with this C struct: struct _GNetSnmpVarBind { …
Sonia Hamilton
  • 4,229
  • 5
  • 35
  • 50
8
votes
2 answers

CGO ignores the LDFLAGS -L option on MacOS

I'm trying to compile the following code on a MacOS machine // #cgo darwin LDFLAGS: -L${SRCDIR}/build/darwin -lprocessing_lib // #cgo linux LDFLAGS: -L${SRCDIR}/build/linux -lprocessing_lib // #include "Processing-bridge.h" // #include…
Anuruddha
  • 3,187
  • 5
  • 31
  • 47
8
votes
1 answer

What does (*[1 << 30]C.YourType) do exactly in CGo?

In the Golang wiki, under "Turning C arrays into Go slices", there is a block of code that demonstrates how to create a Go slice backed by a C array. import "C" import "unsafe" ... var theCArray *C.YourType = C.getTheArray() length…
Some Noob Student
  • 14,186
  • 13
  • 65
  • 103
8
votes
3 answers

How to import and use CGO Brotli implementation from google?

I am trying to import and use cbrotli implementation from google as below: import ( "fmt" "io/ioutil" cbrotli "github.com/google/brotli/go/cbrotli" ) But I am getting the below error while trying to run the program: learn-go [master●●]…
Aditya Singh
  • 15,810
  • 15
  • 45
  • 67
8
votes
2 answers

Good or recommended way to translate C struct to Go struct

i'm using cgo for developing library binding from Go. Let me consider the C struct and Go Struct as below. struct cons_t { size_t type; cons_t *car; cons_t *cdr; }; cons_t* parse(const char *str); and this is go's struct type Cons struct { …
shinpei
  • 393
  • 1
  • 4
  • 15
8
votes
2 answers

go build doesn't find my C standard library when compiling cgo package

I'm trying to compile a go project in a raspberry pi. The project has 5 files, two small .c files and its counterparts .h (one of these files is my code -- it calls the other, which is a base64 library) and a .go files which calls my .c code using…
fiatjaf
  • 11,479
  • 5
  • 56
  • 72
8
votes
1 answer

Can I use c++ in cgo?

Is it possible to mix in some C++ code in cgo? I tried this: package main /* #include extern "C" void test(const char* str) { std::cout << str; } */ // #cgo CFLAGS: -x c++ // #cgo LDFLAGS: -lstdc++ import…
Chris_F
  • 4,991
  • 5
  • 33
  • 63
7
votes
1 answer

Golang fails to link an aarch64/arm64 binary on an x86_64 machine while cross compiling

I am trying to cross compile https://github.com/joohoi/acme-dns for an aarch64 machine on my x86_64 desktop. $ CC=aarch64-linux-gnu-gcc GOOS=linux GOARCH=arm64 CGO_ENABLED=1 go build -v -ldflags="-extld=$CC" #…
Adam Baxter
  • 1,907
  • 21
  • 41
7
votes
3 answers

VSCode import "C" fails on modules

Please see the following screenshot: It says: could not import C (no package data for import path C) I have attached an example project that reproduces the failure, here: https://github.com/microsoft/vscode/files/3783446/example-project.zip I am…
Ace
  • 463
  • 3
  • 6
  • 16
7
votes
1 answer

Calling function from Go in C#

I'm trying to make a .dll file from Golang to be used in a C# script. However, I can't make a simple example work. Here is my Go code: package main import ( "C" "fmt" ) func main() {} //export Test func Test(str *C.char) { …
user10400458
7
votes
2 answers

How do I pass an array of C.double's to a Cgo function?

I'm just getting started with CGo and I'm trying to send data to a C library that performs statistical computations on arrays of floats/doubles. What I'm trying to figure out right now is how to send an array of floats, or C.double's, to a CGo…
Jonathan Steele
  • 589
  • 6
  • 9
1 2
3
56 57