【问题标题】:Go Build Error "build constraints exclude all Go files" when GOARCH=386 with import "C"当 GOARCH=386 导入“C”时,Go Build 错误“构建约束排除所有 Go 文件”
【发布时间】:2021-07-10 12:05:57
【问题描述】:

我正在使用 CGO 包导入 C 代码,我想构建它的 x86 (386) windows 版本。我发现这应该通过设置GOARCH=386 来完成。 它在我的默认环境设置 (GOARCH=amd64) 上正确构建,但是,当我将环境变量设置为“386”时,我收到错误:构建约束排除了我的文件中的所有 Go 文件。 p>

// hello.go
package main

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

import (
    "fmt"
)

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

module hello

go 1.16

我愿意:

go build

我明白了:

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

试验:

  • 没有导入“C”,我不会出错。
  • 使用 // +build windows,386// +build windows,386,!cgo在 package main 之前,我仍然得到同样的错误

所有详情都在Go Issue中找到

设置

set CGO_ENABLED=1  

产生另一种类型的错误:

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
.
.
.

【问题讨论】:

  • 您是否尝试过在那个问题中设置CGO_ENABLED=1
  • 谢谢吉姆。是的,我做到了,但出现了不同类型的错误:code 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 . .
  • 请用相关信息更新问题,我们无法阅读cmets中的长文本或代码。
  • 我已经更新了问题
  • 这些不是 Go 错误,您还需要 32 位库来构建 C 代码。

标签: c go x86 cgo


【解决方案1】:

问题解决了,感谢@JimB。

在代码中,确保使用import "C" 和正确的CGO 指令/库在go 代码中,例如:

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

并且,在构建代码时,请确保运行以下命令:

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

详细步骤和经验教训提到Build Go code that uses CGO and Custom Library

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-10-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多