Can I change default compiler used by cgo?
Asked Answered
P

2

10

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!

Pyrazole answered 30/6, 2017 at 23:53 Comment(2)
just a friendly reminder Cgo is not Go dave.cheney.net/2016/01/18/cgo-is-not-go blog.golang.org/c-go-cgoStupa
@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 :)Pyrazole
I
8

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

go env -w "CC=clang"

Inverter answered 26/4, 2022 at 11:19 Comment(1)
This is actually a good answerResilience
S
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
Sadomasochism answered 9/4, 2021 at 0:24 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.