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
0
votes
1 answer

casting a cgo array into a slice

At the moment I do this for casting a CGO array of doubles into a slice of float64: doubleSlc := [6]C.double{} // Fill doubleSlc floatSlc := []float64{float64(doubleSlc[0]), float64(doubleSlc[1]), float64(doubleSlc[2]), …
prl900
  • 4,029
  • 4
  • 33
  • 40
0
votes
0 answers

CGo: Unexpected Signal During Runtime

I am attempting to write a Go function that sends a string to a C function as a C.CString. func AssertString(fact string) { // sbytes := []byte(fact) // ccp := (*C.char)(unsafe.Pointer(&sbytes[0])) ccp := C.CString(fact) …
wmaxlees
  • 605
  • 1
  • 5
  • 21
0
votes
2 answers

Define variable in cgo

I want, at build time, to define a string variable in cgo. None of the following approaches works. #cgo CFLAGS: -DLOG="common" 'common' undeclared (first use in this function) #cgo CFLAGS: -DLOG=common 'common' undeclared (first use in this…
user2424276
  • 591
  • 1
  • 5
  • 24
0
votes
1 answer

Assembler used by golang when building with and without cgo

Let's say I have a golang package, which contains some assembly code: demopkg/ source1.go source2.go asm_amd64.s If I try to build it using go build, toolchain will use go tool asm to assemble the *.s files. But if I add Cgo to the…
bgeyts668
  • 166
  • 1
  • 5
0
votes
1 answer

sigsuspend does not return when being called by a golang program via cgo

I am working on a project with golang. The project calls C API of LSF (A job scheduler https://en.wikipedia.org/wiki/Platform_LSF). Some of the APIs call sigsuspend() when communicating with LSF system. During test, I found those APIs never return.…
Yong Feng
  • 81
  • 3
0
votes
1 answer

passing string to win32 function with cgo

I try this name := C.CString("vds") C.OpenService(scm, (name), C.DWORD(C.SC_MANAGER_ALL_ACCESS)) but it wont compile .\test.go:28: cannot use name (type *C.char) as type *C.CHAR in argument to _Cfunc_OpenService I tried looking for similar things…
pm100
  • 48,078
  • 23
  • 82
  • 145
0
votes
1 answer

Go cgo ldap_init could not determine kind of name for C.ldap_init

I was trying to learn and understand cgo. I am writing a program in Go where I connect to AD and parse the output. I tested the code in C, which is working fine as expected. The relevant part in C is char *ldap_host = "x.x.x.x"; int ldap_port …
scott
  • 1,557
  • 3
  • 15
  • 31
0
votes
1 answer

CGo: how to pass a 2 dimensional slice to C function

My code is: package main /* #include…
CobbLiu
  • 447
  • 1
  • 7
  • 10
0
votes
1 answer

idiomatic way to use c style iterator with Go

I'm very new to Go programming (3-4 days), and I'm trying to write some code that reads a binary file using an existing third-party C library using cgo. The C library's way of doing this seems fairly standard (for C). Slightly simplified it looks…
jjellis
  • 33
  • 4
0
votes
1 answer

Compiling error with cgo: iostream:38:28: fatal error: bits/c++config.h: No such file or directory

I tried to execute the saxpy exemple from the official cuda nvrtc guide in this page It works fine with the g++ compiler on terminal and following the build instructions: g++ saxpy.cpp -o saxpy -I $CUDA_PATH/include -L $CUDA_PATH/lib64 -lnvrtc…
0
votes
1 answer

passing a byte array from go to cgo

I have the following C code uint32_t cHash32(const char *s, size_t len) { return util::Hash32(s, len); } I am calling it from a go project as follows func Hash32(s []byte) uint32 { return uint32(C.cHash32((*C.char)(unsafe.Pointer(&s)), …
Seif Lotfy
  • 109
  • 2
  • 5
0
votes
1 answer

How to construct C struct in Go side?

I need to call a C function which needs a pointer of struct as argument. Here's the C code: struct Position { uint64_t index; uint64_t offset; }; int read(const char* filename, const Position* pos, const char** data) So in Go code, I think…
Meng
  • 747
  • 1
  • 6
  • 5
0
votes
1 answer

What is the best (safest + most performant) way of getting a specific slice of bytes from an unsafe.Pointer

I'm trying to convert this c++ to go. This is in short what the c code is doing: static const char *pSharedMem = NULL; int sessionInfoOffset; return pSharedMem + pHeader->sessionInfoOffset; This is my (pseudo) go code: var pSharedMem…
Leon
  • 841
  • 3
  • 10
  • 21
0
votes
1 answer

Mac cross compiling (or alternatives) with golang

Ok so I'm trying to compile my Awesomium go wrapper on my mac (everything works fine on linux). My problem is, when I try to compile I get ld: warning: ignoring file /Library/Frameworks//Awesomium.framework/Awesomium, file was built for i386 which…
user2475269
0
votes
1 answer

Go Exception "signal arrived during cgo execution"

In what circumstances Go panics like "signal arrived during cgo execution" when calling a dll? The code to be called is - based on samples in zsyscall_windows.go in src of go distribution: var ( // entry names found using dumpbin /exports …
Kaveh Shahbazian
  • 13,088
  • 13
  • 80
  • 139