'Can I change default compiler used by cgo?

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!



Solution 1:[1]

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

Solution 2:[2]

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

go env -w "CC=clang"

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 James Fennell
Solution 2 nlassaux