英文:
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).
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论