cgo

How to add C files in a subdirectory as part of go build by using pseudo CGO directives?

只谈情不闲聊 提交于 2019-12-12 12:08:32
问题 Per docs, go build with cgo will add any C/C++ files in the root of the package as part of the compilation. Is there a way to make C/C++ files in a given subdirectory to also be part of the compilation as well as the ones in the root by using CGO directives? 回答1: Not really. The only option you have is to make the subdirectory another Go package, but then you'd have to wrap any functionality you need in exported Go functions, and import it into your project. The compiling of C/C++ files is a

Marshalling C objects that cannot be accessed from Go

风流意气都作罢 提交于 2019-12-12 10:50:16
问题 There are some C objects like unions, structs that contain bitfields and structs whose alignment differs from Go's ABI, that cannot be accessed from Go. Some of these structures cannot be changed to be accessible from Go code as they are part of the API of an existing library. To marshall such objects into Go structures we thus cannot really use Go code. Instead w have to write the marshalling code in C. This works fine but I have not found a feasible way to define C functions that operate on

DLL-linking via Windows cgo->gcc->ld gives “undefined-reference-to-(function)” errors [closed]

落爺英雄遲暮 提交于 2019-12-12 10:28:34
问题 This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center. Closed 7 years ago . (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

linking 3rd party static libs in cgo library

回眸只為那壹抹淺笑 提交于 2019-12-12 09:48:35
问题 I have a Go library that provides bindings for the C++ OpenImageIO library (OpenImageiGO). I've been happily building my bindings via the standard dynamic linking to libOpenImageIO, but am now trying to link statically. I'm running into an issue where no matter which combination of flags I try, the external linker fails with a ton of "undefined reference" errors. I seem to recall seeing this issue raised in the past, saying that there was an issue regarding the order in which the linker saw

cgo result has go pointer

ⅰ亾dé卋堺 提交于 2019-12-11 16:20:24
问题 I am writing some go code that exports a function like that: package main import "C" //export returnString func returnString() string { // gostring := "hello world" return gostring } func main() {} I build the .so and the header file by using go build -buildmode=c-shared, but when I call returnString() in my C code, I get "panic: runtime error: cgo result has Go pointer" Is there a way to to this in go 1.9? 回答1: You need to convert your go string to *C.char . C.Cstring is utility function for

C char name[8] to GoLang Name [8]byte

无人久伴 提交于 2019-12-11 13:29:59
问题 I've got a C function that fills a C struct: typedef struct { char name[8]; } I need to copy data into Go lang struct that has the same content: type sData struct { Name [8]byte } The structure has parameters of multiple sizes: 4, 12, 32 so it would be nice to have a function that deals with more than just one size. thanks 回答1: To make this a little more generic, you can decompose the C char array to a *C.char , then use unsafe.Pointer to cast it back to an array. func charToBytes(dest []byte

Making a go string in C with cgo

血红的双手。 提交于 2019-12-11 05:16:20
问题 I'm trying to make a go string from C. I have the pointer and the length, so if I was doing it from go, I could call the C.GoStringN function. cgo generates the GoString struct, so I was wondering if I could use it directly: // struct generated by cgo typedef struct { const char *p; GoInt n; } GoString; // I have s and n from somewhere else, can I do this ? const char* s = ... ; // I own this and dont want go to free it int n = ... ; GoString st = {s, n } ; I'm using this in here to make a go

How/when do I free the memory of a C string created by Go code?

谁都会走 提交于 2019-12-11 05:06:03
问题 Here is my code: helloworld.go : package main /* #include <stdlib.h> */ import "C" import "unsafe" //export HelloWorld func HelloWorld() *C.char { cs := C.CString("Hello World!") C.free(unsafe.Pointer(cs)) return cs } func main() {} node-helloworld.cc : #include "helloworld.h" #include <node.h> #include <string> namespace demo { using v8::FunctionCallbackInfo; using v8::Isolate; using v8::Local; using v8::Object; using v8::String; using v8::Value; void Method(const FunctionCallbackInfo<Value>

CGO: How do you use pointers in Golang to access data from an array in C

喜欢而已 提交于 2019-12-11 00:23:59
问题 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 the data array they point to. I'm trying to get the data stored in the AVFrame class and use Go to write it to a file, and eventually 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

golang: CGO / clang with sqlite3.c

我只是一个虾纸丫 提交于 2019-12-10 22:43:08
问题 I would like to compile to arm on osx. With the following command: GOOS=linux GOARCH=arm CGO_ENABLED=1 go build -o foo main.go But I get: runtime/cgo clang: error: argument unused during compilation: '-mno-thumb' I use this package: https://github.com/mattn/go-sqlite3 I tried the hints: https://github.com/mattn/go-sqlite3/issues/106 Thanks for every help :-) 回答1: "As the bug report you linked to says, you will need a C compiler that targets ARM Linux. While the Go toolchain comes with