douhuan2101 2017-03-14 18:56
浏览 120
已采纳

用Go构建包装C ++

I am trying to wrap C++ code (LabStreamingLayer) in Go.

Update: @dragonx explained how to use go build without swig. But I am still running into a linker issue. The build depends on LSL/liblsl/bin/liblsl.dylib. How do I tell go build to use that file? I tried go build -ldflags "-L ../liblsl/bin -l lsl" app.go with no success.

The Go documentation says that go build will invoke Swig with the c++ option for files with the .swigcxx extension, but go build complains that there are no buildable Go source files in the directory.

  • Platform: Darwin
  • Go version: 1.8
  • Swig version: 3.0.12
  • clang version: 8.0.0

Here are the steps I took to arrive at that error:

  1. Clone the labstreaminglayer repo.
  2. Rename the file liblsl_cpp.i to liblsl.swigcxx (I thought this would tell go build that the file should be used with swig).
  3. cd into LSL/liblsl-Generic and run go build. Go complains that there are no buildable Go source files in this directory.

After that failed, I tried using Swig. I ran swig -c++ -go -cgo -intgosize 64 liblsl_cpp.i, which created a .go file. I then ran go build in that directory, but it raised the error:

ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1

I am not familiar with C++, so I am not sure how to resolve the linker issue. I do know that this C++ code requires the file LSL/liblsl/bin/liblsl64.dylib. I assume that is the file that must be linked?

How can I wrap this C++ code in Go?

Here is the file structure:

LSL
├── liblsl
│   ├── bin
│   ├── distros
│   ├── examples
│   ├── external
│   ├── include
│   ├── project
│   ├── src
│   └── testing
└── liblsl-Generic
    ├── AUTOGENERATE\ HOWTO.txt
    ├── examples
    ├── liblsl.swigcxx
    ├── liblsl_c.i
    ├── liblsl_cpp.i
    ├── liblsl_wrap.cxx  # created by Swig
    └── liblsl.go        # created by Swig
  • 写回答

2条回答 默认 最新

  • dongzhi4470 2017-03-15 08:00
    关注

    I ran into this a few months ago, took longer to figure out than I would have liked, but I don't remember exactly what I did to fix it, but I think it was along these lines:

    1. Don't call swig manually, it actually makes things a bit more difficult. Clean out the files generate by swig.
    2. Create a file a.go in libsl-Generic with the contents:

      package libsls-Generic
      
    3. In theory you have some other file, say app.go that will use libsls-Generic. Write app.go and import libsls-Generic appropriately.
    4. Use go build app.go. This should build the dependency as well.

    I eventually figured out how to get go to incorporate the files manually generated by swig, but I forget the details now. I do remember that when generating files manually with swig, I would have to manually delete certain files when rebuilding. When running with just go, the build step was much simpler.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥20 测距传感器数据手册i2c