Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

corss compile failed from when build on a mac #560

Closed
log225 opened this issue May 17, 2018 · 9 comments
Closed

corss compile failed from when build on a mac #560

log225 opened this issue May 17, 2018 · 9 comments

Comments

@log225
Copy link

log225 commented May 17, 2018

go env below :

GOARCH="amd64"
GOBIN=""
GOCACHE="/Users/cable/Library/Caches/go-build"
GOEXE=""
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GOOS="darwin"
GOPATH="/Users/cable/workspace/go/"
GORACE=""
GOROOT="/usr/local/Cellar/go/1.10.1/libexec"
GOTMPDIR=""
GOTOOLDIR="/usr/local/Cellar/go/1.10.1/libexec/pkg/tool/darwin_amd64"
GCCGO="gccgo"
CC="clang"
CXX="clang++"
CGO_ENABLED="1"
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/var/folders/k0/0qkltxj92tx3f8jd8dqdsxp80000gn/T/go-build490249343=/tmp/go-build -gno-record-gcc-switches -fno-common"

and I input

CGO_ENABLED=1 GOOS=linux GOARCH=amd64 go build

the error is:

/usr/local/Cellar/go/1.10.1/libexec/pkg/tool/darwin_amd64/link: running gcc failed: exit status 1
clang: warning: argument unused during compilation: '-nopie' [-Wunused-command-line-argument]
ld: warning: ignoring file /var/folders/k0/0qkltxj92tx3f8jd8dqdsxp80000gn/T/go-link-890000680/go.o, file was built for unsupported file format ( 0x7F 0x45 0x4C 0x46 0x02 0x01 0x01 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 ) which is not the architecture being linked (x86_64): /var/folders/k0/0qkltxj92tx3f8jd8dqdsxp80000gn/T/go-link-890000680/go.o
Undefined symbols for architecture x86_64:
  "__cgo_topofstack", referenced from:
      __cgo_145d76c7925b_Cfunc_sqlite3_backup_finish in 000001.o
      __cgo_145d76c7925b_Cfunc_sqlite3_backup_init in 000001.o
      __cgo_145d76c7925b_Cfunc_sqlite3_backup_pagecount in 000001.o
      __cgo_145d76c7925b_Cfunc_sqlite3_backup_remaining in 000001.o
      __cgo_145d76c7925b_Cfunc_sqlite3_backup_step in 000001.o
      __cgo_145d76c7925b_Cfunc_sqlite3_user_data in 000002.o
      __cgo_145d76c7925b_Cfunc_sqlite3_value_blob in 000002.o
      ...
  "__cgoexp_145d76c7925b_callbackTrampoline", referenced from:
      _callbackTrampoline in 000000.o
  "__cgoexp_145d76c7925b_commitHookTrampoline", referenced from:
      _commitHookTrampoline in 000000.o
  "__cgoexp_145d76c7925b_compareTrampoline", referenced from:
      _compareTrampoline in 000000.o
  "__cgoexp_145d76c7925b_doneTrampoline", referenced from:
      _doneTrampoline in 000000.o
  "__cgoexp_145d76c7925b_rollbackHookTrampoline", referenced from:
      _rollbackHookTrampoline in 000000.o
  "__cgoexp_145d76c7925b_stepTrampoline", referenced from:
      _stepTrampoline in 000000.o
  "__cgoexp_145d76c7925b_updateHookTrampoline", referenced from:
      _updateHookTrampoline in 000000.o
  "_crosscall2", referenced from:
      _callbackTrampoline in 000000.o
      _stepTrampoline in 000000.o
      _doneTrampoline in 000000.o
      _compareTrampoline in 000000.o
      _commitHookTrampoline in 000000.o
      _rollbackHookTrampoline in 000000.o
      _updateHookTrampoline in 000000.o
      ...
  "_main", referenced from:
     implicit entry/start for main executable
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

I have installed the gcc-4.8.1-for-linux64.dmg on my mac.
then I changed this line in sqlite3.go

#cgo CFLAGS: -DSQLITE_ENABLE_RTREE -DSQLITE_THREADSAFE=1 -DHAVE_USLEEP=1
#cgo !linux,!android CFLAGS: -DHAVE_PREAD64=1 -DHAVE_PWRITE64=1

