Does this work with cross compilation?
Seb-C opened this issue · comments
Hello,
I am cross-building a windows binary from linux. I used go-winres to generate rsrc files, but I didn't find a way to get it included in the executable.
I tried to put the files both in the project root as well as in the same directory than the main, but it didn't work.
Am I missing something?
The documentation mentions that go build automatically embeds in the executable
, but I didn't find any reference or documentation about this behavior or how it's supposed to work. Could you please give some additional references?
This is how I compile the binary for windows:
GOOS=windows GOARCH=amd64 CGO_ENABLED=1 CC=x86_64-w64-mingw32-gcc go build -o ./bin/main.exe ./cmd/main.go
And my json file (it's a bit messy because I tried many things, but still nothing works):
{
"RT_GROUP_ICON": {
"APP": {
"0000": "../assets/icon.png"
},
"IDI_ICON1": {
"0409": "../assets/icon.png"
}
},
"RT_MANIFEST": {
"#1": {
"0409": {
"description": "Project name"
}
}
},
"RT_VERSION": {
"#1": {
"0000": {
"info": {
"0409": {
"ProductName": "Project name"
}
}
}
}
}
}
I found some hints in this discussion about rust: https://users.rust-lang.org/t/when-cross-compiling-from-linux-to-windows-how-do-i-add-an-icon-to-the-exe-file/63117/7
I now installed the package mingw-w64-tools
for debian.
I now have a x86_64-w64-mingw32-windres
in my path along with the x86_64-w64-mingw32-gcc
cross-compiler, but it does not change the result. I am not sure how to use this stuff properly.
In case it is helpful, here is an example of how I'm currently using this tool:
- Add a
go:generate
comment in the source file - Create a config file (could be optional, I forget)
- Run
go generate
with applicableGOOS
andGOARCH
values
After adding the appropriate go:generate
comment to your cmd/main.go
file:
cd /path/to/your/project
GOOS=windows GOARCH=amd64 go generate
GOOS=windows GOARCH=amd64 CGO_ENABLED=1 CC=x86_64-w64-mingw32-gcc go build -o ./bin/main.exe ./cmd/main.go
Forgot to note it, but I generate builds using an Ubuntu environment; confirming that cross compilation does work.
@atc0005 Thank you for the insights. Unfortunately, I don't see any major difference with how you use it. I even tried some of you files/commands, and it did not change anything to my project.
I will continue to investigate tomorrow.
@Seb-C Do you have a public repo you could link to? If not, perhaps create a minimal repro that you could link to and others could assist further.
@atc0005 Thank you for your time. I found the solution.
For the syso
files to be included in the binary, it must be in the same package than another go source file, and this package must be imported by the main.
For some reason, it does not work in my case if I put the syso files directly along with the main.
This is my final setup (a bit non-standard because I want to avoid having random files all over the place):
Makefile:
build-windows:
go-winres make --arch amd64,386 --in ./build/meta/winres.json --out ./build/meta/rsrc
GOOS=windows GOARCH=amd64 CGO_ENABLED=1 CC=x86_64-w64-mingw32-gcc go build -o ./bin/main.exe ./cmd/main.go
I added this to the main:
import _ "***/build/meta"
Then I have build/meta/winres.json
and build/meta/meta.go
(which is an empty file containing only package meta
).