Hi, All,
I have googled quite a bit about this issue, there are some tutorials online. But most of them targeted either at older go releases or C instead of C++.
Can somebody please help me to figure out how to solve the following issues?
I have to use a static library, wrote in C++ in a go project. I have the libcolor.a and the COLOR.h header file, the libcolor.a relies on some other libraries, such as libboost_system.a, libxml2.a etc to build.
I wrote a libcolor.swigcxx file as follows:
---------------------------
%module libcoror
%{
#include "include/COLOR.h"
#include <stddef.h>
#include <vector>
/* This is where we initialize any global parameters that are not search-thread specific */
extern void COLOR_init_global_config(int argc, char *argv[]); // from the COLOR.h file, which is one of the APIs I would like to use
%}
#include "include/COLOR.h"
extern void COLOR_init_global_config(int argc, char *argv[]);
---------------------------
An empty libcolor.go file with the following lines was manually created
---------------------------
package libcolor
// #cgo CFLAGS: -I .
// #cgo CXXFLAGS: -std=c++11 <--- this does not seem to work
// #cgo LDFLAGS: -L${SRCDIR}/lib/ -lCOLOR.a -lz <--- this is placed at the correct place
---------------------------
When I tried to build this using "go build -x" CLI, I hit the following error:
WORK=/tmp/go-build797493895
mkdir -p $WORK/klow/libcolor/_obj/
mkdir -p $WORK/klow/
swig -version
cd $WORK
/usr/local/go/pkg/tool/linux_amd64/compile -o ./klow/libcolor/_obj/_go_.o -trimpath . -p main -complete -buildid 73a7f9534f74346db4b3e0f48875da9dbf8bc2fd -D _$WORK ./swig_intsize.go
cd /home/chzhang/go/src/klow/libcolor
swig -go -cgo -intgosize 64 -module libcolor -o $WORK/klow/libcolor/_obj/libcolor_wrap.cxx -outdir $WORK/klow/libcolor/_obj/ -c++ libcolor.swigcxx
CGO_LDFLAGS="-g" "-O2" /usr/local/go/pkg/tool/linux_amd64/cgo -objdir $WORK/klow/libcolor/_obj/ -importpath klow/libcolor -- -I $WORK/klow/libcolor/_obj/ $WORK/klow/libcolor/_obj/libcolor.go
cd $WORK
gcc -fdebug-prefix-map=a=b -c trivial.c
gcc -gno-record-gcc-switches -c trivial.c
cd /home/chzhang/go/src/klow/libcolor
gcc -I . -fPIC -m64 -pthread -fmessage-length=0 -fdebug-prefix-map=$WORK=/tmp/go-build -gno-record-gcc-switches -I $WORK/klow/libcolor/_obj/ -g -O2 -o $WORK/klow/libcolor/_obj/_cgo_main.o -c $WORK/klow/libcolor/_obj/_cgo_main.c
gcc -I . -fPIC -m64 -pthread -fmessage-length=0 -fdebug-prefix-map=$WORK=/tmp/go-build -gno-record-gcc-switches -I $WORK/klow/libcolor/_obj/ -g -O2 -o $WORK/klow/libcolor/_obj/_cgo_export.o -c $WORK/klow/libcolor/_obj/_cgo_export.c
gcc -I . -fPIC -m64 -pthread -fmessage-length=0 -fdebug-prefix-map=$WORK=/tmp/go-build -gno-record-gcc-switches -I $WORK/klow/libcolor/_obj/ -g -O2 -o $WORK/klow/libcolor/_obj/_tmp_go-build797493895_klow_libcolor__obj_libcolor.cgo2.o -c $WORK/klow/libcolor/_obj/_tmp_go-build797493895_klow_libcolor__obj_libcolor.cgo2.c
g++ -I . -fPIC -m64 -pthread -fmessage-length=0 -fdebug-prefix-map=$WORK=/tmp/go-build -gno-record-gcc-switches -I $WORK/klow/libcolor/_obj/ -g -O2 -o $WORK/klow/libcolor/_obj/_tmp_go-build797493895_klow_libcolor__obj_libcolor_wrap.cxx.o -c $WORK/klow/libcolor/_obj/libcolor_wrap.cxx
# klow/libcolor
In file included from $WORK/klow/libcolor/_obj/libcolor_wrap.cxx:243:0:
./include/COLOR.h:13:43: warning: defaulted and deleted functions only available with -std=c++11 or -std=gnu++11
PerSessionData(const PerSessionData &d)=default;
^
./include/COLOR.h:14:53: warning: defaulted and deleted functions only available with -std=c++11 or -std=gnu++11
PerSessionData& operator=(const PerSessionData&d)=default;
^
g++ -I . -fPIC -m64 -pthread -fmessage-length=0 -fdebug-prefix-map=$WORK=/tmp/go-build -gno-record-gcc-switches -o $WORK/klow/libcolor/_obj/_cgo_.o $WORK/klow/libcolor/_obj/_cgo_main.o $WORK/klow/libcolor/_obj/_cgo_export.o $WORK/klow/libcolor/_obj/_tmp_go-build797493895_klow_libcolor__obj_libcolor.cgo2.o $WORK/klow/libcolor/_obj/_tmp_go-build797493895_klow_libcolor__obj_libcolor_wrap.cxx.o -g -O2
# klow/libcolor
/tmp/go-build797493895/klow/libcolor/_obj/_tmp_go-build797493895_klow_libcolor__obj_libcolor_wrap.cxx.o: In function `_wrap_COLOR_init_global_config_libcolor_3cea422eb6211fe0':
/tmp/go-build/klow/libcolor/_obj/libcolor_wrap.cxx:285: undefined reference to `COLOR_init_global_config(int, char**)'
collect2: error: ld returned 1 exit status
Looks like there are two errors:
1, the C++11 warning
2, the linker can't find the function COLOR_init_global_config in the static library. That means the compiler directives I defined in libcolor.go was not successfully passed to the compiler. From the log, looks like another libcolor.go was generated in the $WORK directory by swig. The one I manually created was not used at all.
Can somebody please help me to figure out what I need to do to get this compiled? The tutorial on
swig.org does not seem to help. I tried to manually build as in
But the 6c etc go tools does not exist anymore.
Thanks,
Chun
--------------------------------
This is my setup
go version go1.7.1 linux/amd64
GOARCH="amd64"
GOBIN=""
GOEXE=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOOS="linux"
GOPATH="/home/chzhang/go"
GORACE=""
GOROOT="/usr/local/go"
GOTOOLDIR="/usr/local/go/pkg/tool/linux_amd64"
CC="gcc"
GOGCCFLAGS="-fPIC -m64 -pthread -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build155119108=/tmp/go-build -gno-record-gcc-switches"
CXX="g++"
CGO_ENABLED="1"