Gamasutra is part of the Informa Tech Division of Informa PLC

This site is operated by a business or businesses owned by Informa PLC and all copyright resides with them. Informa PLC's registered office is 5 Howick Place, London SW1P 1WG. Registered in England and Wales. Number 8860726.


Gamasutra: The Art & Business of Making Gamesspacer
View All     RSS
January 22, 2021
arrowPress Releases







If you enjoy reading this site, you might also want to check out these UBM Tech sites:


 

Setting An Executable Icon From The Command Line in Windows

by Mason Remaley on 01/11/21 10:10:00 am

The following blog post, unless otherwise noted, was written by a member of Gamasutra’s community.
The thoughts and opinions expressed are those of the writer and not Gamasutra or its parent company.

 

This morning, I decided it was long overdue that Way of Rhea get its own icon.

I believe that if you’re building a project in Visual Studio there’s a UI through which you can change your exe’s icon—but I’m not a Visual Studio user. It took me quite a while to figure out how to set an exe’s icon from the command line, so I figured I’d document what I learned here in the hopes of saving someone else some time.

It’s possible to dynamically load and set a window icon via code, but for the icon to show up in the file explorer it actually needs to be baked into the executable. This makes sense—explorer.exe shouldn’t have to have to run an executable to determine its icon!

The rest of this post will walk you through how to do this. The only Rust specific bit is the syntax by which I pass arguments to the linker.

Table of contents

Instructions

1. Create an icon

First, you’ll need to create a square image ideally at least 256x256px, and save it as a .ico. If you’re not sure how to create a .ico, you can use GIMP.

2. Create a resources file

Next, you’ll need to create a .rc file that provides the icon path. Here’s what it should look like assuming you aren’t including any other resources:

resources.rc

arbitrary_name_here ICON "path\to\your\icon.ico"

3. Compile the resources file

Next, you’ll need to compile your .rc file. The official way to do this is via rc.exe.

Unfortunately, rc.exe is not in the path by default, so you’ll need to find it. Mine is located at C:\Program Files\ (x86)\Windows Kits\10\bin\10.0.18362.0\x86\rc.exe. It was likely placed there when I installed Visual Studio.

Once you’ve located rc.exe, you can use it to compile your .rc file into a .res file:

rc resources.rc

Programmatically determining the path to rc.exe is, unfortunately, not easy. If you need to do this, here are some options:

 

If you’ve found a better way to do this, or know if it’s possible to use vswhere for this purpose, let me know and I’ll update this post!

Lastly, you need to link with the .res file when building your executable. How exactly you do this depends on your compiler and linker.

If you’ve used a library to handle the resource compilation step for you, it will likely automate this step as well. If not, here’s how I did it in Rust with unrelated options removed for clarity:

cargo rustc -- -C link-args="resources.res"


That’s it! If everything has gone well, your executable should display your icon in the file explorer and the task bar.

If things aren’t working correctly, there are third party tools like Resource Hacker that you can use to compare your final executable’s resources to that of an executable with a working icon.

If you enjoyed this post and want to read more posts like it in the future, you can follow me on Gamasutra, Twitter, join our Discord,or sign up for my newsletter!


Related Jobs

innogames
innogames — Hamburg, Germany
[01.22.21]

Senior Mobile Game Developer (C++) - Forge of Empires
Pixelberry Studios
Pixelberry Studios — Mountain View , California, United States
[01.21.21]

Engineering Manager
Deep Silver Volition
Deep Silver Volition — Champaign, Illinois, United States
[01.21.21]

Senior Technical Designer
Remedy Entertainment
Remedy Entertainment — Espoo, Finland
[01.21.21]

Senior Programmer (Character Tech)





Loading Comments

loader image