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

使用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 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog
  • ¥15 Excel发现不可读取的内容
  • ¥15 关于#stm32#的问题:CANOpen的PDO同步传输问题