golang 在使用和不使用 cgo 进行构建时使用的汇编器

Assembler used by golang when building with and without cgo

假设我有一个 golang 包,其中包含一些汇编代码:

 demopkg/
   source1.go
   source2.go
   asm_amd64.s

如果我尝试使用 go build 构建它,工具链将使用 go tool asm 到 assemble *.s 文件。

但是如果我将 Cgo 添加到混合物中,通过将单个 import "C" 放入任何来源,go 将切换到 gcc assembler.

执行go build -n就能看到了。第一个案例中对 /usr/local/go/pkg/tool/linux_amd64/asm 的调用被对 gcc 的调用所取代。除此之外,它开始抱怨语法错误。

是否记录了此行为,以便我可以依靠它来维护我的包?我可以强制 go build 使用一个精确的 assembler 吗?

是的,它在 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.