Hi, I followed the link ' https://github.com/golang/go/wiki/cgo#function-variables' and made the following changes: *1)main.go* package main //#cgo CFLAGS: -I./syslog-ng-3.6.2/ //#cgo LDFLAGS: syslog-node.so //#cgo LDFLAGS: -L/usr/local/lib/ -lsyslog-ng //#cgo LDFLAGS: -L/usr/local/lib/syslog-ng -ldbparser //#cgo CFLAGS: -I. //void callOnMeGo_cgo(char *key, char *value, size_t value_len); <<<<< // #include "syslog-node1.h" //#include <stdlib.h> import "C"
//export CallBack func CallBack(key *C.char, value *C.char, value_len C.size_t){ if remap, ok := field_mappings[C.GoString(key)]; ok { fmt.Println(key, remap)//Need to check how field mappings work } } func ReloadPatternDB(opts Y){ x := C.CString(opts.x) C.reload_pattern_db(x, (C.key_value_cb)(unsafe.Pointer(C.callOnMeGo_cgo))); } 2)Created a file 'cfuncs.go' *====================* package main /* #include <stdio.h> // The gateway function void callOnMeGo_cgo(char *key, char *value, size_t value_len) { printf("C.callOnMeGo_cgo(): called"); return CallBack(key, value, value_len); } */ import "C" *3)Following is my C header file:* #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 initialize_engine(const char* filename, const char* module_path, key_value_cb cb); int reload_pattern_db(const char* filename, key_value_cb cb); void match(const char* pattern, size_t pattern_len, const char* program, size_t program_len); #endif *4)My make file:============* .PHONY: all clean all: main main: syslog-node.so main.go syslog-node1.h go build -v -x main.go syslog-node.so: syslog-node.c syslog-node1.h cfuncs.go 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/ -I. syslog-node.c clean: rm main rm syslog-node.so *5) When I run the make command, I get the following error * v$ 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/ -I. syslog-node.c go build -v -x main.go WORK=/tmp/go-build729657300 command-line-arguments mkdir -p $WORK/b001/ cd /home/nitish/Documents/goworkspace/src/Syslogng/node-syslog-ng-dev CGO_LDFLAGS='"-g" "-O2" "syslog-node.so" "-L/usr/local/lib/" "-lsyslog-ng" "-L/usr/local/lib/syslog-ng" "-ldbparser"' /usr/local/go/pkg/tool/linux_amd64/cgo -objdir $WORK/b001/ -importpath command-line-arguments -- -I $WORK/b001/ -g -O2 -I/home/nitish/Documents/goworkspace/src/Syslogng/node-syslog-ng-dev/syslog-ng-3.6.2 -I/home/nitish/Documents/goworkspace/src/Syslogng/node-syslog-ng-dev ./main.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/nitish/Documents/goworkspace/src/Syslogng/node-syslog-ng-dev -fPIC -m64 -pthread -fmessage-length=0 -fdebug-prefix-map=$WORK/b001=/tmp/go-build -gno-record-gcc-switches -I ./ -g -O2 -I/home/nitish/Documents/goworkspace/src/Syslogng/node-syslog-ng-dev/syslog-ng-3.6.2 -I/home/nitish/Documents/goworkspace/src/Syslogng/node-syslog-ng-dev -o ./_x001.o -c _cgo_export.c TERM='dumb' gcc -I /home/nitish/Documents/goworkspace/src/Syslogng/node-syslog-ng-dev -fPIC -m64 -pthread -fmessage-length=0 -fdebug-prefix-map=$WORK/b001=/tmp/go-build -gno-record-gcc-switches -I ./ -g -O2 -I/home/nitish/Documents/goworkspace/src/Syslogng/node-syslog-ng-dev/syslog-ng-3.6.2 -I/home/nitish/Documents/goworkspace/src/Syslogng/node-syslog-ng-dev -o ./_x002.o -c main.cgo2.c TERM='dumb' gcc -I /home/nitish/Documents/goworkspace/src/Syslogng/node-syslog-ng-dev -fPIC -m64 -pthread -fmessage-length=0 -fdebug-prefix-map=$WORK/b001=/tmp/go-build -gno-record-gcc-switches -I ./ -g -O2 -I/home/nitish/Documents/goworkspace/src/Syslogng/node-syslog-ng-dev/syslog-ng-3.6.2 -I/home/nitish/Documents/goworkspace/src/Syslogng/node-syslog-ng-dev -o ./_cgo_main.o -c _cgo_main.c cd /home/nitish/Documents/goworkspace/src/Syslogng/node-syslog-ng-dev 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 -g -O2 syslog-node.so -L/usr/local/lib/ -lsyslog-ng -L/usr/local/lib/syslog-ng -ldbparser # command-line-arguments /tmp/go-build729657300/b001/_cgo_main.o:/tmp/go-build/cgo-generated-wrappers:2: undefined reference to `callOnMeGo_cgo' collect2: error: ld returned 1 exit status makefile:6: recipe for target 'main' failed make: *** [main] Error 2 Can someone please point me the what am I missing in my make file or something else in my code base that will resolve this error ? Thanks On Wed, Apr 24, 2019 at 2:35 PM Jan Mercl <0xj...@gmail.com> wrote: > On Wed, Apr 24, 2019 at 10:47 AM Nitish Saboo <nitish.sabo...@gmail.com> > wrote: > > > ./main.go:80:32: cannot use key (type *_Ctype_char) as type string in > map index > > func CallBack(ckey *C.char, value *C.char, value_len C.size_t){ > key := C.GoString(ckey) > if remap, ok := field_mappings[key]; ok { > fmt.Println(key, remap) > } > } > > > > ./main.go:91:21: cannot use CallBack (type func(*_Ctype_char, > *_Ctype_char, _Ctype_ulong)) as type *[0]byte in argument to > _Cfunc_reload_pattern_db > > ./main.go:143:21: cannot use CallBack (type func(*_Ctype_char, > *_Ctype_char, _Ctype_ulong)) as type *[0]byte in argument to > > See https://github.com/golang/go/wiki/cgo#function-variables: > > "Because of the pointer passing rules Go code can not pass a > function value directly to C. " > -- 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. For more options, visit https://groups.google.com/d/optout.