用于检测CGO构建的预处理器标志是什么?

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

Preprocessor flag to detect CGO build?

问题

在与我的go文件并通过CGO一起编译的c文件中,我想通过预处理器检查它是否是通过go编译的。我想这样做是因为,例如,我想用#ifdef标志保护#include _cgo_export.h,因为这样的头文件仅在编译期间存在,我不希望我的编辑器警告它不存在。

英文:

In a c file that's beside my go file and is compiled together via CGO, I'd like to check via preprocessor whether it's being compiled via go or not. I'd like to do this because, for example, I'd like to protect #include _cgo_export.h with #ifdef flags, since such header exists solely during compilation and I don't want my editor to warn about its absence.

答案1

得分: 3

文档中可以看到:

// #cgo CFLAGS: -DWHATEVER_YOU_WANT_TO_INDICATE_CGO=1
import "C"

(如果你不想要一个值,只需使用-D FOO)或者在环境中设置CGO_CFLAGS

你可以通过使用go build -x来查看后台发生了什么。
对我来说,它在编译cgo生成的_cgo_defun.c文件时显示了-D GOOS_freebsd -D GOARCH_amd64,但仅限于该文件,而不适用于我的*.c文件。因此,我认为没有可用的预定义预处理器标志(文档中也没有提到任何标志)。

英文:

From the documentation just do:

// #cgo CFLAGS: -DWHATEVER_YOU_WANT_TO_INDICATE_CGO=1
import "C"

(or just -D FOO if you don't want a value) or set CGO_CFLAGS in the environment.

You can see what is happening behind the scenes with go build -x.
For me it shows -D GOOS_freebsd -D GOARCH_amd64 while compiling the cgo generated _cgo_defun.c file, but only for that file and not to my own *.c files. So I don't think there are any usable predefined preprocessor flags (and the documentation also doesn't mention any).

huangapple
  • 本文由 发表于 2015年3月12日 00:39:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/28992375.html
匿名

发表评论

匿名网友

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

确定