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

Trying to build static CGO executable with oracle libraries on Linux/Ubuntu

I have already searched for some days, tried several suggestions but none helped. At the moment I just want to create a small Go snippet which connects to an Oracle Database. While everything works by using normal go build and invoking the resulting…
hanneslehmann
  • 133
  • 10
4
votes
2 answers

Go: passing argv to C function

Since Go 1.6 this code: argc := len(*argv) c_argc := C.int(argc) c_argv := make([]*C.char, argc) for index, value := range *argv { c_argv[index] = C.CString(value) defer C.free(unsafe.Pointer(c_argv[index])) } err := C.MPI_Init(&c_argc,…
Eddynand Fuchs
  • 344
  • 1
  • 13
4
votes
1 answer

cgo **char to slice string

I have developed a pam module using the cgo. can not be converted to []string a ** char func pam_sm_authenticate(pamh *C.pam_handle_t, flags C.int, argc C.int, argv **C.char) int { fmt.Println(C.GoString(*argv[0])) return 0 } error…
4
votes
1 answer

Is there a way to include multiple c-archive packages in a single binary

I'm trying to include multiple Go c-archive packages in a single C binary, but I'm getting multiple definition errors due to the full runtime being included in each c-archive. I've tried putting multiple packages in the same c-archive but go build…
Per Johansson
  • 6,697
  • 27
  • 34
4
votes
0 answers

Invocation static vars from C++ on cgo

I have a static var that calls to a constructor in a library in C++, something like this: class ClassToBuild { public: ClassToBuild() { // Some cool stuff happens here... } }; static ClassToBuild classToBuild; The problem is that when this…
4
votes
1 answer

How to wrap a C "hello world" with argv using cgo?

Consider the following hello.c: #include int main(int argc, char* argv[]) { printf("Hello, world! argv[0]=%s\n", argv[0]); } Makefile only contains one line: all: hello. How can I wrap (and compile) this function around a Go program,…
d33tah
  • 10,999
  • 13
  • 68
  • 158
4
votes
1 answer

Cgo: can't set callback in C-struct from Go

I have a struct in C lib with some callbacks defined inside it. Problem that Go treat this fields as *[0]byte array type and I can't set it to pointer: ./test.go:16: cannot use _Cgo_ptr(_Cfpvar_fp_cb_func) (type unsafe.Pointer) as type *[0]byte in…
sisoft
  • 953
  • 10
  • 22
4
votes
1 answer

Unable to include required C header files for go program

I am trying to include a header file which exists in /usr/local/WordNet-3.0/include/ in my go program using these flags // #cgo CFLAGS: -I/usr/local/WordNet-3.0/include // #cgo LDFLAGS: /usr/local/WordNet-3.0/lib/libWN.3.dylib /* #include…
Sachin Anand
  • 428
  • 4
  • 10
4
votes
1 answer

Cgo + windows + mingw-w64 : "sorry, unimplemented: 64-bit mode not compiled in"

While trying to install suapapa/go_sass on windows 8.1 with the command : go get github.com/suapapa/go_sass I came across this error : cc1.exe: sorry, unimplemented: 64-bit mode not compiled in According to stackoverflow and other forums, this…
Nicolas Marshall
  • 4,186
  • 9
  • 36
  • 54
4
votes
1 answer

How to use external .c files with CGO?

To write some C code in a comment above import "C" is straightforward: // foo.go package main /* int fortytwo() { return 42; } */ import "C" import "fmt" func main() { fmt.Printf("forty-two == %d\n", C.fortytwo()) …
Jacob Marble
  • 28,555
  • 22
  • 67
  • 78
4
votes
1 answer

CGO undefined reference in included files

Wraping up OpenJtalk in Go, files are successfully included and types are referenced without an issue, but functions trigger an undefined reference error. jtalk.go: package main // #cgo CFLAGS: -I/home/vagrant/open_jtalk/njd [...etc] /* #include…
localhost
  • 845
  • 3
  • 14
  • 32
4
votes
2 answers

integer division in Go called from C

I am able to perform integer division in go by this program : package main import "fmt" func main() { a := 10 b := 5 fmt.Println(a/b) } Then I made a program in go that has functions for +, -, * and /. and I made a program in C that calls…
Ridhima
  • 308
  • 1
  • 3
  • 14
4
votes
2 answers

Cross compile CGO app on darwin for linux

I'm having issues trying to cross-compile a Go app on OS X to run on linux/amd64. The app in question is using libvips via this vips go package. As such, it is using CGO and needs to be compiled with CGO support. I'm on Go 1.4 and running the…
cpjolicoeur
  • 12,766
  • 7
  • 48
  • 59
4
votes
1 answer

cgo : Undefined symbols for architecture x86_64

I would like to call the go func from the C function space, but the program throws the build error. example.go package main /* #include "test.c" */ import "C" import "fmt" func Example() { fmt.Println("this is go") …
subhash kumar singh
  • 2,716
  • 8
  • 31
  • 43
4
votes
1 answer

How to bind Go functions to C calls without using cgo?

In a project I'm working on I need to use a bunch of C functions from Go. The current implementation is using cgo to achieve that, but doing so has a massive performance impact, which I am trying to remove. cgo marks all C code as a syscall,…
Tom van der Woerdt
  • 29,532
  • 7
  • 72
  • 105