building a linux binary with -race on a mac

448 views
Skip to first unread message

Joseph Lorenzini

unread,
Feb 15, 2017, 7:35:49 AM2/15/17
to golang-nuts
All:

I am writing and building my code on a mac. The code is a linux binary though so its obviously a cross compilation. This works fine:

GOOS=linux GOARCH=amd64  go build . 

This does not:

GOOS=linux GOARCH=amd64  go build -race . 
go build: -race requires cgo; enable cgo by setting CGO_ENABLED=1

Nor does this:

 GOOS=linux GOARCH=amd64  CGO_ENABLED=1 go build -race . 
# runtime/cgo
ld: unknown option: --build-id=none
clang: error: linker command failed with exit code 1 (use -v to see invocation)



Go env below:

GOARCH="amd64"

GOBIN="/Users/jlorenzini/go/bin"

GOEXE=""

GOHOSTARCH="amd64"

GOHOSTOS="darwin"

GOOS="darwin"

GOPATH="/Users/jlorenzini/go"

GORACE=""

GOROOT="/usr/local/go"

GOTOOLDIR="/usr/local/go/pkg/tool/darwin_amd64"

CC="clang"

GOGCCFLAGS="-fPIC -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/var/folders/8w/7ndx_j6s36x9hm9dns67j21s3hynqn/T/go-build987997970=/tmp/go-build -gno-record-gcc-switches -fno-common"

CXX="clang++"

CGO_ENABLED="1"

Ian Lance Taylor

unread,
Feb 15, 2017, 9:56:00 AM2/15/17
to Joseph Lorenzini, golang-nuts
On Wed, Feb 15, 2017 at 4:35 AM, Joseph Lorenzini <jal...@gmail.com> wrote:
>
> I am writing and building my code on a mac. The code is a linux binary
> though so its obviously a cross compilation. This works fine:
>
> GOOS=linux GOARCH=amd64 go build .
>
> This does not:
>
> GOOS=linux GOARCH=amd64 go build -race .
> go build: -race requires cgo; enable cgo by setting CGO_ENABLED=1
>
> Nor does this:
>
> GOOS=linux GOARCH=amd64 CGO_ENABLED=1 go build -race .
> # runtime/cgo
> ld: unknown option: --build-id=none
> clang: error: linker command failed with exit code 1 (use -v to see
> invocation)

The race detector depends on a module written in C++, and requires the
C++ linker to do the final link. In order to do what you want, you
will need a Darwin -> GNU/Linux C++ cross-compiler. You can get one
by building GCC or clang in the appropriate way. When you have that,
set the CC and CXX environment variables to point to your
cross-compiler.

Ian
Reply all
Reply to author
Forward
0 new messages