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

How to cross compile from Mac OS to Windows (with answer) #101

Closed
cgimenez opened this issue Dec 25, 2020 · 5 comments
Closed

How to cross compile from Mac OS to Windows (with answer) #101

cgimenez opened this issue Dec 25, 2020 · 5 comments

Comments

@cgimenez
Copy link

cgimenez commented Dec 25, 2020

Just in case, for people who want to cross compile from Mac OS to Windows. It's not restricted to giu, but might be useful to people using it.

At first I thought It will be a nightmare, but it proved to be very easy, without using https://github.com/karalabe/xgo - so without installing Docker.

My config is :

  • Mac OS 10.12.6 (rather old uh?)
  • Mac ports 2.6.4 (I'm a port big fan over brew)

Under terminal :
sudo port install mingw-w64

Then, you would appreciate to run your app under Wine instead of using a full blown VM. So head to https://www.winehq.org/ - Don't bother trying to install Wine under mac ports, it's a trap, will take forever and a bunch of installed packaged will be "upgraded" to universal versions.

You're done.

Under your giu app directory use
GOOS=windows GOARCH=amd64 CGO_ENABLED=1 CC=x86_64-w64-mingw32-gcc CXX=x86_64-w64-mingw32-g++ HOST=x86_64-w64-mingw32 go build -ldflags "-extldflags=-static" -p 4 -v -o bin/app-amd64.exe

First compilation will take some times to proceed, but subsequent builds will be very fast.

Then
wine bin/app-amd64.exe

Fine. Simple. Efficient. Easy. Coding/Debugging iterations are very fast, it's a breeze !

@AllenDang
Copy link
Owner

@cgimenez Works like a charm!
BTW, do you know how to embed icon in to exe?
Currently I still have to open Windows in VM and use ResHack to set icon.

@cgimenez
Copy link
Author

cgimenez commented Jan 1, 2021

I did not tried but there's an explanation here : https://stackoverflow.com/questions/708238/how-do-i-add-an-icon-to-a-mingw-gcc-compiled-executable

Hopefully, the windres utility is available with the mac port's x86_64-w64-mingw32-binutils
You can check its presence by issuing a x86_64-w64-mingw32-windres --version

So, I think you might try to

  • create a .rc file,
  • "compile" it with windres during your build process (or manually, once in a while),
  • then pass the .o file generated by windres to the mingw linker via the -extldflags

@AllenDang
Copy link
Owner

Update here, I got it works!

cat > YourExeName.rc << EOL
id ICON "./res/app_win.ico"
GLFW_ICON ICON "./res/app_win.ico"
EOL

x86_64-w64-mingw32-windres YourExeName.rc -O coff -o YourExeName.syso

GOOS=windows GOARCH=amd64 CGO_ENABLED=1 CC=x86_64-w64-mingw32-gcc CXX=x86_64-w64-mingw32-g++ HOST=x86_64-w64-mingw32 go build -ldflags "-s -w -H=windowsgui -extldflags=-static" -p 4 -v -o YourExeName.exe

rm YourExeName.syso
rm YourExeName.rc

This script could build Window Executable from MacOS and set app icon and window icon properly.

@cgimenez
Copy link
Author

cgimenez commented Jan 4, 2021

Great !! Seems that embedding the .syso file is the go linker's job (I thought would be done by the mingw linker)

Found this article about windows manifests files, might be useful to configure further the behaviour of a windows application at runtime : https://xuri.me/2016/06/15/embedded-icon-in-go-windows-application.html

@lzytaro
Copy link
Contributor

lzytaro commented Mar 23, 2022

在这里更新,我得到它的工作!

cat > YourExeName.rc <<  EOL
 id ICON "./res/app_win.ico" 
GLFW_ICON ICON "./res/app_win.ico" 
EOL
 
x86_64-w64-mingw32-windres YourExeName.rc -O coff -o YourExeName.syso 

GOOS =windows GOARCH=amd64 CGO_ENABLED=1 CC=x86_64-w64-mingw32-gcc CXX=x86_64-w64-mingw32-g++ HOST=x86_64-w64-mingw32 go build -ldflags " -s -w -H=windowsgui -extldflags=-静态" -p 4 -v -o YourExeName.exe 

rm YourExeName.syso 
rm YourExeName.rc

此脚本可以从 MacOS 构建 Window Executable 并正确设置应用程序图标和窗口图标。

I've been able to successfully mutate windows programs on macos, but it doesn't work, no error messages, no windows appear. It works fine on macOS. My windows system is windows server 2019 64 bit system

`#!/bin/zsh

cat > ytfan_gui.rc << EOL
id ICON "./res/app_win.ico"
GLFW_ICON ICON "./res/app_win.ico"
EOL

x86_64-w64-mingw32-windres ytfan_gui.rc -O coff -o ytfan_gui.syso

GOOS=windows GOARCH=amd64 CGO_ENABLED=1 CC=x86_64-w64-mingw32-gcc CXX=x86_64-w64-mingw32-g++ HOST=x86_64-w64-mingw32 go build -ldflags "-s -w -H=windowsgui -extldflags=-static" -p 4 -v -o ytfan_gui.exe

rm ytfan_gui.syso
rm ytfan_gui.rc`

`package main

import "github.com/AllenDang/giu"

func loop() {
giu.SingleWindow().Layout(
giu.Label("test"),
giu.Button("aaaaaaa"),
)
}

func main() {
wnd := giu.NewMasterWindow("芋头返", 400, 200, giu.MasterWindowFlagsNotResizable)
wnd.Run(loop)
}`

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants