goembedded-resourcecgowebview2

Add icon on a Golang program using webview/webview_go


I've been trying to add an icon to an executable generated by go build. Internally, this executable uses webview/webview_go.

I've used windres to generate the .syso file from the following .rc file

#include <windows.h>
100 ICON "favicon.ico"

(comand used to generate: C:\mingw64\bin\windres.exe -i .\windress-res\app.rc -O coff -o cmd/app.syso)

After, I build the executable

$env:CC = "C:\mingw64\bin\gcc.exe"; 
$env:CXX = "C:\mingw64\bin\g++.exe"; 
go build --ldflags "-linkmode=external -H=windowsgui -s -w"  cmd/app.go

The resulting executable has no icon.

Then, I read that I have to add a flag to --ldflags: -linkmode external, but this generates multiple errors along the lines of github.com/webview/webview_go(.text): relocation target webview_dispatch not defined.

From my understanding, I cannot use the internal go linker since the built exe uses CGO.

How can I embed the icon into the executable then ? I've been struggling to find an answer that works.

I'm open to any solution that will get an icon in my file, as long as it's scriptable.


Solution

  • Aaaand asking the question on stackoverflow is great because I generally figure out the answer on my own not long after. No exception this time.

    By using the -extldflags flag of the go linker, I was able to add the icon using the g++ linker.

    This gives the following command (in --ldflags, which passes the go linker flags from the build command). (Note the change to single quotes for the --ldflags parameter, because we need double quotes inside.)

    $env:CC = "C:\mingw64\bin\gcc.exe"; 
    $env:CXX = "C:\mingw64\bin\g++.exe"; 
    go build --ldflags '-linkmode=external -H=windowsgui -s -w -extldflags "cmd/app.syso"' cmd/app.go