Resource File on Windows?

1,114 views
Skip to first unread message

Jeremy Cowgar

unread,
May 14, 2011, 1:27:36 AM5/14/11
to golang-nuts
How can I link a resource file into my Windows GO project? I wish to
have the resource include the manifest necessary to use modern (>=XP)
widget set instead of the Windows 95 blocky widget set.

Thanks,

Jeremy

Evan Shaw

unread,
May 14, 2011, 1:41:00 AM5/14/11
to Jeremy Cowgar, golang-nuts

There's no way yet. See this issue:
http://code.google.com/p/go/issues/detail?id=1552

- Evan

brainman

unread,
May 14, 2011, 1:59:16 AM5/14/11
to golan...@googlegroups.com
I'm not certain, you are after something like that:

If yes, it is possible to change linker (8l) to include resource pe section. The problem I have with that idea is that the structure of that section will be quite complicated to describe with optional parameters. Also this is windows specific, and I hate to add complexity to such simple program for 0 benefit on any other platform.

I think it would be better to write a separate program that does the job. You could easily add extra PE section to a pre-built executable with whatever contents you like. $GOROOT/src/pkg/debug/pe directory contains some basic code for pe file access. Wei wrote the code, so I don't know much about it, but it is "go", thus it is just matter of doing it. You could make the program that creates your resource section to have what you want, for a particular app. Or you could make it general enough, so it reads some "definition file" and builds resource section out of it.

I was thinking about doing it myself, but it is low priority for me at this moment. And it gets lower as other problems creep up.

