douhuan2101 2017-03-14 10:56
浏览 121
已采纳

用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:

  1. ld: symbol(s) not found for architecture x86_64
  2. 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:

  1. LSL
  2. ├── liblsl
  3. │   ├── bin
  4. │   ├── distros
  5. │   ├── examples
  6. │   ├── external
  7. │   ├── include
  8. │   ├── project
  9. │   ├── src
  10. │   └── testing
  11. └── liblsl-Generic
  12.    ├── AUTOGENERATE\ HOWTO.txt
  13.    ├── examples
  14.    ├── liblsl.swigcxx
  15.    ├── liblsl_c.i
  16.    ├── liblsl_cpp.i
  17. ├── liblsl_wrap.cxx # created by Swig
  18. └── liblsl.go # created by Swig

展开全部

  • 写回答

2条回答 默认 最新

  • dongzhi4470 2017-03-15 00: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 C#i编程中so-ir-192编码的字符集转码UTF8问题
  • ¥15 51嵌入式入门按键小项目
  • ¥30 海外项目,如何降低Google Map接口费用?
  • ¥15 fluentmeshing
  • ¥15 手机/平板的浏览器里如何实现类似荧光笔的效果
  • ¥15 盘古气象大模型调用(python)
  • ¥15 传人记程序做的plc 485从机程序该如何写
  • ¥15 已知手指抓握过程中掌指关节、手指各关节和指尖每一帧的坐标,用贝塞尔曲线可以拟合手指抓握的运动轨迹吗?
  • ¥50 libwebsockets 如何添加其他socket事件回调
  • ¥50 实现画布拖拽算子排布,通过flink实现算子编排计算,请提供思路
手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部