SWIG can generate interface for other languages to call C++ functions. If C++ codes are separeted to mutiple directories, we may have some trouble.
- Prepare directories like this.
- invoke
- invoke.cpp
- invoke.hpp
- lib.cpp
- lib.hpp
- lib.i
%module lib
%{
#include "lib.hpp"
%}
%include "lib.hpp"
Other files are described in go_cpp_linkage.md
.
- Create shared library
clang++ invoke.cpp -shared -fPIC -o libinvoke.so
- Genarate glue codes
swig -go -c++ -cgo -intgosize 64 lib.i
- Add to enable dynamic linking to
lib.go
・
・
・
// source: lib/lib.i
package lib
/*
#cgo LDFLAGS: -L ./ -linvoke -ldl
・
・
・
- Check your
LD_LIBRARY_PATH
. If it is not included the location oflibinvoke.so
, you must add them to it.
export LD_LIBRARY_PATH=~/go/src/github.com/Shikugawa/go-cpp-ffi/lib:$LD_LIBRARY_PATH
- Build