Hi Andrey, I made the following changes as suggested by you:
InitStruct := C.calloc(1, C.sizeof_struct_Foo) InitStruct.key_value_cb = C.callOnMeGo_cgo InitStruct.data = C.int(1) C.initialize_engine(pattern_db, module_path, (*C.Foo)(unsafe.Pointer(InitStruct))) Still Getting following error: Am i missing something? go build -v -x main.go cfuncs.go WORK=/tmp/go-build822337807 command-line-arguments mkdir -p $WORK/b001/ cd /home/nsaboo/Documents/goworkspace/src/poc pkg-config --cflags -- syslog-ng eventlog pkg-config --libs -- syslog-ng eventlog CGO_LDFLAGS='"-g" "-O2" "syslog-node.so" "-L/usr/local/lib/" "-lsyslog-ng" "-L/usr/local/lib/syslog-ng" "-ldbparser" "-L/usr/local/lib" "-Wl,--export-dynamic" "-lgmodule-2.0" "-pthread" "-lgthread-2.0" "-pthread" "-lglib-2.0" "-lsyslog-ng" "-lglib-2.0" "-levtlog"' /usr/local/go/pkg/tool/linux_amd64/cgo -objdir $WORK/b001/ -importpath command-line-arguments -- -I/usr/local/include/syslog-ng -I/usr/local/include/syslog-ng/ivykis -I/usr/local/include/eventlog -I/usr/include/glib-2.0 -I/usr/lib/x86_64-linux-gnu/glib-2.0/include -I $WORK/b001/ -g -O2 -I/usr/local/include/ ./main.go ./cfuncs.go cd $WORK gcc -fno-caret-diagnostics -c -x c - || true gcc -Qunused-arguments -c -x c - || true gcc -fdebug-prefix-map=a=b -c -x c - || true gcc -gno-record-gcc-switches -c -x c - || true cd $WORK/b001 TERM='dumb' gcc -I /home/nsaboo/Documents/goworkspace/src/poc -fPIC -m64 -pthread -fmessage-length=0 -fdebug-prefix-map=$WORK/b001=/tmp/go-build -gno-record-gcc-switches -I/usr/local/include/syslog-ng -I/usr/local/include/syslog-ng/ivykis -I/usr/local/include/eventlog -I/usr/include/glib-2.0 -I/usr/lib/x86_64-linux-gnu/glib-2.0/include -I ./ -g -O2 -I/usr/local/include/ -o ./_x001.o -c _cgo_export.c TERM='dumb' gcc -I /home/nsaboo/Documents/goworkspace/src/poc -fPIC -m64 -pthread -fmessage-length=0 -fdebug-prefix-map=$WORK/b001=/tmp/go-build -gno-record-gcc-switches -I/usr/local/include/syslog-ng -I/usr/local/include/syslog-ng/ivykis -I/usr/local/include/eventlog -I/usr/include/glib-2.0 -I/usr/lib/x86_64-linux-gnu/glib-2.0/include -I ./ -g -O2 -I/usr/local/include/ -o ./_x002.o -c main.cgo2.c TERM='dumb' gcc -I /home/nsaboo/Documents/goworkspace/src/poc -fPIC -m64 -pthread -fmessage-length=0 -fdebug-prefix-map=$WORK/b001=/tmp/go-build -gno-record-gcc-switches -I/usr/local/include/syslog-ng -I/usr/local/include/syslog-ng/ivykis -I/usr/local/include/eventlog -I/usr/include/glib-2.0 -I/usr/lib/x86_64-linux-gnu/glib-2.0/include -I ./ -g -O2 -I/usr/local/include/ -o ./_x003.o -c cfuncs.cgo2.c TERM='dumb' gcc -I /home/nsaboo/Documents/goworkspace/src/poc -fPIC -m64 -pthread -fmessage-length=0 -fdebug-prefix-map=$WORK/b001=/tmp/go-build -gno-record-gcc-switches -I/usr/local/include/syslog-ng -I/usr/local/include/syslog-ng/ivykis -I/usr/local/include/eventlog -I/usr/include/glib-2.0 -I/usr/lib/x86_64-linux-gnu/glib-2.0/include -I ./ -g -O2 -I/usr/local/include/ -o ./_cgo_main.o -c _cgo_main.c cd /home/nsaboo/Documents/goworkspace/src/poc TERM='dumb' gcc -I . -fPIC -m64 -pthread -fmessage-length=0 -fdebug-prefix-map=$WORK/b001=/tmp/go-build -gno-record-gcc-switches -o $WORK/b001/_cgo_.o $WORK/b001/_cgo_main.o $WORK/b001/_x001.o $WORK/b001/_x002.o $WORK/b001/_x003.o -g -O2 syslog-node.so -L/usr/local/lib/ -lsyslog-ng -L/usr/local/lib/syslog-ng -ldbparser -L/usr/local/lib -Wl,--export-dynamic -lgmodule-2.0 -pthread -lgthread-2.0 -pthread -lglib-2.0 -lsyslog-ng -lglib-2.0 -levtlog TERM='dumb' /usr/local/go/pkg/tool/linux_amd64/cgo -dynpackage main -dynimport $WORK/b001/_cgo_.o -dynout $WORK/b001/_cgo_import.go cat >$WORK/b001/importcfg << 'EOF' # internal # import config packagefile bufio=/usr/local/go/pkg/linux_amd64/bufio.a packagefile encoding/json=/usr/local/go/pkg/linux_amd64/encoding/json.a packagefile fmt=/usr/local/go/pkg/linux_amd64/fmt.a packagefile os=/usr/local/go/pkg/linux_amd64/os.a packagefile path=/usr/local/go/pkg/linux_amd64/path.a packagefile runtime=/usr/local/go/pkg/linux_amd64/runtime.a packagefile time=/usr/local/go/pkg/linux_amd64/time.a packagefile runtime/cgo=/usr/local/go/pkg/linux_amd64/runtime/cgo.a packagefile syscall=/usr/local/go/pkg/linux_amd64/syscall.a EOF /usr/local/go/pkg/tool/linux_amd64/compile -o $WORK/b001/_pkg_.a -trimpath $WORK/b001 -p main -buildid 6VU9NC3t5EyTg_0nt_wU/6VU9NC3t5EyTg_0nt_wU -goversion go1.12.4 -D _/home/nsaboo/Documents/goworkspace/src/poc -importcfg $WORK/b001/importcfg -pack -c=2 $WORK/b001/_cgo_gotypes.go $WORK/b001/main.cgo1.go $WORK/b001/cfuncs.cgo1.go $WORK/b001/_cgo_import.go # command-line-arguments ./main.go:92:12: InitStruct.key_value_cb undefined (type unsafe.Pointer has no field or method key_value_cb) ./main.go:93:12: InitStruct.data undefined (type unsafe.Pointer has no field or method data) ./main.go:170:12: InitStruct.key_value_cb undefined (type unsafe.Pointer has no field or method key_value_cb) ./main.go:171:12: InitStruct.data undefined (type unsafe.Pointer has no field or method data) makefile:6: recipe for target 'main' failed make: *** [main] Error 2 Thanks, Nitish On Wed, Jul 10, 2019 at 9:20 PM andrey mirtchovski <mirtchov...@gmail.com> wrote: > the Go "Callback" struct may have completely different size and > alignment than the C.Foo one expected by C.initialize_engine. > therefore you want to construct the C version when calling > C.initialize_engine, and the Go version for everything else. Your call > may look something like this: > > initStruct := C.calloc(1, sizeof(C.Foo)) > initStruct.key_value_cb = C.callOnMeGo_cgo > initStrudct.data: C.int(1) > > C.initialize_engine(pattern_db, module_path, > (*C.Foo)(unsafe.Pointer(initStruct))) > > the Go Callback struct is not required unless you expect the callbacks > to change or be settable by library clients, in which case you'll need > to construct a more elaborate struct that implements locking and (at > least what I do) a map between callback names and interface{} pointing > to the callbacks sent by the library clients. > > On Wed, Jul 10, 2019 at 8:14 AM Nitish Saboo <nitish.sabo...@gmail.com> > wrote: > > > > Hi Andrey, > > > > I am new to Go and cgo therefore did not exactly understand your point. > > Can you explain me in the context of the code that I had posted.It will > really help me to get hold of the things much better. > > > > Thanks, > > Nitish > > > > On Wed, Jul 10, 2019 at 5:19 PM andrey mirtchovski < > mirtchov...@gmail.com> wrote: > >> > >> what i do is have a similar struct in Go, and the original C one. here > >> 'callbacks' is the go one, and > >> "type Engine C.struct_cl_engine" is the opaque C one: > >> > >> https://github.com/mirtchovski/clamav/blob/master/callback.go#L63 > >> > >> On Wed, Jul 10, 2019 at 5:25 AM Nitish Saboo <nitish.sabo...@gmail.com> > wrote: > >> > > >> > Hi Andrey, > >> > > >> > I understand the issue here but how can I pass a callback function in > a struct to C code.I need a struct because I want to pass and an extra > parameter 'userdata' as well that I would require back as part of callback. > >> > > >> > Thanks, > >> > Nitish > >> > > >> > On Wed, Jul 10, 2019 at 4:39 PM andrey mirtchovski < > mirtchov...@gmail.com> wrote: > >> >> > >> >> i don't think you can use a go function directly in the callback. you > >> >> need a correctly-typed C helper to do it. at least that used to be > the > >> >> case. see examples: > >> >> > >> >> https://github.com/mirtchovski/clamav/blob/master/cfuncs.go > >> >> https://github.com/mirtchovski/clamav/blob/master/callback.go > -- You received this message because you are subscribed to the Google Groups "golang-nuts" group. To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscr...@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/CALjMrq7ybuL6n2eG-KUGbCQrq0HHi6kTwrJ30j1JnfHday21cA%40mail.gmail.com. For more options, visit https://groups.google.com/d/optout.