假设我有一个 golang 包,其中包含一些汇编代码:
demopkg/
source1.go
source2.go
asm_amd64.s
如果我尝试使用 go build
构建它,工具链将使用 go tool asm
来组装 *.s 文件。
但是如果我将 Cgo 添加到混合物中,通过将单个 import "C"
放入任何源代码,go 将切换到 gcc 汇编程序。
我可以通过执行 go build -n
看到它。第一种情况下对 /usr/local/go/pkg/tool/linux_amd64/asm
的调用被对 gcc
的调用所取代。除此之外,它开始提示语法错误。
是否记录了此行为,以便我可以依靠它来维护我的包?我可以强制 go build
使用一个确切的汇编器吗?
最佳答案
是的,它在 cgo documentation 中
When the Go tool sees that one or more Go files use the special import "C", it will look for other non-Go files in the directory and compile them as part of the Go package. Any .c, .s, or .S files will be compiled with the C compiler. Any .cc, .cpp, or .cxx files will be compiled with the C++ compiler. Any .h, .hh, .hpp, or .hxx files will not be compiled separately, but, if these header files are changed, the C and C++ files will be recompiled. The default C and C++ compilers may be changed by the CC and CXX environment variables, respectively; those environment variables may include command line options.
关于gcc - golang 在使用和不使用 cgo 进行构建时使用的汇编程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35587395/