Reputation: 406
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
.
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.
// 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:
All details are found in Go Issue
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
.
.
.
Upvotes: 5
Views: 16408
Reputation: 406
Problem solved, thanks to @JimB.
In the code, make sure you use import "C"
& the correct CGO
directives/libraries In go code, like:
/*
#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
Upvotes: 5