dongmu1951 2014-12-05 13:41
浏览 6
已采纳

如何在Go中包含来自第三方程序包的标头?

Let's supose my package workspace has github.com/yada/yada third party package. Inside this package there is a yoda.go.h header I would like to reuse (not sure if it's a good idea, but that's a hole new question). How do I import the header from a dependecy package into my own package?

package main

// #cgo pkg-config: my-dep other-dep
// #include <someHeader.h>
// #include <otherHeader.h>
// #include github.com/yada/yada/yoda.go.h // doesn't work :(
import "C"

Apart from being a good idea or not, I still would like to know if it's possible.

PS: If you think it's really a bad idea, what should I do instead?

  • 写回答

2条回答 默认 最新

  • dpp3047 2014-12-05 17:29
    关注

    Use CGO CFLAGS directive to reference additional include path.

    //#cgo CFLAGS: -I $GOPATH/src/github.com/yada/yada/
    ...
    //#include "yoda.go.h"
    import "C"
    

    CORRECTION:

    The go tool does not expand $GOPATH variable during build. Instead, the full path should be used there. Corrected code:

    //#cgo CFLAGS: -I /full/path/to/src/github.com/yada/yada/
    //#include "yoda.go.h"
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 关于smbclient 库的使用
  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画