douweibiao8471
2016-02-23 20:22
浏览 297
已采纳

使用cgo和不使用cgo进行编译时,golang使用的汇编程序

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?

图片转代码服务由CSDN问答提供 功能建议

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

  demopkg / 
 source1.go 
 source2.go 
 asm_amd64.s 
   
 
 

如果我尝试使用 go build进行构建 ,工具链将使用 go工具asm 来组装* .s文件。

但是,如果我将Cgo添加到混合物中,只需放置一个 导入“ C” 到任何源中,转到将切换到gcc汇编器。

我可以通过执行 go build -n 。 从第一种情况到 / usr / local / go / pkg / tool / linux_amd64 / asm 的调用被替换为 gcc 的调用。 除此之外,它还会开始抱怨语法损坏。

此行为是否已记录在案,因此我可以依靠它来维护我的软件包? 我可以强制 go build 使用一个确切的汇编程序吗?

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • dourong4031 2016-02-23 20:35
    已采纳

    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.

    已采纳该答案
    打赏 评论

相关推荐 更多相似问题