I want to use the C++ program xmr-stak in my Go program. After some research, I settled on SWIG to generate the bindings. The issue is that ld cannot find the library file. I'll just write down step by step what I did.
I created this file at src/miner/xmrstak/xmrstak.swigcxx
along with the source code of xmr-stak:
%module xmrstak
%{
#include "xmrstak/net/jpsock.hpp"
#include "xmrstak/jconf.hpp"
#include "xmrstak/misc/executor.hpp"
%}
%include "xmrstak/misc/executor.hpp"
Since Swig is supported by Go, it will automatically generate the bindings at build time. But Go still requires a package name, so I've just created package.go
:
package xmrstak
Now, I can build this project, which results in lots of errors like:
/tmp/go-build176223381/gitlab.com/jgillich/autominer/miner/xmrstak/_obj/xmrstak_wrap.cxx.o: In function `executor::inst()':
./xmrstak/misc/executor.hpp:35: undefined reference to `executor::executor()'
To fix this, it appears I need to tell ld to link the library. I've added the following to my package.go
:
// #cgo LDFLAGS: -L${SRCDIR}/xmrstak/ -l${SRCDIR}/libxmr-stak-backend.a
import "C"
And copied the libxmr-stak-backend.a
in the same folder. This file is obtained by building xmr-stak using cmake . && make
and should include everything I need (for now).
My issue is that this doesn't actually work. I get the following output:
$ go install
# gitlab.com/jgillich/autominer/miner/xmrstak
xmrstak/misc/executor.hpp:43: Warning 507: No Go typemap defined for ex_event &&
[further warnings ommitted]
# gitlab.com/jgillich/autominer/miner/xmrstak
/usr/bin/ld: cannot find -l/home/jgillich/go/src/gitlab.com/jgillich/autominer/miner/xmrstak/libxmr-stak-backend.a
collect2: error: ld returned 1 exit status
The file however definitely exists:
$ ls
libxmr-stak-backend.a package.go xmrstak xmrstak.swigcxx
$ file /home/jgillich/go/src/gitlab.com/jgillich/autominer/miner/xmrstak/libxmr-stak-backend.a
/home/jgillich/go/src/gitlab.com/jgillich/autominer/miner/xmrstak/libxmr-stak-backend.a: current ar archive
Did I do anything wrong here?