英文:
Assembler used by golang when building with and without cgo
问题
假设我有一个包含一些汇编代码的 Golang 包:
demopkg/
source1.go
source2.go
asm_amd64.s
如果我尝试使用 go build
来构建它,工具链将使用 go tool asm
来汇编 *.s 文件。
但是,如果我在其中任何一个源文件中添加了 import "C"
,将 Cgo 添加到混合中,Go 将切换到使用 gcc 汇编器。
我可以通过执行 go build -n
来看到这一点。在第一种情况下,对 /usr/local/go/pkg/tool/linux_amd64/asm
的调用被替换为对 gcc
的调用。除此之外,它还开始抱怨语法错误。
这种行为是否有文档记录,以便我可以依赖它来维护我的包?我可以强制 go build
使用一个确切的汇编器吗?
英文:
Let's say I have a golang package, which contains some assembly code:
demopkg/
source1.go
source2.go
asm_amd64.s
If I try to build it using go build
, toolchain will use go tool asm
to assemble the *.s files.
But if I add Cgo to the mixture, by putting a single import "C"
into any of the sources, go will switch to gcc assembler.
I can see it by executing go build -n
. Calls to the /usr/local/go/pkg/tool/linux_amd64/asm
from the first case get replaced by calls to gcc
. Besides that, it starts complaining about broken syntax.
Is this behaviour documented, so I can rely on it for the maintaining of my package? Can I force go build
to use one exact assembler?
答案1
得分: 2
是的,这在cgo文档中有说明:
当Go工具发现一个或多个Go文件使用特殊的导入"C"时,它会在目录中查找其他非Go文件,并将它们编译为Go包的一部分。任何.c、.s或.S文件将使用C编译器进行编译。任何.cc、.cpp或.cxx文件将使用C++编译器进行编译。任何.h、.hh、.hpp或.hxx文件将不会单独编译,但如果这些头文件发生更改,C和C++文件将被重新编译。默认的C和C++编译器可以通过CC和CXX环境变量进行更改,这些环境变量可以包含命令行选项。
英文:
Yes, it's in the 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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论