(For structure see "PE File Resources" in http://msdn.microsoft.com/en-us/magazine/ms809762.aspx)

Alex

Paulo Pinto

unread,
May 14, 2011, 2:50:55 AM5/14/11
to golang-nuts
Actually the standard way any Windows developer is used to, is to make
use of the linker.

In C, C++, Delphi and .Net, it is the linker responsibility to take
the output of the resource compiler,
message compiler, just to cite two of the resource tools, and embed
the result in the executable
during the linking phase.

This is not Windows specific. Aix also uses resources, and it is also
the linker's job to put them
into the final binary.

AllenDang

unread,
May 14, 2011, 9:06:13 AM5/14/11
to golang-nuts
If you just want to enable system theme, simply put a
"exename.exe.manifest" file with your go file and compile, it works.
You can refer related MSDN article to see the content of manifest
file.

Jeremy Cowgar

unread,
May 14, 2011, 10:44:16 AM5/14/11
to golang-nuts
I was not aware of that, thank you. This does fix my immediate need
but an additional need, as is probably many others, is embedding of an
application icon. That is one major use of resource files being linked
into the executable.

For anyone else trying this, I did run into a problem making it work.
I created my .manifest file and it had zero effect. After pulling my
hair out for a while I read a blog post that stated Windows caches the
.manifest file and the time it looked for a manifest file. The first
time I ran my .exe file it contained no manifest file and Windows
remembered that. It was ignoring my file. I recompiled my application
and viola, the manifest file worked. Windows saw that it was a new
executable since the last time it looked for a manifest so it searched
for and found my exename.exe.manifest file.

2011/5/14 AllenDang <alle...@gmail.com>:

Wei guangjing

unread,
May 15, 2011, 3:09:33 AM5/15/11
to Jeremy Cowgar, golang-nuts
I just make a patch to emit resource for 8l. <http://codereview.appspot.com/4516055/>

8l can link Windows pe obj, so can use windres to compile resoure file to obj file and use 8l link it into exe. example for simple hello.go:

#cat resource.rc
0 ICON "my.ico"

#windres -o resource.o resource.rc
#8g hello.go
#gopack grc _go_.8 hello.8 resource.o
#8l -o hello.exe _go_.8

should see the icon on hello.exe.

Jeremy Cowgar

unread,
May 15, 2011, 9:21:41 AM5/15/11
to Wei guangjing, golang-nuts
I'd love to use your patch however once applied I can no longer compile Go via:

cd src && ./all.bash

some time into the compile process I begin getting GP faults on 8l :-(
Reverting the change the above command compiles Go w/no issue. The
linker does build fine, but later use of it seems to be causing the
issue. Does a complete rebuild of Go work for you with the new linker?

Jeremy

2011/5/15 Wei guangjing <vcc...@gmail.com>:

Wei guangjing

unread,
May 15, 2011, 9:57:22 AM5/15/11
to Jeremy Cowgar, golang-nuts
sorry, I forget check if no .rsrc. I uploaded new patch, please try again.

Jeremy Cowgar

unread,
May 15, 2011, 5:04:41 PM5/15/11
to Wei guangjing, golang-nuts
Great! I had applied your patch. go now rebuilds from scratch with out
a problem and following your instructions I have embedded into the
resulting executable file an Icon and Manfiest file via a standard .RC
file. My application has a windows icon and the modern widgets (not
blocky 95 style). The resulting executable requires no manifest file.

Is the going to become part of core? It doesn't seem like that complex
of an addition and essential if Go wishes to be a first class citizen
on Windows.

AllenDang

unread,
May 25, 2011, 7:26:56 AM5/25/11
to golang-nuts
Any idea about when this patch will be applied to Go release?

On May 15, 9:57 pm, Wei guangjing <vcc....@gmail.com> wrote:
> sorry, I forget check if no .rsrc. I uploaded new patch, please try again.
>
> 在 2011年5月15日 下午9:21,Jeremy Cowgar <jcow...@gmail.com>写道:
>
>
>
>
>
>
>
> > I'd love to use your patch however once applied I can no longer compile Go
> > via:
>
> > cd src && ./all.bash
>
> > some time into the compile process I begin getting GP faults on 8l :-(
> > Reverting the change the above command compiles Go w/no issue. The
> > linker does build fine, but later use of it seems to be causing the
> > issue. Does a complete rebuild of Go work for you with the new linker?
>
> > Jeremy
>
> > 2011/5/15 Wei guangjing <vcc....@gmail.com>:
> > > I just make a patch to emit resource for 8l.
> > > <http://codereview.appspot.com/4516055/>
>
> > > 8l can link Windows pe obj, so can use windres to compile resoure file to
> > > obj file and use 8l link it into exe. example for simple hello.go:
>
> > > #cat resource.rc
> > > 0 ICON "my.ico"
>
> > > #windres -o resource.o resource.rc
> > > #8g hello.go
> > > #gopack grc _go_.8 hello.8 resource.o
> > > #8l -o hello.exe _go_.8
>
> > > should see the icon on hello.exe.
>
> > > 在 2011年5月14日 下午10:44,Jeremy Cowgar <jcow...@gmail.com>写道:
>
> > >> I was not aware of that, thank you. This does fix my immediate need
> > >> but an additional need, as is probably many others, is embedding of an
> > >> application icon. That is one major use of resource files being linked
> > >> into the executable.
>
> > >> For anyone else trying this, I did run into a problem making it work.
> > >> I created my .manifest file and it had zero effect. After pulling my
> > >> hair out for a while I read a blog post that stated Windows caches the
> > >> .manifest file and the time it looked for a manifest file. The first
> > >> time I ran my .exe file it contained no manifest file and Windows
> > >> remembered that. It was ignoring my file. I recompiled my application
> > >> and viola, the manifest file worked. Windows saw that it was a new
> > >> executable since the last time it looked for a manifest so it searched
> > >> for and found my exename.exe.manifest file.
>
> > >> 2011/5/14 AllenDang <allen...@gmail.com>:

Russ Cox

unread,
May 25, 2011, 7:56:24 AM5/25/11
to AllenDang, golang-nuts
On Wed, May 25, 2011 at 07:26, AllenDang <alle...@gmail.com> wrote:
> Any idea about when this patch will be applied to Go release?

It's there now. hg pull; hg update default

mattn

unread,
May 25, 2011, 9:13:53 AM5/25/11
to golan...@googlegroups.com, AllenDang
I want to add resource file to 8l like below.

-----------------
# 8l  -o demo.exe _go_.8 resource.o
usage: 8l [-options] [-E entry] [-H head] [-I interpreter] [-L dir] [-T text] [-R rnd] [-r path] [-o out] main.8
mingw32-make: *** [demo.exe] Error 1
-----------------

And Makefile need to become...

-----------------
include $(GOROOT)/src/Make.inc

TARG=foo
GOFILES=\
foo.go\

ifeq ($(GOOS),windows)
OFILES:=resource.o
endif

include $(GOROOT)/src/Make.cmd

resource.o : resource.rc
windres -o resource.o resource.rc
-----------------

Thanks.
- Yasuhiro Matsumoto

AllenDang

unread,
May 25, 2011, 10:14:40 PM5/25/11
to golang-nuts
Hi, Jeremy

Can you give me a hint about how to enable modern widgets with no
manifest file required?

On May 16, 5:04 am, Jeremy Cowgar <jcow...@gmail.com> wrote:
> Great! I had applied your patch. go now rebuilds from scratch with out
> a problem and following your instructions I have embedded into the
> resulting executable file an Icon and Manfiest file via a standard .RC
> file. My application has awindowsicon and the modern widgets (not
> blocky 95 style). The resulting executable requires no manifest file.
>
> Is the going to become part of core? It doesn't seem like that complex
> of an addition and essential if Go wishes to be a first class citizen
> onWindows.
>
> Jeremy
>
> 2011/5/15 Wei guangjing <vcc....@gmail.com>:
>
>
>
>
>
>
>
> > sorry, I forget check if no .rsrc. I uploaded new patch, please try again.
>
> > 在 2011年5月15日 下午9:21,Jeremy Cowgar <jcow...@gmail.com>写道:
>
> >> I'd love to use your patch however once applied I can no longer compile Go
> >> via:
>
> >> cd src && ./all.bash
>
> >> some time into the compile process I begin getting GP faults on 8l :-(
> >> Reverting the change the above command compiles Go w/no issue. The
> >> linker does build fine, but later use of it seems to be causing the
> >> issue. Does a complete rebuild of Go work for you with the new linker?
>
> >> Jeremy
>
> >> 2011/5/15 Wei guangjing <vcc....@gmail.com>:
> >> > I just make a patch to emitresourcefor 8l.
> >> > <http://codereview.appspot.com/4516055/>
>
> >> > 8l can linkWindowspe obj, so can use windres to compile resoure file
> >> > to
> >> > obj file and use 8l link it into exe. example for simple hello.go:
>
> >> > #catresource.rc
> >> > 0 ICON "my.ico"
>
> >> > #windres -oresource.oresource.rc
> >> > #8g hello.go
> >> > #gopack grc _go_.8 hello.8resource.o
> >> > #8l -o hello.exe _go_.8
>
> >> > should see the icon on hello.exe.
>
> >> > 在 2011年5月14日 下午10:44,Jeremy Cowgar <jcow...@gmail.com>写道:
>
> >> >> I was not aware of that, thank you. This does fix my immediate need
> >> >> but an additional need, as is probably many others, is embedding of an
> >> >> application icon. That is one major use ofresourcefiles being linked
> >> >> into the executable.
>
> >> >> For anyone else trying this, I did run into a problem making it work.
> >> >> I created my .manifest file and it had zero effect. After pulling my
> >> >> hair out for a while I read a blog post that statedWindowscaches the
> >> >> .manifest file and the time it looked for a manifest file. The first
> >> >> time I ran my .exe file it contained no manifest file andWindows
> >> >> remembered that. It was ignoring my file. I recompiled my application
> >> >> and viola, the manifest file worked.Windowssaw that it was a new
> >> >> executable since the last time it looked for a manifest so it searched
> >> >> for and found my exename.exe.manifest file.
>
> >> >> 2011/5/14 AllenDang <allen...@gmail.com>:
> >> >> > If you just want to enable system theme, simply put a
> >> >> > "exename.exe.manifest" file with your go file and compile, it works.
> >> >> > You can refer related MSDN article to see the content of manifest
> >> >> > file.
>
> >> >> > On 5月14日, 下午1时27分, Jeremy Cowgar <jcow...@gmail.com> wrote:
> >> >> >> How can I link aresourcefile into myWindowsGO project? I wish to
> >> >> >> have theresourceinclude the manifest necessary to use modern
> >> >> >> (>=XP)
> >> >> >> widget set instead of theWindows95 blocky widget set.
>
> >> >> >> Thanks,
>
> >> >> >> Jeremy

Jeremy Cowgar

unread,
May 26, 2011, 4:39:38 AM5/26/11
to AllenDang, golang-nuts
You must have applied the patch discussed in this thread. Then:

1. Create a RC file
2. Create a Manifest file
3. Compile your RC file
4. Link your compiled RC file with your application

For 1 & 2, please see https://github.com/jcowgar/go-iup ... Look in
the demos directory for demo.rc and demo.manifest.

For 3 & 4:

$ windres -o demo-rc.o demo.rc
$ 8g demo.go
$ gopack grc _go_.8 demo.8 demo-rc.o
$ 8l -o demo.exe _go_.8

Jeremy

2011/5/25 AllenDang <alle...@gmail.com>:

Jeremy Cowgar

unread,
May 26, 2011, 2:16:05 PM5/26/11
to golang-nuts
The only way I know is how I described. The manifest file has to be
compiled into a resource and that resource then has to be linked into
your executable. The .rc file includes the .manifest file. windres
then compiles the resource file and gopack then packs your resource
object and your code objects together. Then 8l does the final linking.

I don't think there is a way to take a manifest file and directly
place it into the executable, but I could be wrong. Is there a problem
with compiling it to a resource and linking it in?

As for the sample manifest file, it is located on github.com, please
see https://github.com/jcowgar/go-iup and look under demos for
demo.manifest. Also see the related demo.rc file.

Jeremy Cowgar

On Thu, May 26, 2011 at 10:47 AM, Dang Allen <alle...@gmail.com> wrote:
> Hi, Jeremy
>
> Thanks for your reply! But what I want to know is "how to embed the
> manifest file into exe?". I think I have to add a line like "123
> RCDATA c:\...\my.exe.manifest" into RC file, and compile.
>
> But I cannot do this right after explorer related articles in MSDN.
> Can you send me your manifest and RC file for reference?

thaddaeus...@gmail.com

unread,
Sep 6, 2013, 4:26:54 PM9/6/13
to golan...@googlegroups.com, jco...@gmail.com
I've been recently working with Allen Dang's gform library.  I was trying to get the dialog method of creating a window to work and was running into a few problems. 

First, I needed to include the resource file with the .exe
Second, I couldn't follow the instructions in this thread exactly as my Go installation did not provide an 8g binary.
Third, when attempting to compile using go tool 8g, it was unable to locate the local import of github.com/AllenDang/gform

I was finally able to get everything to work correctly by doing the following:

Copying the github.com directory from %GOPATH%\pkg\windows_386 into C:\Go\pkg\windows_386.
After that I could compile and link the resource as follows:
%GOPATH%\src\cmd\test>windres -o test-rc.o test.rc
%GOPATH%\src\cmd\test>go tool 8g test.go
%GOPATH%\src\cmd\test>go tool pack grc _go_.8 test.8 test-rc.o
%GOPATH%\src\cmd\test>go tool 8l -o test.exe _go_.8

Finally, I had a test.exe file that could launch successfully.

It seems that go tool 8g is only looking for imports within the GOROOT  and not in GOPATH.  That seems to be a severe flaw.  I would definitely recommend the windows version of the Go binary get updated to allow for go tool 8g to search for imports within GOPATH.

Thanks for this thread as it helped solve all the problems I was having with this issue.

Thaddaeus

Ken Allen

unread,
Sep 7, 2013, 12:01:40 AM9/7/13
to golan...@googlegroups.com, jco...@gmail.com, thaddaeus...@gmail.com
Just name the output of windres with .syso and use go build.

%GOPATH%\src\cmd\test>windres -o test-rc.syso test.rc
%GOPATH%\src\cmd\test>go build

easy peasey

Thaddaeus Fillmore

unread,
Sep 9, 2013, 10:37:27 AM9/9/13
to Ken Allen, golan...@googlegroups.com, jco...@gmail.com
Ken,

Thanks!  That worked great.  I was able to build the resource file and compile from within goclipse!  Is it just me or is this convention not included in any of the Go documentation?  I had googled extensively and was unable to locate that simple fix.

Thanks again,

Thaddaeus

Ken Allen

unread,
Sep 9, 2013, 10:50:48 AM9/9/13
to golan...@googlegroups.com, jco...@gmail.com
I found out about it somewhere on this list but I think you are correct in that it is not documented anywhere. I'll try to confirm that sometime today and file an issue to fix that since this is such a useful feature for us poor windows developers.


On Saturday, May 14, 2011 1:27:36 AM UTC-4, Jeremy Cowgar wrote:
Reply all
Reply to author
Forward
0 new messages