覆盖外部包的cgo编译器和链接器标志?

jCu*_*uga 7 go cgo

假设我想使用一些很棒的go包.我可以通过以下方式加入:

import "github.com/really-awesome/project/foobar"
Run Code Online (Sandbox Code Playgroud)

在该项目的foobar.go文件中,它定义了一些cgo指令,如:

#cgo windows CFLAGS: -I C:/some-path/Include
#cgo windows LDFLAGS: -L C:/some-path/Lib -lfoobar
Run Code Online (Sandbox Code Playgroud)

但如果我在foobar其他地方安装了C依赖项,我真的需要这些行说:

#cgo windows CFLAGS: -I C:/different-path/Include
#cgo windows LDFLAGS: -L C:/different-path/Lib -lfoobar
Run Code Online (Sandbox Code Playgroud)

有没有办法覆盖或特朗普在哪里cgo寻找这些依赖?现在我的修复是在运行后手动编辑这两行,go get ./...这将获取github.comreally-awesome/project/foobar代码.

注意:我正在使用MinGw编译器,但我怀疑这很重要.

更新:

我试过添加标志来构建无济于事:

go build -x -gcflags="-I C:/different/include -L C:/different-path/lib -lfoobar"
go build -x -ccflags="-I C:/different/include" -ldflags="-L C:/different-path/lib -lfoobar"
Run Code Online (Sandbox Code Playgroud)

通过-x参数,我看到了标志的打印输出,它们不包括我在命令行上设置的标志.也许#cgo CFLAGS/LDFLAGS外部go包顶部的陈述压制我告诉它使用的...

小智 5

您可以通过设置CGO_CPPFLAGSCGO_LDFLAGS环境变量来做到这一点。

例如,在我的 MacBook 上,Homebrew 安装在~/.homebrew(而不是/usr/local)中,因此当我尝试go get使用本机绑定打包时,他们找不到标头和库。

为了解决这个问题,我将这两行添加到我的~/.zshenv文件中:

export CGO_CPPFLAGS="-I $BREW_HOME/include"
export CGO_LDFLAGS="-L $BREW_HOME/lib"
Run Code Online (Sandbox Code Playgroud)


sqw*_*eek 2

这是由 担任的角色#cgo pkgconfig: foobar。如果库是这样编写的,它将从 foobar 的 pkgconfig 定义中获取正确的路径。

我意识到它不是问题的直接答案,并且 pkgconfig 不完全是一个本机 Windows 工具...我很想听听是否存在任何其他解决方案。