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

使用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?

  • 写回答

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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料
  • ¥15 使用R语言marginaleffects包进行边际效应图绘制