but still error……

@mattn
Copy link
Owner

mattn commented May 17, 2018

Please read README.

@mattn
Copy link
Owner

mattn commented May 17, 2018

You need set CC to cross compiler.

gjrtimmer added a commit to gjrtimmer/go-sqlite3 that referenced this issue May 26, 2018
Compile Section Closes mattn#175
Compile Section Closes mattn#201
Compile Section Closes mattn#206
Compile Section Closes mattn#404
Compile Section Closes mattn#217
Compile Section Closes mattn#224
Compile Section Closes mattn#234
Compile Section Closes mattn#242
Feature table Closes mattn#255
Description Section Closes mattn#232
Golang:1.6 not supported Closes mattn#272
Golang:1.5 not supported + compilation section Closes mattn#283
usleep Implemented Closes mattn#285
FAQ Section Closes mattn#289
Compile Section closes mattn#295
FAQ Section Closes mattn#305
PR339 Closes mattn#318 mattn#321
Compilation Section Closes mattn#341
PR407 Closes mattn#364
Feature `sqlite_vtable` Closes mattn#393
Compile Section Closes mattn#416
sqlite_trace feature Closes mattn#433
Compilation Section Closes mattn#435
Compilation Section Closes mattn#443
Golang:1.6 Not Supported Closes mattn#445
Compilation Section Closes mattn#451
Compilation Section Closes mattn#467
Compilation Section Closes mattn#491
Compilation Section Closes mattn#495
Compilation Section Closes mattn#505
Compilation Section Closes mattn#557
Compilation Section Closes mattn#560
@mattn mattn closed this as completed in c9394b1 May 27, 2018
@mcolburn
Copy link

mcolburn commented Oct 2, 2019

For anyone looking for a solution to compile from Mac to Windows amd64 and include sqlite, if you use Homebrew:

brew install mingw-w64

Then to build:

GOOS="windows" GOARCH="amd64" CGO_ENABLED="1" CC="/usr/local/opt/mingw-w64/bin/x86_64-w64-mingw32-gcc" go build

This worked for me. I used information from: docwhat's post.

@Peakchen
Copy link

Peakchen commented Jan 2, 2020

if use win x86, how can i complier? what to set ?

@gabstv
Copy link

gabstv commented Jan 28, 2020

if use win x86, how can i complier? what to set ?

If you mean from macOS to windows x86 32bit:

export GOOS=windows
export GOARCH=386
export CGO_ENABLED=1
export CC=i686-w64-mingw32-gcc
export OS=windows
export ARCH=386

go build -ldflags "-s -w" -o etc.exe main.go

I'm trying to build from macOS to linux amd64, but without success.

Does anybody have a set of envs/configs (CC, flags) that works?

My gcc is trying to link with the macOS stdlib.h (of Commandlinetools) instead of the non darwin header files, even when I set the CPATH :

export GOOS=linux
export GOARCH=amd64
export CGO_ENABLED=1
export CC=gcc-9
export OS=linux
export ARCH=amd64
export CGO_CPPFLAGS="-I /usr/local/Cellar/gcc/9.1.0/include/c++/9.1.0"

go build -ldflags "-s -w" -o example.bin main.go

Output:

# runtime/cgo
In file included from /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/stdlib.h:61,
                 from /usr/local/Cellar/gcc/9.1.0/include/c++/9.1.0/stdlib.h:30,
                 from _cgo_export.c:3:
/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/Availability.h:257:22: error: missing binary operator before token "("
  257 |     #if __has_builtin(__is_target_arch)
      |                      ^
make: *** [build-linux] Error 2

@vishnukvmd
Copy link
Contributor

If someone else is struggling with cross-compilations issues on OSX, I highly recommend xgo.

Steps:

  • Install xgo (go get github.com/karalabe/xgo).
  • Ensure that your project is within your GOPATH.
  • Run xgo ./path/to/project.
  • Profit.

@anacrolix
Copy link

@vishnukvmd what if you're using Go modules?

@vishnukvmd
Copy link
Contributor

@anacrolix Placing your project inside /Users/{user}/go/src/{project} should help.

@anacrolix
Copy link

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants