4

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:

  • Without import "C", I get no error.
  • With // +build windows,386 or // +build windows,386,!cgo, before package main, I still get the same error

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
.
.
.
Bassem Ramzy
  • 326
  • 3
  • 10
  • Did you try setting `CGO_ENABLED=1` like in that issue? – JimB Apr 15 '21 at 13:01
  • Thanks Jim. Yes, I did, but got different types of errors: ```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 . . ``` – Bassem Ramzy Apr 15 '21 at 13:06
  • Please update the question with the relevant information, we cannot read long text or code in the comments. – JimB Apr 15 '21 at 13:09
  • These are not Go errors, you need the 32bit libraries as well to build the C code. – JimB Apr 15 '21 at 13:13
  • So, the solution is to use CGO_ENABLED=1 with fixing c compiler error. What confuses me is why this code builds with CGO_ENABLED=0 on GOARCH=amd64. – Bassem Ramzy Apr 15 '21 at 13:21
  • I'm not sure what you mean, this doesn't compile to anything without `CGO_ENABLED=1`, because the file requires cgo and a C compiler. You must have something else that is compiling, or you are mistaken about the command output. – JimB Apr 15 '21 at 13:25
  • Thanks Jim, I will check that – Bassem Ramzy Apr 15 '21 at 13:29
  • I'm having exactly the same issue. Did you ever find a solution? – Neville Cook Sep 18 '21 at 02:15
  • @Neville Cook, I added the answer to this question – Bassem Ramzy Sep 20 '21 at 16:56

1 Answers1

4

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

Bassem Ramzy
  • 326
  • 3
  • 10