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

huangapple go评论78阅读模式
英文:

Override an external package's cgo compiler and linker flags?

问题

假设我想使用一些很棒的Go包。我可以通过以下方式引入它:

import "github.com/really-awesome/project/foobar"

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

#cgo windows CFLAGS: -I C:/some-path/Include
#cgo windows LDFLAGS: -L C:/some-path/Lib -lfoobar

但是,如果我在其他地方安装了foobar的C依赖项,我真的需要修改这些行,使其指向正确的路径:

#cgo windows CFLAGS: -I C:/different-path/Include
#cgo windows LDFLAGS: -L C:/different-path/Lib -lfoobar

有没有办法覆盖或修改cgo查找这些依赖项的路径?目前,我的解决方法是在运行go get ./...之后手动编辑这两行,该命令会获取github.com/really-awesome/project/foobar的代码。

注意:我正在使用MinGW编译器,尽管我觉得这并不重要。

更新:

我尝试使用go build添加标志,但没有成功:

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"

使用-x参数,我可以看到打印出的标志,并且它们不包括我在命令行上设置的标志。也许外部Go包顶部的#cgo CFLAGS/LDFLAGS语句覆盖了我设置的标志...

英文:

Let's say I want to use some awesome go package. I can include it by:

import "github.com/really-awesome/project/foobar"

And inside that project's foobar.go file, it defines some cgo instructions like:

#cgo windows CFLAGS: -I C:/some-path/Include
#cgo windows LDFLAGS: -L C:/some-path/Lib -lfoobar

But if I have that foobar C dependency installed somewhere else, I would really need those lines to say:

#cgo windows CFLAGS: -I C:/different-path/Include
#cgo windows LDFLAGS: -L C:/different-path/Lib -lfoobar

Is there a way to override or trump where cgo is looking for these dependencies? Right now my fix is to manually edit those two lines after running go get ./... which will fetch the github.comreally-awesome/project/foobar code.

NOTE: I'm using the MinGw compiler, though I doubt that matters.

update:

I have tried adding flags to go build to no avail:

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"

With the -x argument I see the printout of flags and they don't include the ones I am setting on the command line. Perhaps the #cgo CFLAGS/LDFLAGS statements at the top of the external go package squash what I am telling it to use...

答案1

得分: 8

你可以通过设置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"
英文:

You can do this by setting the CGO_CPPFLAGS and CGO_LDFLAGS environment variables.

For example, on my MacBook, Homebrew is installed in ~/.homebrew (instead of /usr/local), so when I try to go get packages with native bindings they can't find the headers and libs.

To fix that I added these two lines to my ~/.zshenv file:

export CGO_CPPFLAGS="-I $BREW_HOME/include"
export CGO_LDFLAGS="-L $BREW_HOME/lib"

答案2

得分: 2

这种角色可以由#cgo pkgconfig: foobar来完成。如果库是以这种方式编写的,它将从foobar的pkgconfig定义中获取正确的路径。

我意识到这不是对问题的直接回答,而且pkgconfig并不是一个本地的Windows工具...如果有其他解决方案,我会很感兴趣听到。

英文:

This is kind of the role filled by #cgo pkgconfig: foobar. If the library had been written that way, it would pick up the correct paths from foobar's pkgconfig definition.

I realise its not a direct answer to the question, and that pkgconfig isn't exactly a native windows tool... I'd be interested to hear if any other solutions exist.

huangapple
  • 本文由 发表于 2015年2月25日 10:52:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/28710276.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定