cgo | cgo by example

 by   giorgisio Go Version: Current License: MIT

kandi X-RAY | cgo Summary

kandi X-RAY | cgo Summary

cgo is a Go library. cgo has no bugs, it has no vulnerabilities, it has a Permissive License and it has low support. You can download it from GitHub.

cgo by example
Support
    Quality
      Security
        License
          Reuse

            kandi-support Support

              cgo has a low active ecosystem.
              It has 28 star(s) with 6 fork(s). There are 2 watchers for this library.
              OutlinedDot
              It had no major release in the last 6 months.
              There are 1 open issues and 0 have been closed. There are no pull requests.
              It has a neutral sentiment in the developer community.
              The latest version of cgo is current.

            kandi-Quality Quality

              cgo has 0 bugs and 0 code smells.

            kandi-Security Security

              cgo has no vulnerabilities reported, and its dependent libraries have no vulnerabilities reported.
              cgo code analysis shows 0 unresolved vulnerabilities.
              There are 0 security hotspots that need review.

            kandi-License License

              cgo is licensed under the MIT License. This license is Permissive.
              Permissive licenses have the least restrictions, and you can use them in most projects.

            kandi-Reuse Reuse

              cgo releases are not available. You will need to build from source code and install.

            Top functions reviewed by kandi - BETA

            kandi has reviewed cgo and discovered the below as its top functions. This is intended to give you an instant insight into cgo implemented functionality, and help decide if they suit your requirements.
            • Prints C .
            Get all kandi verified functions for this library.

            cgo Key Features

            No Key Features are available at this moment for cgo.

            cgo Examples and Code Snippets

            No Code Snippets are available at this moment for cgo.

            Community Discussions

            QUESTION

            Cgo: undefined reference to [C function]
            Asked 2022-Apr-05 at 14:47

            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:56

            I 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.

            Source https://stackoverflow.com/questions/71749797

            QUESTION

            Using cgo with go modules
            Asked 2022-Mar-04 at 03:00

            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:00

            If 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_example

            Source https://stackoverflow.com/questions/71194180

            QUESTION

            Go cgo - ignore C source files on windows
            Asked 2022-Feb-28 at 07:49

            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:49

            A similar issue suggests (when tailored to your case):

            workarounds

            Source https://stackoverflow.com/questions/71291234

            QUESTION

            opencl - using image multiple times
            Asked 2022-Feb-08 at 14:30

            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:

            1. call clCreateImage for every original image and store the cl_mem pointers in a global map (something like: filename -> cl_mem)
            2. call scale(cl_mem img, int width, int height), that calls a kernel and reads the output image data. Then show the scaled images.
            3. when the a window resize event occures: drop all scaled images and call scale(...) another time, with the cl_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:30

            You are releasing right after the first resize operation

            Source https://stackoverflow.com/questions/71030389

            QUESTION

            How to resolve circular dependencies when using go modules and cgo
            Asked 2022-Jan-31 at 14:12

            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:12

            If 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.

            Source https://stackoverflow.com/questions/70913606

            QUESTION

            Build Rust static library to use within CGO
            Asked 2022-Jan-19 at 10:30

            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:30

            The problem was in right flags for CGO. This is how header for CGO in main.go looks now

            Source https://stackoverflow.com/questions/70756015

            QUESTION

            I am facing an error while running simple go code
            Asked 2021-Dec-27 at 14:06

            I am trying to run a simple go code

            ...

            ANSWER

            Answered 2021-Dec-27 at 13:36

            Following commands helped me.
            Try to reinstall Xcode command-line tools and upgrade llvm and gcc.

            Source https://stackoverflow.com/questions/70494946

            QUESTION

            Why can't I link to this DLL in a package folder using CGO?
            Asked 2021-Dec-27 at 08:13

            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:13

            A 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.

            Source https://stackoverflow.com/questions/70492538

            QUESTION

            How to compile an amd64 binary that uses C on an M1 (arm64) Mac
            Asked 2021-Dec-25 at 22:25

            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:25

            The 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.

            Source https://stackoverflow.com/questions/70482886

            QUESTION

            Ctypes calling Go Dll with arguments (C string)
            Asked 2021-Dec-14 at 15:34

            How to pass a string as argument from Python to a Go Dll using ctypes:

            Go-code:

            ...

            ANSWER

            Answered 2021-Dec-14 at 13:12

            A 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.

            Source https://stackoverflow.com/questions/70349271

            Community Discussions, Code Snippets contain sources that include Stack Exchange Network

            Vulnerabilities

            No vulnerabilities reported

            Install cgo

            You can download it from GitHub.

            Support

            For any new features, suggestions and bugs create an issue on GitHub. If you have any questions check and ask questions on community page Stack Overflow .
            Find more information at:

            Find, review, and download reusable Libraries, Code Snippets, Cloud APIs from over 650 million Knowledge Items

            Find more libraries
            CLONE
          • HTTPS

            https://github.com/giorgisio/cgo.git

          • CLI

            gh repo clone giorgisio/cgo

          • sshUrl

            git@github.com:giorgisio/cgo.git

          • Stay Updated

            Subscribe to our newsletter for trending solutions and developer bootcamps

            Agree to Sign up and Terms & Conditions

            Share this Page

            share link