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
152
votes
24 answers

exec: "gcc": executable file not found in %PATH% when trying go build

I am using Windows 10. When I tried to build Chaincode it reported this error # github.com/hyperledger/fabric/vendor/github.com/miekg/pkcs11 exec: "gcc": executable file not found in %PATH% My chaincode imports: import ( "fmt" "strconv" …
jaswanth
  • 1,759
  • 2
  • 10
  • 16
50
votes
4 answers

How do you statically link a c library in go using cgo?

So there's a bunch of stuff on the group that suggests you can do this in go (although not on the cgo documentation): package bridge import "fmt" // #cgo CFLAGS: -I/Users/doug/projects/c/go-bridge/include // #cgo LDFLAGS:…
Doug
  • 32,844
  • 38
  • 166
  • 222
24
votes
6 answers

Cgo: sorry, unimplemented: 64-bit mode not compiled in

I'm currently trying to add some C code to my Go project. nothing fancy /* #include void test() { printf("hooola") } */ import ( "C" ) func MessageBox() { C.test() } However this will return cc1.exe: sorry, unimplemented:…
Raggaer
  • 3,244
  • 8
  • 36
  • 67
24
votes
3 answers

DLL-linking via Windows cgo->gcc->ld gives "undefined-reference-to-(function)" errors

(Very detailed problem report -- tl;dr at the bottom!) I really prefer GLFW to Glut and want to get its Golang binding working under Windows 64-bit with Go 1.0.1 64-bit. Under Linux, the binding it works flawlessly. This is in principle doable under…
metaleap
  • 2,132
  • 2
  • 22
  • 40
21
votes
2 answers

Why is compiling with CGO_ENABLED=0 slower?

When writing programs which utilize network, you can see quite noticeable slowdown of compilation with CGO_ENABLED=0. For example, the simplest HTTP server: package main import ( "flag" "fmt" "log" "net/http" ) func handler(w…
Wojciech Kaczmarek
  • 2,173
  • 4
  • 23
  • 40
21
votes
4 answers

Why cgo's performance is so slow? is there something wrong with my testing code?

I'm doing a test: compare excecution times of cgo and pure Go functions run 100 million times each. The cgo function takes longer time compared to the Golang function, and I am confused with this result. My testing code is: package main import ( …
习明昊
  • 621
  • 3
  • 7
  • 11
20
votes
3 answers

Compile cgo lib on Cygwin64: "ld: cannot find -lmingw32"

I'm trying to use a cgo library on Windows, namely github.com/mattn/go-sqlite3 I use Cygwin64 and installed with all "Development" packages, so gcc is availabe. But running go get github.com/mattn/go-sqlite3 results…
Fabian Schmengler
  • 24,155
  • 9
  • 79
  • 111
16
votes
2 answers

Convert Go []byte to a C *char

I have a byte.Buffer that I pack with data using the binary.Write() function. I then need to send this byte array to a C function. Using Go 1.6 I have not been successful at figuring this out. buf := new(bytes.Buffer) //create my…
dangeroushobo
  • 1,291
  • 2
  • 16
  • 28
16
votes
2 answers

Is it possible to use environment variables in a cgo CFLAGS comment?

I'm attempting to write some C bindings for the Go language, and have run into a sort of sticky situation when setting up the Cgo compilation in Windows. I have code that looks like the following: // #cgo windows CFLAGS: -I…
Chris Covert
  • 2,684
  • 3
  • 24
  • 31
15
votes
1 answer

How does cgo handle signals?

I ran into a problem where the Go program uses a .so lib, and the c code needs to deal with a signal SIGALRM. But it seems that once a signal SIGALRM is released, the program crashes. From "The Go Programming Language" I see this: If the non-Go…
Sy.Z
  • 151
  • 4
15
votes
2 answers

How do I copy a Go string to a C char * via CGO in golang?

I want to copy a Go string into a char * via CGO. Am I allowed to do this something like this? func copy_string(cstr *C.char) { str := "foo" C.GoString(cstr) = str }
steve landiss
  • 1,833
  • 3
  • 19
  • 30
14
votes
1 answer

From []byte to char*

I want to wrap a C function that takes a char* pointing to (the first element of) a non-empty buffer of bytes. I'm trying to wrap that in a Go function using CGo so that I can pass it a []byte, but I don't know how to do the conversion. A simplified…
Fred Foo
  • 355,277
  • 75
  • 744
  • 836
13
votes
1 answer

Calling Go from Python

I tried to run a simple in a go script from python and I got a segmentation fault. Here is my code: main.go package main import ( /* typedef struct foo{ int a; int b; int c; int d; int e; int f; } foo; */ "C" ) func main() {} //export…
Vince
  • 133
  • 1
  • 1
  • 5
13
votes
4 answers

Is it possible to zero a Golang string's memory "safely"?

Recently I've been setting up libsodium in one of my projects by using cgo, in order to use the crypto_pwhash_str and crypto_pwhash_str_verify functions. This has all gone very smoothly and I now have a small collection of functions that receive a…
Lucas
  • 10,476
  • 7
  • 39
  • 40
12
votes
1 answer

Golang + cgo - AppDelegate implementation doesn't work

I want to code an app in go that is able to open a custom filetype (.slc) on MacOS. I created a blank xcode project to get all the necessary code and implemented it via cgo into my app. When I double click a file the app opens but complains that it…
Crack_David
  • 2,775
  • 1
  • 14
  • 30
1
2 3
56 57