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 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