Skip to content

Windows cross compiling not working with libraries that use CGO #40616

Closed
@Nv7-GitHub

Description

@Nv7-GitHub

What version of Go are you using (go version)?

$ go version
go version go1.14.6 darwin/amd64

Does this issue reproduce with the latest release?

I believe I have the latest release.

What operating system and processor architecture are you using (go env)?

go env Output
$ go env
GO111MODULE=""
GOARCH="amd64"
GOBIN=""
GOCACHE="/Users/nishant/Library/Caches/go-build"
GOENV="/Users/nishant/Library/Application Support/go/env"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GOINSECURE=""
GONOPROXY=""
GONOSUMDB=""
GOOS="darwin"
GOPATH="/Users/nishant/go"
GOPRIVATE=""
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/usr/local/go"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/usr/local/go/pkg/tool/darwin_amd64"
GCCGO="gccgo"
AR="ar"
CC="clang"
CXX="clang++"
CGO_ENABLED="1"
GOMOD=""
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/var/folders/h3/pj5y_lss04s9bpst6tzqc5vh0000gp/T/go-build966515058=/tmp/go-build -gno-record-gcc-switches -fno-common"

What did you do?

I have written a simple Pong program using raylib-go. I have a Mac running amd64, and I am trying to cross-compile for windows, amd64.
My program:

Program
package main

import "github.com/gen2brain/raylib-go/raylib"
import . "strconv"

func main() {
WIDTH := int32(800)
HEIGHT := int32(450)
rl.InitWindow(WIDTH, HEIGHT, "Pong")

rl.SetTargetFPS(180)

ballx := int32(WIDTH/2)
bally := int32(HEIGHT/2)

velx := int32(2)
vely := int32(2)

radius := float32(10)

r := int32(radius)

paddlegap := int32(20)
paddlesize := []int32{10, 40}

score := 0

gameover := false

for !rl.WindowShouldClose() {
if !gameover {
	rl.BeginDrawing()

	rl.ClearBackground(rl.Black)

  ballx += velx
  bally += vely

  if ballx > (WIDTH - r - paddlegap) {
    velx *= -1
  } else if ballx < r {
    gameover = true
  }

  if bally > (HEIGHT - r) {
    vely *= -1
  } else if bally < r {
    vely *= -1
  }

  if (ballx < (paddlegap + paddlesize[0])) && (rl.GetMouseY() - paddlesize[1]/2 < bally) && (bally < rl.GetMouseY() + paddlesize[1]/2) {
      velx *= -1
      score += 1
  }

  rl.DrawRectangle(WIDTH - paddlegap, bally - paddlesize[1]/2, paddlesize[0], paddlesize[1], rl.RayWhite)

  rl.DrawRectangle(paddlegap, rl.GetMouseY() - paddlesize[1]/2, paddlesize[0], paddlesize[1], rl.RayWhite)

  rl.DrawCircle(ballx, bally, radius, rl.RayWhite)

  rl.DrawText(Itoa(score), WIDTH/2, paddlegap, 20, rl.RayWhite)

	rl.EndDrawing()
} else {
  rl.BeginDrawing()

  rl.ClearBackground(rl.Black)

  rl.DrawText("Your score was: " + Itoa(score) + ". Game Over.", WIDTH/4, HEIGHT/2, 30, rl.RayWhite)

  rl.EndDrawing()
}
}

rl.CloseWindow()

}

When I run the command GOOS=windows GOARCH=amd64 CGO_ENABLED=1 go build -o pong.exe pong.go, I get the error

# runtime/cgo
gcc_libinit_windows.c:7:10: fatal error: 'windows.h' file not found

When I try to compile without CGO_ENABLED (GOOS=windows GOARCH=amd64 go build -o pong.exe pong.go), I get the error

# github.com/gen2brain/raylib-go/raylib
../../../go/src/github.com/gen2brain/raylib-go/raylib/raylib.go:111:10: undefined: _Ctype_struct_rAudioBuffer
../../../go/src/github.com/gen2brain/raylib-go/raylib/raylib.go:1099:9: undefined: LoadImageEx

What did you expect to see?

I expected to find a file called "pong.exe", which should have been a valid windows executable. I expect that something like raylib should work since it is known for being cross-platform.

What did you see instead?

I got errors in cross-compiling

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions