Re: [go-nuts] Passing Struct from Go to C

2019-07-10 Thread Nitish Saboo
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 

Re: [go-nuts] Passing Struct from Go to C

2019-07-10 Thread andrey mirtchovski
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  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  
> 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  
>> 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  
>> > 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/CAK4xykXPuOuEo-TCXrJDPhRymX_xQC-B_Yrywz1QcvkoYYA9PQ%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.


Re: [go-nuts] Passing Struct from Go to C

2019-07-10 Thread andrey mirtchovski
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  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  
> 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/CAK4xykVRS61Z5d-60QKTtNm%2BGO2TNbWeteb6KGNQq0490f8BVw%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.


Re: [go-nuts] Passing Struct from Go to C

2019-07-10 Thread Nitish Saboo
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 
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/CALjMrq7Xsz7PB3T3SpZD%3Dg%3DtXahAPMjo7k77HGmDkDKf2_p_bQ%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.


Re: [go-nuts] Passing Struct from Go to C

2019-07-10 Thread andrey mirtchovski
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/CAK4xykXyfti3P-49W-jbM9s_Jssb6m0w_FvN7JM6%3DR1s8mdGcg%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.


Re: [go-nuts] Passing Struct from Go to C

2019-07-10 Thread Nitish Saboo
Hi Tamas,

I made it *C.Foo but still getting the error.

main.go
===

type Callback struct {
Func func(*C.char , *C.char , C.size_t, C.int)
UserData int32
}

Calling C code in the following manner
=

C.initialize_engine(pattern_db, module_path,
(*C.Foo)(unsafe.Pointer({
Func: C.callOnMeGo_cgo
UserData: C.int(1),
})))

//export ParsedData
func ParsedData(key *C.char, value *C.char, value_len C.size_t, work C.int)
{
fmt.Println("WORK:")
fmt.Print()
//performing some operation here
}

cfunc.go
=

package main

/*

#include 

// The gateway function
void callOnMeGo_cgo(char *key, char *value, size_t value_len, int work)
{
//printf("C.callOnMeGo_cgo(): called");
void ParsedData(const char *key, const char *value, size_t value_len, int
work);
ParsedData(key, value, value_len, work);
}
*/
import "C"



syslog-node.h
===
#ifndef _TEST_H_
#define _TEST_H_

#include 

typedef void (*key_value_cb)(const char* key, const char* value, size_t
value_len, int work);
typedef struct Foo{
key_value_cb cbnitish;
int data;
}Foo;
int initialize_engine(const char* filename, const char* module_path, Foo
*cb);
int reload_pattern_db(const char* filename, Foo *cb);

#endif

syslog-node.c
===

gboolean pdbtool_accumulate_fields(NVHandle handle, const gchar *name,
const gchar *value, gssize length, gpointer user_data)
{
  user_data->cbnitish(name, value, length, user_data->data);// Callback
happening here
  return FALSE;
}

Eventually user_data will get the instance of the struct.

Looks like there is some issue with my  struct declaration in Go.Can some
please guide me  as in what is the issue ?


Error:

go build -v -x main.go cfuncs.go
WORK=/tmp/go-build037176041
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 << 

Re: [go-nuts] Passing Struct from Go to C

2019-07-10 Thread Tamás Gulácsi
*C.foo

-- 
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/32a3a3c6-78e2-492d-9542-8a1c29deb600%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: [go-nuts] Passing Struct from Go to C

2019-07-10 Thread Nitish Saboo
Hi,

Looks like I was able to resolve the C error.I am getting the following Go
error while running 'make' command:

