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
0 answers

How to tell cgo to mark a []byte for garbage collection?

I am calling the following C function from Go: char *my_read(int dd) { char *buf = malloc(sizeof(char) * BUF_SUZE); if (!buf) { return NULL; // cannot allocate memory } while (read(dd, buf, BUF_SIZE) < 0) { if…
Attila O.
  • 15,659
  • 11
  • 54
  • 84
0
votes
2 answers

Calling c function, order of "import fmt", "import C" is causing build error, why?

I have the following simple test go code : call_c.go package main /* int getData(int *p, int n ) { int i; for(i=0;i
tony-p-lee
  • 797
  • 1
  • 7
  • 13
0
votes
1 answer

Go causes OpenGL to segfault with time.Tick but not time.After

I have the following two files: bridge.go: package cube // #cgo LDFLAGS: -lGL -lGLEW -lglfw // #include // int init(GLFWwindow**); // void render(GLFWwindow*); import "C" import ( "fmt" "time" ) func Init() { var window…
Mediocre Gopher
  • 2,274
  • 1
  • 22
  • 39
0
votes
1 answer

golang cgo: libevent handler values are set to null during execution

i'm working on porting this C API in go https://github.com/shammash/vde3, the library has is own event loop that use libevent, i'm using CGO. the library require a full vde_event_handler that is composed this way {event_add = 0x7fffe4de0db0,…
kurojishi
  • 321
  • 1
  • 4
  • 12
0
votes
2 answers

How to access a variable in an union in a struct from the Windows API?

I got input.ki undefined (type C.INPUT has no field or method ki). I tried using the 'union_' prefix but without any luck. Any ideas? package main // #include // #include import "C" //…
bbigras
  • 1,311
  • 2
  • 17
  • 32
0
votes
1 answer

Troubles using glib through gtk in Go with gco

My understanding of C is quite poor. I can read the code, but I have no idea how to include/build/make/configure anything. This is probably why I do not manage to get the following Go code to compile. This code my attempt at adapting…
Niriel
  • 2,605
  • 3
  • 29
  • 36
0
votes
1 answer

cgo and pkg-config

I want to run GraphicsMagick with cgo. /* #cgo pkg-config: GraphicsMagick-config #include static int gm(int argc, char **argv) { int status; status = GMCommand(argc, argv); return 1-status; } */ then I run 'go install',…
SunRunAway
  • 305
  • 4
  • 10
-1
votes
0 answers

Goroutine leads to inconsistent accessing of X11 clipboard in Qt

I wrote a small C wrapper for Qt's QClipboard class so I can access it from my Go app. Part of my Go app repeatedly accesses the clipboard. On MacOS, regardless of whether the loop is in a goroutine or a part of the main thread, accessing is as fast…
DrownedSuccess
  • 123
  • 1
  • 8
-1
votes
1 answer

Go program loads a DLL compiled from Go code error

Go program loads a DLL compiled from Go code What should I do Thank you. go version 1.18 windows 10 Sometimes, after calling the operation, it can run normally for the first time. If it runs again, it will report an error, and then it will always…
Recar
  • 1
  • 2
-1
votes
1 answer

"cgo argument has Go pointer to Go pointer" when pass pointer from Struct to C method

I am using cgo to calling C method which accept struct pointer as below: package main /* typedef struct Client{ struct real_client c; } Client; int doSomething(struct real_client *c ) { .... } */ import "C" import "fmt" type Client…
kiwi
  • 67
  • 1
  • 1
  • 6
-1
votes
1 answer

Using a cgo shared library in a Go program

Trying to test cgo, so I wrote the below: //go:build lib // +build lib package main import "C" import "fmt" //export HelloWorld func HelloWorld() { fmt.Printf("hello world") } func main() {} // go build -tags lib -buildmode=c-shared -o…
Hasan A Yousef
  • 22,789
  • 24
  • 132
  • 203
-1
votes
1 answer

Why casting interface{} to cgo types is not allowed?

It's more of a technical question, not really an issue. Since we don't have variadic functions in cgo and there's currently no valid solution, I wonder if it'd be possible to cast interface{} to cgo types. So this would allow us to have more dynamic…
inertia
  • 9
  • 4
-1
votes
1 answer

C++ and Go header is Throwing an Error

Afer reading How To Use C++ In Go, I started to need threads in my c++ programs. Recently when i tried using it it gave me this compiler error: error: 'thread' was not declared in this scope thread parsed(parser, var["ExpAct"],…
Ank i zle
  • 2,089
  • 3
  • 14
  • 36
-1
votes
1 answer

Does Go support calling C++ function?

I want to call some c++ functions(which are specific for my c++ library ) in Go. But I am not sure, does Go support that? I've already tried that calling dll functions from go But it does not work for .so file Do I have to write a c wrapper for my…
Mehmet Özcan
  • 85
  • 1
  • 5
-1
votes
1 answer

Program changes memory values when using calloc() vs make() for slices

I am trying to build a slice of pointers manually and with C.calloc() for allocating the array portion of the slice. I am able to do this successfully though when I try and add pointers that I allocate with make() some of the values (of what the…
Noah
  • 1,647
  • 1
  • 9
  • 18