kandi X-RAY | cgo Summary
kandi X-RAY | cgo Summary
cgo by example
Support
Quality
Security
License
Reuse
Top functions reviewed by kandi - BETA
- Prints C .
cgo Key Features
cgo Examples and Code Snippets
Community Discussions
Trending Discussions on cgo
QUESTION
I'm running a go program in a docker container (golang:1.18-bullseye
).
I haev tried running it both with go run main.go
and go run .
My code looks likes this, both header files are located in the Include
directory given in the CFLAGS:
ANSWER
Answered 2022-Apr-05 at 13:56I was able to reproduce and fix this. There are also some additional gotchas. Start by just focusing on running go build
:
Ok so the go compiler has found the header file, but cannot find the shared library. I think you modified your code for the question slightly and this is not an issue, but the path for -L
in LDFLAGS
has to be either:
- relative to the source directory using
${SRCDIR}
- an absolute path
- avoid this entirely and leverage pkg-config
I just used the relative directory containing the
so
file as my argument for-L
.
Ok, that aside, you must also give a -l
argument in LDFLAGS to find the file in the paths you pointed to (IE: libvendera.so
needs -lvendora
).
Once go build
works you have an application that still needs the know where the so
file is to run (so hence a shared library). To do this you will likely need to set LD_LIBRARY_PATH
and point to the directory containing the so
file much like you did with -L
.
QUESTION
I'm trying to create a go program and I need to use freetype, but module for freetype in go seems really different from the original freetype library for c. Overcome this problem I created a simple library in C, and I'm trying to call a function in that library from Go using cgo. But I couldn't seem to figure out where should my .a file needs to be located in order go to be able to find it. I'm using go modules and my project is not in the GOPATH.
So my question is where I need to put my C library's .h and .a files?
...ANSWER
Answered 2022-Mar-04 at 03:00If i got you right. It's a problem that you don't know how to tell cgo tool chain where to find .h and .a files. There is many examples on web, provide mine here.
code_exampleQUESTION
I have a lib, that uses some C on linux. On windows it is just a dummy noop lib with functions that do nothing.
The lib is in 3 files: lib_linux.go
, lib_win.go
and lib.c
But when I try to compile it on windows, it throws this error: C source files not allowed when not using cgo or SWIG: lib.c
How can I tell to the go compiler to ignore the C source files on windows?
...ANSWER
Answered 2022-Feb-28 at 07:49A similar issue suggests (when tailored to your case):
workarounds
- Rename the
.c
file lib_linux.c;- Or add a build constraint
// +build linux
condition to the top of the.c
file (conditional compilation).
QUESTION
So I want to make a app that shows some images and when the window resizes, it scales also all images to fit on the screen. My strategy is this:
- call clCreateImage for every original image and store the
cl_mem
pointers in a global map (something like:filename -> cl_mem
) - call
scale(cl_mem img, int width, int height)
, that calls a kernel and reads the output image data. Then show the scaled images. - when the a window resize event occures: drop all scaled images and call
scale(...)
another time, with thecl_mem
pointers stored in the map
I tried it on a small scale where I just create one image, and call scale
two times. The first is OK and scaled, but the second is corrupted. I don't know why, the src image is CL_MEM_READ_ONLY | CL_MEM_COPY_HOST_PTR
, the out image is created in scale
function.
original: https://ibb.co/pQDwScW
first time scaled (to fit in 300x300): https://ibb.co/nnN9kTY
second try (to fit in 500x500): https://ibb.co/561SkSn
But also when I try just 300x300 and then also 300x300 it is corrupted. Is there a rule, that images can't be used in a kernel multiple times? Or im just missing something?
EDIT:
C code is here: https://pastebin.com/NpSaxRyT
It is used from golang with cgo, so the createImage
returns the cl_mem
as void*
, so that the Go part doesn't need to include cl.h
.
init()
function is called on startup of the Go program. createImage
function is called from Go with pixel data.
So the image pointers (cl_mem
pointers) are in Go and then the resize
(scale
) function is called. It takes the void*
pointer and buffer pointer where the pixel data will be transfered.
Also the buffer is allocated by Go, but with the first image it is OK and I think that the GC doesn't kick in (also tried to disable GC entirely).
like so:
...ANSWER
Answered 2022-Feb-08 at 14:30You are releasing right after the first resize operation
QUESTION
In my project, I am using callbacks for bi-directional calls from C into go and vice versa using CGO. I resolved the issue of circular dependencies by compiling the C part into a library, then compiling the go part into a library, then a final linker pass puts it all together. This is working fine when not using go modules. Go source files are listed on the command line explicitly. I have been told that as of go 1.12 "this is not the right way to do it".
As the project has grown, I now want to use go modules. Unfortunately, this changes the behaviour of the go compiler. It now wants to resolve external dependencies and implicitly includes them in the output file. Due to the circular dependency, it now always ends up with an undefined reference or multiple definitions. How to resolve circular dependencies when using cgo and go modules "the right way"?
This is a minimal example to illustrate the problem. Remove the file-name "hello.go" from the call to go in the Makefile to see how it falls apart.
This is the error message:
...ANSWER
Answered 2022-Jan-31 at 14:12If you look at the verbose output from the go build
command, you will see that when compiling the directory as a complete go package, the main.c
file is being included as part of the C code used in hello.go
.
From the documentation:
When the Go tool sees that one or more Go files use the special import "C", it will look for other non-Go files in the directory and compile them as part of the Go package
The easiest solution here is to separate the main C and Go packages, so that they don't interfere with each other's build process. Testing this out, removing the main.c
file will build libchello.a
and libgohello.a
, and then adding it back in will complete the build of main
.
QUESTION
I am trying to build my Rust crate as static lib to furter use it within Golang through FFI.
So far tried buch of different approaches regarding linking, but still having undefined reference
kind error from final go binary:
ANSWER
Answered 2022-Jan-19 at 10:30The problem was in right flags for CGO. This is how header for CGO in main.go looks now
QUESTION
I am trying to run a simple go code
...ANSWER
Answered 2021-Dec-27 at 13:36Following commands helped me.
Try to reinstall Xcode command-line tools and upgrade llvm and gcc.
QUESTION
I am trying to use CGO to use realsense2.dll (Intel's RealSense camera library). I am on Windows 11 x64.
My test program that counts the number of devices connected works when my directory layout is:
- rs2test
- go.mod
- realsense2.dll
- context.go (wrapper for C code)
- devicelist.go (wrapper for C code)
- error.go (wrapper for C code)
- main.go
and in each wrapper for C code file I have the following directives:
...ANSWER
Answered 2021-Dec-27 at 08:13A DLL is needed not only when building the application but also (and most importantly) when running it.
The DLL must be in the same folder as the executable, the current working directory, or on the PATH in order to be found. This is how Windows works. For more details refer to DLL search order.
QUESTION
My app compiles fine when GOARCH
is set to arm64
(or is omitted). However, when I try to compile an amd64 binary (GOOS=darwin GOARCH=amd64 go build
), I get the following error:
ANSWER
Answered 2021-Dec-25 at 22:25The answer to the wasm question (as you posted) talks about cgo. cgo invokes platform compiler with platform specific headers/libs (on Mac, with framework too). When you cross-compile with CC
, you also need cross-compile compiler + headers/libs + frameworks. It is not easy: you may need tools like xgo. But still cross-compile may fail.
Go is different, Go re-implements a HAL in go or plan9 ASM on each OS/arch. So when you cross-compile cgo + go for am64 on arm64 together, go build
will try to blend "cgo+arm64" with "go+amd64". Sadly, it is an empty set for the built-in go build
tool.
Refer to the @fperson's own answer.
QUESTION
How to pass a string
as argument from Python to a Go Dll using ctypes:
Go-code:
...ANSWER
Answered 2021-Dec-14 at 13:12A Go string and a C string are entirely unrelated (except in that both are called strings, which is a lie for at least one of them).
Here Python is sending a C string because you've told it to, but Go expects a Go string, which has a completely diffrent layout so it blows up. And if it didn't blow up at the callsite it'd probably blow up when the GC tries to handle the string, which it can't, because it's not a Go string.
You want to look at the magical "C"
pseudo-package: you need to take in a *C.char
and copy that to a Go string
using C.GoString
before you can pass it to anything expecting a go String. Or something along those lines, my experience with cgo (especially calling into it) is limited to avoiding this as a bad idea.
Regardless you probably want to at the very least read the cgo documentation in full, FFI is tricky at the best of time, and FFI between two managed languages much more so.
Community Discussions, Code Snippets contain sources that include Stack Exchange Network
Vulnerabilities
No vulnerabilities reported
Install cgo
Support
Reuse Trending Solutions
Find, review, and download reusable Libraries, Code Snippets, Cloud APIs from over 650 million Knowledge Items
Find more librariesStay Updated
Subscribe to our newsletter for trending solutions and developer bootcamps
Share this Page