9

I am trying to execute cgo code under ubuntu 14.04, it seems like cgo assume CC/CXX to be gcc/g++. And I need to explicitly specify CC/CXX in order to use, say, clang. Can I configure default compiler used through go's build constraints?

Thanks!

Xiaoqin Zhu
  • 101
  • 2
  • 4
  • 1
    just a friendly reminder Cgo is not Go https://dave.cheney.net/2016/01/18/cgo-is-not-go https://blog.golang.org/c-go-cgo – Yandry Pozo Jul 01 '17 at 01:19
  • @YandryPozo Appreciated, just experienced "Combing Go code and C code results in the intersection of both worlds, not the union; the memory safety of C, and the debuggability of a Go program." in past few days... although I love the first paragraph most :) – Xiaoqin Zhu Jul 01 '17 at 06:50

2 Answers2

6

The C or C++ compiler used by cgo can be specified using the CC and CXX environment variables respectively. For example, to use Clang:

CC=clang go build path/to/cgo/dependent/code.go

The variables can also specify flags to be passed to the compilers; for example, to run GCC with optimizations:

CC="gcc -O2" go build path/to/cgo/dependent/code.go
James Fennell
  • 340
  • 2
  • 8
4

You can specify which compiler to use by setting the CC environment variable:

go env -w "CC=clang"

nlassaux
  • 2,335
  • 2
  • 21
  • 35
Hilbert
  • 51
  • 3