douxie7738 2017-12-19 23:02 采纳率: 0%
浏览 91

如何将C ++库与CGO和Swig链接?

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?

  • 写回答

1条回答 默认 最新

  • douzhanglun4482 2017-12-20 00:09
    关注

    This fixed it for me:

    // #cgo LDFLAGS: -L${SRCDIR} -lxmr-stak-backend
    import "C"
    

    Seems like I was just using ld wrong.

    评论

报告相同问题?

悬赏问题

  • ¥15 如何用python向钉钉机器人发送可以放大的图片?
  • ¥15 vue3加ant-design-vue无法渲染出页面
  • ¥15 matlab(相关搜索:紧聚焦)
  • ¥15 基于51单片机的厨房煤气泄露检测报警系统设计
  • ¥15 路易威登官网 里边的参数逆向
  • ¥15 Arduino无法同时连接多个hx711模块,如何解决?
  • ¥50 需求一个up主付费课程
  • ¥20 模型在y分布之外的数据上预测能力不好如何解决
  • ¥15 processing提取音乐节奏
  • ¥15 gg加速器加速游戏时,提示不是x86架构