是否可以在cgo CFLAGS注释中使用环境变量?

I'm attempting to write some C bindings for the Go language, and have run into a sort of sticky situation when setting up the Cgo compilation in Windows. I have code that looks like the following:

// #cgo windows CFLAGS: -I C:/dev/extlibs/include/
// #cgo windows LDFLAGS: -lMyLib -L C:/dev/extlibs/lib/
// #include <mylib/mylib.h>
import "C"

which is allows me to avoid installing Dlls, libs, and header files directly into my C:\Windows directory, but doesn't allow for much flexibility when other developers are working with a different file system setup (they all need the libs to be in C:/dev/extlibs/...).

Is there a way I could referent an environment variable from within the code? Perhaps something along the lines of:

// #cgo windows CFLAGS: -I $EXTLIBS$/include/

Or is there another way that people solve this issue that I have missed? I've spent some time googling around on this subject and haven't seen much that has been useful, so any information and/or resources could be a real help!

2个回答



您可以使用环境变量 CPATH </ code> 环境变量,其中:</ p>


指定要搜索的目录列表,就像使用-I指定一样,但是在-I选项给定的任何路径之后 命令行。 无论使用哪种语言进行预处理,都将使用此环境变量。</ p>
</ blockquote>

我认为 -L </ code>的等效项是 LIBRARY_PATH </ code>(在 CPATH </ code>链接中描述)。</ p>

根据 http://golang.org/cmd/cgo/ 一种推荐的以独立于平台的方式解决此问题的方法是使用 pkg-config 。</ p>

  // #cgo pkg-config:mylib otherlib 
</ code> </ pre>

它可用于Windows( http://ftp.gnome.org/pub/gnome/binaries/win32/dependencies/ ),有关在以下位置安装它的更多信息 此问题(如何在Windows中安装pkg config indows?) </ p>

除此之外,请将所有依赖项放入go-code的子目录中,并在您的 CFLAGS </ code>中使用相对路径 和 LDFLAGS </ code>,并与其他开发人员共享整个捆绑软件。</ p>
</ div>

展开原文

原文

You could try using environment variables, the Gentoo Linux Wiki page on Safe C Flags has an example in the following format

CXXFLAGS="${CFLAGS}"

So you may be able to do something like

// #cgo windows CFLAGS: -I "${EXTLIBS}"/include/

but my syntax may be off, and that may be Makefile specific.

You could also try setting a CPATH environment variable which:

specifies a list of directories to be searched as if specified with -I, but after any paths given with -I options on the command line. This environment variable is used regardless of which language is being preprocessed.

The equivalent for -L is, I think, LIBRARY_PATH (Described at the CPATH link).

According to http://golang.org/cmd/cgo/ one sort of recommended way to get around this in a platform independant way is to use pkg-config.

// #cgo pkg-config: mylib otherlib

It's available for windows (http://ftp.gnome.org/pub/gnome/binaries/win32/dependencies/) and there's some more information on installing it at this question (How to install pkg config in windows?)

Other than that, put all the dependencies into a sub-directory of the go-code, use relative paths in your CFLAGS and LDFLAGS, and share the entire bundle with other developers.

dongshi8359
dongshi8359 FWIW,我想我将尝试pkg-config,看看它是否比上一次更好。 感谢您的资源!
7 年多之前 回复
dongzuan4491
dongzuan4491 是的,我花了一些时间才整理出对Win8的依赖关系:-(尽管最后,链接的SO问题在注释中有一些很好的链接。
7 年多之前 回复
dprnr5559
dprnr5559 这些是一些好主意。 我会尝试的。 FWIW,我在darwin / linux上使用pk-config,但它似乎总是比过去在Windows上值得的麻烦。
7 年多之前 回复

According to the docs for CGO:

When building, the CGO_CFLAGS, CGO_CPPFLAGS, CGO_CXXFLAGS and CGO_LDFLAGS environment variables are added to the flags derived from these directives. Package-specific flags should be set using the directives, not the environment variables, so that builds work in unmodified environments.

Using this knowledge, I have had success building a third-party package that wraps a C library so long as it provides it as a system package. The example I linked to:

package sdl

// #cgo LDFLAGS: -lSDL2
// #include <SDL2/SDL.h>
import "C"

Even though it specifies a system package for SDL2, and I have SDL2 installed in some non-system directory, I am able to still build this package using some of the environment variables I mentioned, such as in the following:

export SDL_PATH=/home/mark/where/I/installed/sdl
CGO_CFLAGS="-I$SDL_PATH/include" CGO_LDFLAGS="-L$SDL_PATH/lib" go build hello.go
LD_LIBRARY_PATH="$SDL_PATH/lib" ./hello

Of course, this is Linux, but you can probably use the same ideas in Windows.

立即提问