927 questions
5
votes
1
answer
107
views
Use Petsc-Library in Go
I am trying to use the PETSc (Portable, Extensible Toolkit for Scientific Computation) library in Go over cgo. After the call of PetscInitialize, the program will crash at a random point with the ...
2
votes
1
answer
186
views
CGO allows function returning string but it does not work
I want to call a go function from C++ and return a string.
The function:
//export EditStr
func EditStr(a string) (ostr string) {
ostr = fmt.Sprintf("[["+a+"]]")
fmt....
1
vote
0
answers
89
views
Environment variables not accessible in dynamically loaded .dylib on macOS using Rust libloading, but works on Windows
macOS: The dynamically loaded .dylib cannot read environment variables that were set by the main Rust process at runtime
Windows: The same code works perfectly - the .dll can access environment ...
4
votes
0
answers
65
views
Failed to create thumbnail using the PTB_SaveAsImage function from the C API (In GO)
For testing the callas PDF toolbox SDK, I want to convert a sample PDF to a PNG. The callas license is activated.
In my main() function, I successfully initialize the SDK. After that, I call the ...
1
vote
1
answer
158
views
ctypes + cgo and ownership of strings
Background
I am currently using a DLL I generated with golang in python. As a sanity check for debugging I added a function called return_string(), which takes a pointer to a string in C (char*) ...
0
votes
1
answer
92
views
Deferring free on a parameter
I am working with cgo, I have a parameter that's being passed in as a *C.char. Once it's been used in a function call I no longer need it, so I defer freeing it, but when I do I don't get a result ...
1
vote
2
answers
141
views
Cross compiling a Go native binary for Java code using docker
I'm trying to compile a Go native binary to use in Java code on Mac. The binary needs to be executed on a linux host which has the following specifications:
# uname -a
Linux <hostname> 5.14.0-...
3
votes
1
answer
285
views
Zig as C Linux->Mac cross-compiler for go project with go-sqlite3 error: unable to find dynamic system library 'resolv'
I'm building a Go application with native (CGO_ENABLED=1) SQLite support using https://github.com/mattn/go-sqlite3.
I'm trying to get the builder docker image to a reasonable size. (goreleaser/...
2
votes
1
answer
79
views
How to process mismatch C & Go structure issue when Go process C written file format?
I have a file written in C with the format "header.h", and I want to read it using CGO to automatically retrieve the field names defined in header.h.
Here is a shortened example where I ...
0
votes
1
answer
122
views
Golang ImageMagick Leftover Threads
I have written a linux service in Go that processes a large number of image files, converting them from TIFF to JPEG and adding a text overlay on top. I'm using the Go package gographics/imagick/v3 ...
2
votes
1
answer
221
views
Calling a static C/C++ library from Go
I'm still on the learning curve with Go, but I've managed to compile and run a Go program that includes a C object file.
The objective is to call an existing static C++ library. The call will be a bit ...
0
votes
0
answers
374
views
golang and C with SIMD instructions
I am quite new to golang, and I am doing some experiments with golang and cgo. I wanted to see if golang can benefit with a C program that uses SIMD operations on a simple test, however what I saw is ...
2
votes
0
answers
86
views
Unable to get the c stack trace with a corefile from a cgo routine
I'm using https://github.com/gen2brain/aac-go.git to encode AAC in my golang project, but sometimes I got crash in my ARMV7l linux and I have executed like CGO_DEBUG=1 GOTRACEBACK=crash ./binary to ...
0
votes
0
answers
195
views
Go CGO: Binary doesn't find shared library with rpath, only works with LD_LIBRARY_PATH
I am working on a GO project that links to a shared library (.so) using CGO. I want to embed the library path in the binary using rpath,so that it runs without needing to set the LD_LIBRARY_PATH. ...
1
vote
1
answer
192
views
cant run go code with sqlite3 on windows but works fine on linux
I have to switch to windows for a week and I cant run my project
I installed GCC via Cygwin and added it to path, I can call it from any shell. I set CGOENABLED=1 because it was required but I keep ...
3
votes
0
answers
53
views
Go Mobile Performance: Regular CPU Usage Gaps on iOS
I'm encountering an interesting performance pattern when running a Go benchmark on iOS using gomobile. The CPU usage shows regular gaps of 30ms and 40ms between execution periods when monitored using ...
0
votes
1
answer
43
views
Code written in golang, output as a C dynamic library, is called by posgres17, but Golang-generated dynamic library functions is blocked
everyone:
I wrote a demo, but it doesn't work properly, can you help me, thank you very much.
Code written in golang, output as C dynamic library "libtest1.so", is called by the "...
1
vote
1
answer
107
views
How to use enums and structs defined in another package with cgo?
When I want to create an instance of a struct, defined in another package then I get an IncompatibleAssign error.
The project has following structure:
def.go
root/root.go
In def.go a struct is ...
0
votes
2
answers
119
views
Does Go implicitly keep alive the parent structures of pointers passed to C functions?
Consider the following snippet in Go that uses cgo to wrap a C API:
/*
#include <myheader.h>
void MyCall(my_type* a, my_type* b);
*/
import "C"
// Go wrapper for native pointer ...
2
votes
1
answer
84
views
Invoke function pointer from go. (Windows Management Infrastructure client)
I develop go pure go client for Windows Management Infrastructure (MI). It's the successor of WMI.
While initialization works as indented
package main
import (
"unsafe"
"...
1
vote
0
answers
64
views
Absolute path removal when #cgo is used
I am working with an existing go code that uses #cgo parts. I am trying to make this code compile in a reproducible way in terms of the output binary (the same sha256/ md5 no matter in what dir it is ...
1
vote
0
answers
38
views
Embedding a .so file while building Go project that uses C-API via C-Go interface
I have a Go project that uses the C-APIs of a C Library, via the C-Go interface. After the project is built, it requires a .so file while its execution. So now when I want to ship the project's binary ...
0
votes
1
answer
131
views
How can I link a Go static library to a C program
I'm trying to call a Go function from a C program. I have built a static library from my Go source, however, ld is unable to find the function I want to call from the C program.
Go code:
package main
...
1
vote
0
answers
65
views
Right way to add .h dependency to go_library using Cgo?
I am trying to build a go_binary that depends on com_github_pebbe_zmq4, a go wrapper of the libzmq in C++. I've been struggling to make pebbe/zmq4 "see" zmq.h in libzmq, and can't make it ...
1
vote
0
answers
93
views
CGO and Clang not compiling on Mac for Typedef Enum Struct but working on Linux
I have a typedef enum struct defined as such
typedef enum struct EnumStructName : int { Val1, Val2, Val2 } EnumStructName;
When I am compiling the C++ code on my Mac its working fine. And its ...
3
votes
0
answers
128
views
Why do some fields in the Go structs generated using cgo become anonymous?
I'm developing using a third-party SDK, but I've encountered an issue related to CGO conversion.
this C struct from third-party SDK,
C struct
typedef struct PicHead
{
unsigned short Flag;
...
0
votes
1
answer
77
views
In cgo, how to call c meth with go func arg
Following is my code, I hope go can capture c code's progress
package main
/*
#cgo CFLAGS: -I/home/roroco/Downloads/go/test_cgo/submodule/test_cgo_cpp/include
#cgo LDFLAGS: -L/home/roroco/Downloads/...
1
vote
0
answers
136
views
Why CGo-based build targeting GOOS=wasip1 failed
I cannot build a cgo-based project targeting GOOS=wasip1. Here is github repository helping reproduction.
Environment
Go 1.22.4
Debian 12
Output of go env command goes as
GO111MODULE=''
GOARCH='...
2
votes
0
answers
170
views
Embedded python script in golang with go routines
I'm working on a Go application that interfaces with Python via CGO to process store data. I'm using goroutines because I have more than 4M store.
Issue: The execution gets blocked after some ...
1
vote
1
answer
275
views
Marshaling memory between C and CGO (free/malloc)
I am working on a project where i have a C application that needs to call an sdk that is written in Go. We have created a wrapper DLL that we can invoke from our C application. However, the marshaling ...
1
vote
0
answers
121
views
Why CGO_FLAGS not point to the include file when go build?
I go get portaudio go module. And try to run the examples.
go build mp3.go
And it reports
# github.com/bobertlo/go-mpg123/mpg123
../../../../go/pkg/mod/github.com/bobertlo/go-mpg123@xxx/mpg123/mpg123....
1
vote
2
answers
220
views
How to set cgo env variable before vscode launch code?
I have golang code like:
import (
"github.com/bobertlo/go-mpg123/mpg123"
)
func main() {
...
}
From terminal, to build this code. I must set below env variable:
export ...
2
votes
0
answers
34
views
cgo : running 2 or more dynamic link GO from C executable
run C exe that linked with 2 GO dynamic link shared object .
text
"*Go code linked into, and called from, a non-Go program
In this mode Go code acts as a library that may be called by a non-Go ...
0
votes
1
answer
166
views
How to apply `export LD_LIBRARY_PATH` in cgo
I use following main.go call cpp lib so
package main
/*
#cgo LDFLAGS: -L/home/roroco/Dropbox/cpp/cpp_lib/draft/go_call_cpp -lprogress
#include "progress_lib.h"
// Callback function to be ...
1
vote
1
answer
65
views
docker run go process with cgo, receive signal sent by pid 0
Here is my structure.
main.go with C signal handler (ref: handle signal with cgo, go cannot handle signal)
use go build and run in docker (with -v /:/rootfs:ro in docker-compose.yml)
Experiments:
...
1
vote
0
answers
75
views
Is it possible to have several dynamically linked Go libs in one process?
The main question - is it possible to have several Go runtimes linked dynamically into one binary and running in the one process?
I have two Go libraries which are linked into one Swift app and after ...
-2
votes
1
answer
135
views
Build Go with C++ (cgo) via docker linux x64 for linux i386
I use the docker golang:1.22 for building my Go project for Windows x32/x64 and Linux x32/x64.
I have installed:
apt -y install build-essential
apt -y install gcc-multilib
apt -y install g++-multilib
...
0
votes
1
answer
236
views
handle signal with cgo, go cannot handle signal
Here is my main.go
package main
/*
#include <stdio.h>
#include <signal.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
// Global variable to store old ...
1
vote
1
answer
172
views
Need help pinning go/c memory for ffi call
I am learning go and cgo and ffi, I have the header file below:
#include <stdarg.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdlib.h>
typedef struct MyStruct3 {
...
2
votes
1
answer
71
views
Golang passing sql.DB between C and Go
I'm writing on an encrypt for sql.DB connection, but have some problems in converting pointer between C and Go. Looks like sql.DB is not a simple struct in Go and have GoPointer, any chance can export ...
3
votes
1
answer
50
views
How to pass a C function pointer around in go
I can pass a C function pointer to a C function, but passing it to a go function gives invalid operation.
I have 100 go functions wrapping C functions and most share the same setup and arguments so I ...
0
votes
0
answers
121
views
C-code in Golang. Gortsplib client-play-format-h264
I'm trying to launch gortsp example: https://github.com/bluenviron/gortsplib/blob/main/examples/client-play-format-h264. But I have a problem with h264_decoder.go: "exec: "pkg-config": ...
0
votes
0
answers
62
views
Strange behavior of global go variable in c-archive compiled library
everyone.
Right now I'm writing my "scientific work" about fuzzing and I've got a problem with something in my fuzzing lib.
I try to fuzz go-ethereum lib (specificly bn256 part of it):
go-...
0
votes
0
answers
89
views
Does disabling GC make CGO callbacks safe?
I'm integrating a c library into my go program, and one of the functions I have to use have a callback interface. As is common with these, the function in question also takes a pointer, that it will ...
2
votes
1
answer
322
views
fyne/go RunNative example
I'm trying to run this example
I'm have an error
"android/386 requires external (cgo) linking, but cgo is not enabled [android,386]"
in terminal after: fyne package -os android -appID com....
2
votes
0
answers
268
views
How to use CGo properly to avoid memory leaks? Working with LightGbm C Api from Microsoft
I am using the LightGBM C Api in our ML model hosting service, written in Golang. I've written a CGO wrapper around the C Api. I am using the “lib_lightgbm.so” library file provided on Github.
I am on ...
2
votes
1
answer
120
views
go build with cgo displays undefined reference
This is my directory structure before any build process:
$ tree .
.
├── go.mod
├── include
│ └── calc.h
├── lib
├── main.go
└── src
├── calc.c
└── execute.c
Then, I compiled C files and ...
0
votes
1
answer
234
views
Mocking CGO function call for Unit testing
I am using Tensorflow Lite API in C to infer results from a model file, for which code is written in a .c file across different functions. The functions include get_interpreter which creates a new ...
0
votes
1
answer
97
views
how to read char * string from a c union in cgo?
I have a c union that stores my data as part of a value struct
union data_union {
char *string_val;
int8_t int8_val;
int64_t int64_val;
// so on
};
typedef enum {
MY_STRING,
MY_INT8,
...
3
votes
1
answer
680
views
How to embed C dynamic libraries into Go binary
I have a Golang project which uses some C libraries from another team in my company which come with shared objects and would be very difficult to compile statically.
I would like to keep the binary ...