go交叉编译出错(Mac编译Linux)

Mac编译Linux命令:

CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build

执行之后发现报错:

# github.com/mattn/go-sqlite3
../../../../mattn/go-sqlite3/sqlite3_go18.go:18:10: undefined: SQLiteConn

然后我去网上找答案,很多人都说,需要把CGO_ENABLED的值设为1。当我改为1之后,又出现新的错误:

# os/user
/usr/local/go/src/os/user/getgrouplist_unix.go:15:35: warning: passing 'gid_t *' (aka 'unsigned int *') to parameter of type 'int *' converts between pointers to integer types with different sign [-Wpointer-sign]
/usr/include/unistd.h:653:43: note: passing argument to parameter here


# github.com/oushu/oushu-assist/lava/main
/usr/local/go/pkg/tool/darwin_amd64/link: running clang failed: exit status 1
ld: warning: ignoring file /var/folders/7z/dryhn78d0lx320rv7s3fpnjm0000gn/T/go-link-742574817/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/7z/dryhn78d0lx320rv7s3fpnjm0000gn/T/go-link-742574817/go.o
Undefined symbols for architecture x86_64:
  "__cgo_topofstack", referenced from:
      __cgo_f7895c2c5a3a_C2func_getnameinfo in 000002.o
      __cgo_f7895c2c5a3a_Cfunc_getnameinfo in 000002.o
      __cgo_f7895c2c5a3a_C2func_getaddrinfo in 000004.o
      __cgo_f7895c2c5a3a_Cfunc_gai_strerror in 000004.o
      __cgo_f7895c2c5a3a_Cfunc_getaddrinfo in 000004.o
      __cgo_b8ad650f185f_Cfunc_sqlite3_backup_finish in 000006.o
      __cgo_b8ad650f185f_Cfunc_sqlite3_backup_init in 000006.o
      ...
  "__cgoexp_b8ad650f185f_callbackTrampoline", referenced from:
      _callbackTrampoline in 000005.o
  "__cgoexp_b8ad650f185f_commitHookTrampoline", referenced from:
      _commitHookTrampoline in 000005.o
  "__cgoexp_b8ad650f185f_compareTrampoline", referenced from:
      _compareTrampoline in 000005.o
  "__cgoexp_b8ad650f185f_doneTrampoline", referenced from:
      _doneTrampoline in 000005.o
  "__cgoexp_b8ad650f185f_rollbackHookTrampoline", referenced from:
      _rollbackHookTrampoline in 000005.o
  "__cgoexp_b8ad650f185f_stepTrampoline", referenced from:
      _stepTrampoline in 000005.o
  "__cgoexp_b8ad650f185f_updateHookTrampoline", referenced from:
      _updateHookTrampoline in 000005.o
  "_crosscall2", referenced from:
      _callbackTrampoline in 000005.o
      _stepTrampoline in 000005.o
      _doneTrampoline in 000005.o
      _compareTrampoline in 000005.o
      _commitHookTrampoline in 000005.o
      _rollbackHookTrampoline in 000005.o
      _updateHookTrampoline in 000005.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)

其实错误原因就是对于split来说你这个默认的gcc去编译是有问题的。
执行go env 查看 到CC=“clang”,这里我们需要重新下载一个gcc去编译:

  1. 下载gcc的包,连接:gcc下载。然后解压。
  2. 设置下载该gcc的全局变量;
  3. 执行交叉编译命令:CGO_ENABLED=1 CC=x86_64-pc-linux-gcc GOOS=linux GOARCH=amd64 go build

这样对于报错的交叉编译问题就解决啦。还是用了一些时间,才找到了解决 办法。

发布了67 篇原创文章 · 获赞 68 · 访问量 4万+

猜你喜欢

转载自blog.csdn.net/ITqingliang/article/details/103999318