douwen5741 2013-06-05 04:41
浏览 310
已采纳

是否可以在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条回答 默认 最新

  • dongmeng1875 2013-06-10 13:11
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 fluent的在模拟压强时使用希望得到一些建议
  • ¥15 STM32驱动继电器
  • ¥15 Windows server update services
  • ¥15 关于#c语言#的问题:我现在在做一个墨水屏设计,2.9英寸的小屏怎么换4.2英寸大屏
  • ¥15 模糊pid与pid仿真结果几乎一样
  • ¥15 java的GUI的运用
  • ¥15 Web.config连不上数据库
  • ¥15 我想付费需要AKM公司DSP开发资料及相关开发。
  • ¥15 怎么配置广告联盟瀑布流
  • ¥15 Rstudio 保存代码闪退