简体   繁体   English

Go 构建错误“构建约束排除所有 Go 文件”当 GOARCH=386 时导入“C”

[英]Go Build Error "build constraints exclude all Go files" when GOARCH=386 with import "C"

I'm using CGO package to import C code, and I want to build an x86 ( 386 ) windows version of it. I'm using CGO package to import C code, and I want to build an x86 ( 386 ) windows version of it. I found out this should be done by setting GOARCH=386 .我发现这应该通过设置GOARCH=386来完成。 It builds properly on my default environment settings (GOARCH=amd64), however, when I set the environment variable to "386", I get error: build constraints exclude all Go files in my file.它在我的默认环境设置 (GOARCH=amd64) 上正确构建,但是,当我将环境变量设置为“386”时,出现错误:构建约束排除了我文件中的所有 Go文件。

// hello.go
package main

/*
int CFunc() {
}
*/
import "C"

import (
    "fmt"
)

func main() {
    fmt.Println("Hello, Go!")
}
go.mod

module hello

go 1.16

I do:我愿意:

go build

I get:我得到:

C:\Users\basse\source\repos\xhptdc8_babel\go\info\hello>go build
package hello: build constraints exclude all Go files in C:\Users\basse\source\repos\xhptdc8_babel\go\info\hello

Trials:试验:

  • Without import "C", I get no error.没有导入“C”,我没有错误。
  • With // +build windows,386 or // +build windows,386,!cgo , before package main , I still get the same error使用// +build windows,386// +build windows,386,!cgo在 package main 之前,我仍然得到同样的错误

All details are found in Go Issue所有详细信息都可以在Go 问题中找到

Setting环境

set CGO_ENABLED=1  

Generates another type of errors:产生另一种类型的错误:

F:/Work/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/10.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: skipping incompatible F:/Work/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/10.2.0/../../../../x86_64-w64-mingw32/lib/libmingwthrd.a when searching for -lmingwthrd
F:/Work/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/10.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: skipping incompatible F:/Work/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/10.2.0/../../../../x86_64-w64-mingw32/lib\libmingwthrd.a when searching for -lmingwthrd
F:/Work/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/10.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: skipping incompatible F:/Work/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/10.2.0/../../../../x86_64-w64-mingw32/lib/libmingwthrd.a when searching for -lmingwthrd
F:/Work/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/10.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: cannot find -lmingwthrd
.
.
.

Problem solved, thanks to @JimB.感谢@JimB,问题解决了。

In the code, make sure you use import "C" & the correct CGO directives/libraries In go code, like:在代码中,确保在 go 代码中使用import "C"和正确的CGO指令/库,例如:

/*
    #cgo CFLAGS: -Wall -g -I../../../../lib/include/
    #cgo 386   LDFLAGS: -L./ -l:../../../../lib/x86dummy/xhptdc8_driver
    #include "xhptdc8_interface.h"
*/
import "C"

And, when building the code, make sure you run the following commands:并且,在构建代码时,请确保运行以下命令:

set GOARCH=386
set CGO_ENABLED=1
set GOGCCFLAGS=-m32

Detailed steps and lessons learned are mentioned Build Go code that uses CGO and Custom Library详细步骤和经验教训都提到了使用 CGO 和自定义库构建 Go 代码

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM