Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
Nv7-GitHub opened this issue Aug 6, 2020 · 4 comments
Closed

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

Nv7-GitHub opened this issue Aug 6, 2020 · 4 comments
Labels

Comments

@Nv7-GitHub
Copy link

@Nv7-GitHub Nv7-GitHub commented Aug 6, 2020

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

@Nv7-GitHub Nv7-GitHub changed the title Windows cross compiling not working with raylib-go Windows cross compiling not working with libraries that use CGO Aug 6, 2020
@AlexRouSg
Copy link
Contributor

@AlexRouSg AlexRouSg commented Aug 6, 2020

When cross-compiling, you must specify a C cross-compiler for cgo to use. You can do this by setting the generic CC_FOR_TARGET or the more specific CC_FOR_${GOOS}_${GOARCH} (for example, CC_FOR_linux_arm) environment variable when building the toolchain using make.bash, or you can set the CC environment variable any time you run the go tool.

The CXX_FOR_TARGET, CXX_FOR_${GOOS}_${GOARCH}, and CXX environment variables work in a similar way for C++ code.

https://golang.org/cmd/cgo/#hdr-Using_cgo_with_the_go_command:~:text=When%20cross%2Dcompiling%2C%20you%20must%20specify%20a,time%20you%20run%20the%20go%20tool.

@Nv7-GitHub
Copy link
Author

@Nv7-GitHub Nv7-GitHub commented Aug 6, 2020

Could you please explain how to use these and what I would have to do? Thanks!

@AlexRouSg
Copy link
Contributor

@AlexRouSg AlexRouSg commented Aug 6, 2020

You first have to find and install a C/C++ cross compiler that targets windows. Then set the CC/CXX enviromental variables to point to the correct compiler.

The issue tracker is not used for questions so please see https://github.com/golang/go/wiki/Questions if you need someone to walk you through the process of installing the crosscompiler and setting things up.

@toothrot toothrot added the OS-Windows label Aug 7, 2020
@toothrot
Copy link
Contributor

@toothrot toothrot commented Aug 7, 2020

Closing this issue, and would like to encourage you to follow up on the list of forums suggested by @AlexRouSg.

@toothrot toothrot closed this Aug 7, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
3 participants
You can’t perform that action at this time.