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 unsafe.Pointer(*Callback literal) (type
unsafe.Pointer) to type _Ctype_struct_Foo
./main.go:166:93: 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:166:137: cannot use _Ctype_int(1) (type _Ctype_int) as type int32
in field value
makefile:6: recipe for target 'main' failed
make: *** [main] Error 2


Can someone please guide me here ?

Thanks,
Nitish

On Wed, Jul 10, 2019 at 1:03 PM Nitish Saboo <nitish.sabo...@gmail.com>
wrote:

> 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(&Callback{
> 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 <stdio.h>
>
> // 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 <stdlib.h>
>
> 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
> <https://groups.google.com/d/msgid/golang-nuts/c8b2f46a-78e2-4306-8e96-6c30bb7529f5%40googlegroups.com?utm_medium=email&utm_source=footer>
> .
> For more options, visit https://groups.google.com/d/optout.
>

-- 
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/CALjMrq6p-m_ZxvJMueK%2BV03cwUayFL5_sqv5drAQBNFAXxMOqQ%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to