gcc -L/usr/local/lib -lsyslog-ng -o syslog-node.so
-L/usr/local/lib/syslog-ng -ldbparser -c `pkg-config --libs --cflags
glib-2.0` -I/usr/local/include/syslog-ng/ -I./syslog-ng-3.6.2/
-I/usr/local/include/eventlog/ syslog-node.c
go build -v -x main.go cfuncs.go
WORK=/tmp/go-build873967443
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 hIEYVdT7igzjOGrFONpW/hIEYVdT7igzjOGrFONpW
-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:91:57: cannot convert unsafe.Pointer(*Callback literal) (type
unsafe.Pointer) to type _Ctype_struct_Foo
./main.go:91:84: cannot use _Cgo_ptr(_Cfpvar_fp_callOnMeGo_cgo) (type
unsafe.Pointer) as type func(*_Ctype_char, *_Ctype_char, _Ctype_ulong,
_Ctype_int) in field value
./main.go:91:128: cannot use _Ctype_int(1) (type _Ctype_int) as type int32
in field value
./main.go:166:66: cannot convert 

[go-nuts] Passing Struct from Go to C

2019-07-10 Thread Nitish Saboo
Hi,

I am trying to pass a Struct from Go to C (this is cgo).I m passing a 
callback function in that struct that will be called from C code to Go.


main.go
===

type Callback struct {
Func func(*C.char , *C.char , C.size_t, C.int)
UserData int32
}

Calling C code in the following manner
=

C.initialize_engine(pattern_db, module_path, 
(C.Foo)(unsafe.Pointer({
Func: C.callOnMeGo_cgo,
UserData: C.int(1),
})))

//export ParsedData
func ParsedData(key *C.char, value *C.char, value_len C.size_t, work C.int) 
{
fmt.Println("WORK:")
fmt.Print()
//performing some operation here
}

cfunc.go
=

package main

/*

#include 

// The gateway function
void callOnMeGo_cgo(char *key, char *value, size_t value_len, int work)
{
//printf("C.callOnMeGo_cgo(): called");
void ParsedData(const char *key, const char *value, size_t value_len, int 
work);
ParsedData(key, value, value_len, work);
}
*/
import "C"



syslog-node.h
===
#ifndef _TEST_H_
#define _TEST_H_

#include 

typedef void (*key_value_cb)(const char* key, const char* value, size_t 
value_len, int work);
typedef struct Foo{
key_value_cb cbnitish;
int data;
}Foo;
int initialize_engine(const char* filename, const char* module_path, Foo 
*cb);
int reload_pattern_db(const char* filename, Foo *cb);

#endif

syslog-node.c
===

gboolean pdbtool_accumulate_fields(NVHandle handle, const gchar *name, 
const gchar *value, gssize length, gpointer user_data)
{
  user_data->cbnitish(name, value, length, user_data->data);// Callback 
happening here
  return FALSE;
}

Eventually user_data will get the instance of the struct.


Getting following error during make command:
==

nsaboo@ubuntu:~/Documents/goworkspace/src/poc$ make
gcc -L/usr/local/lib -lsyslog-ng -o syslog-node.so 
-L/usr/local/lib/syslog-ng -ldbparser -c `pkg-config --libs --cflags 
glib-2.0` -I/usr/local/include/syslog-ng/ -I./syslog-ng-3.6.2/ 
-I/usr/local/include/eventlog/ syslog-node.c
syslog-node.c: In function ‘pdbtool_accumulate_fields’:
syslog-node.c:33:12: warning: dereferencing ‘void *’ pointer
   user_data->cbnitish(name, value, length, user_data->data);
^~
syslog-node.c:33:12: error: request for member ‘cbnitish’ in something not 
a structure or union
syslog-node.c:33:53: warning: dereferencing ‘void *’ pointer
   user_data->cbnitish(name, value, length, user_data->data);
 ^~
syslog-node.c:33:53: error: request for member ‘data’ in something not a 
structure or union
makefile:12: recipe for target 'syslog-node.so' failed
make: *** [syslog-node.so] Error 1

Can someone please guide me here ?

Thanks,
Nitish

-- 
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/c8b2f46a-78e2-4306-8e96-6c30bb7529f5%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.