cgo

C macro definitions in Cgo

。_饼干妹妹 提交于 2019-12-23 15:59:15
问题 I want to use existing macro values from C code in go. Using import "C" pseudo package we can import enums defined in C. Is there a way to import C macros? package main /* enum levels { low1, medium, high }; #define CMA 30 typedef enum { LOW = 0, MEDIUM = 1, HIGH = 2 } security; */ import "C" import "fmt" func main() { cc := new(C.enum_levels) //cm := new(C._*someliteral*_CMA) fmt.Println(*cc) fmt.Println(C.MEDIUM) } 来源: https://stackoverflow.com/questions/44241836/c-macro-definitions-in-cgo

how to compile Cuda source with Go language's cgo?

社会主义新天地 提交于 2019-12-22 10:39:10
问题 I wrote a simple program in cuda-c and it works on eclipse nsight. This is source code: #include <iostream> #include <stdio.h> __global__ void add( int a,int b, int *c){ *c = a + b; } int main(void){ int c; int *dev_c; cudaMalloc((void**)&dev_c, sizeof(int)); add <<<1,1>>>(2,7,dev_c); cudaMemcpy(&c, dev_c, sizeof(int),cudaMemcpyDeviceToHost); printf("\n2+7= %d\n",c); cudaFree(dev_c); return 0; } Now I'm trying to use this code with Go language with cgo!!! So I wrote this new code: package

How to make stdcall from Go

二次信任 提交于 2019-12-22 10:24:10
问题 I have a pointer to a COM interface and would like to take the function pointers from its virtual table and make method calls. To do this I need to make stdcall method calls. In Go how do I make a call with convention stdcall or make a call with convention stdcall in cgo? 回答1: See "godoc syscall Proc" for instructions on how to call stdcall functions on windows. Be warned that *Proc.Call does allocate / deallocate memory, so, if you care about efficiency, you should use correspondent syscall

How to make stdcall from Go

。_饼干妹妹 提交于 2019-12-22 10:21:35
问题 I have a pointer to a COM interface and would like to take the function pointers from its virtual table and make method calls. To do this I need to make stdcall method calls. In Go how do I make a call with convention stdcall or make a call with convention stdcall in cgo? 回答1: See "godoc syscall Proc" for instructions on how to call stdcall functions on windows. Be warned that *Proc.Call does allocate / deallocate memory, so, if you care about efficiency, you should use correspondent syscall

Cannot access c variables in cgo

跟風遠走 提交于 2019-12-20 05:57:59
问题 I'm trying to access a c struct in cgo, but go this could not determine kind of name for C.utmpx utmpx is a c struct here is the go code: /* #include <stdio.h> #include <stdlib.h> #include <utmpx.h> #include <fcntl.h> #include <unistd.h> */ import "C" type record C.utmpx fd, err := os.Open(C._PATH_UTMPX) // this works fd, err := os.Open(C.UTMPX_FILE) // error In the utmpx.h file , there is #define _PATH_UTMPX "/var/run/utmpx" #define UTMPX_FILE _PATH_UTMPX I can use _PATH_UTMPX but get the

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

一世执手 提交于 2019-12-18 17:29:58
问题 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 dynamic linked application, I am stuck when I try to run the static compiler. I already have build other projects statically (even with CGO) without problems, but here gcc is not finding the oracle library. Maybe someone has a hint? Error during

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

夙愿已清 提交于 2019-12-17 08:25:10
问题 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: /Users/doug/projects/c/go-bridge/build/libgb.a // #include <junk.h> import "C" func Run() { fmt.Printf("Invoking c library...\n") C.x(10) fmt.Printf("Done\n") } However, it doesn't seem to work: /var/folders/.../bridge.a(bridge.cgo2.o)(__TEXT/__text): x: not defined This

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

自作多情 提交于 2019-12-17 08:25:09
问题 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: /Users/doug/projects/c/go-bridge/build/libgb.a // #include <junk.h> import "C" func Run() { fmt.Printf("Invoking c library...\n") C.x(10) fmt.Printf("Done\n") } However, it doesn't seem to work: /var/folders/.../bridge.a(bridge.cgo2.o)(__TEXT/__text): x: not defined This

Call Go function with string parameter from C?

心不动则不痛 提交于 2019-12-12 19:00:55
问题 I can call a Go function without parameters from C, per below. This compiles via go build and prints Hello from Golang main function! CFunction says: Hello World from CFunction! Hello from GoFunction! main.go package main //extern int CFunction(); import "C" import "fmt" func main() { fmt.Println("Hello from Golang main function!") //Calling a CFunction in order to have C call the GoFunction C.CFunction(); } //export GoFunction func GoFunction() { fmt.Println("Hello from GoFunction!") } file1

How to access a C pointer array from Golang

非 Y 不嫁゛ 提交于 2019-12-12 14:28:58
问题 I'm writing an app for the windows platform using FFmpeg and it's golang wrapper goav, but I'm having trouble understanding how to use the C pointers to gain access to an array. I'm trying to get the streams stored in the AVFormatContext class to use in go, and eventually add frames to a texture in OpenGl to make a video player with cool transitions. I think understanding how to cast and access the C data will make coding this a lot easier. I've stripped out all the relevant parts of the C