11

I have a small C console program and I want to add an .ico file to it, so that the executable looks nice.

How can I do this in CodeBlocks with MinGW/gcc?

Lundin
  • 195,001
  • 40
  • 254
  • 396
user136036
  • 11,228
  • 6
  • 46
  • 46

1 Answers1

19

I could not find relevant help via google that a total beginner (like me for C) could follow, so I will Q&A this topic.

  • First of all you need an .ico file. Put it in the folder with your main.c file.
  • In CodeBlocks go to File -> New -> Empty File and name it icon.rc. It has to be visible in the Workspace/Project otherwise CodeBlocks will not be aware of this file. It will show up there in a project folder called Resources .
  • Put the following line in it: MAINICON ICON "filename.ico". MAINICON is just an identifier, you can choose something different. More info 1 & More info 2.
  • Save the files and compile - CodeBlocks will do everything else for you

What will happen now, is windres.exe (the Resource Compiler) compiling the resource script icon.rc and the icon to an object binary file to obj\Release\icon.res. And the linker will add it to the executable.

It's so easy yet it took me quite a while to find it out - I hope I can save someone else having the same problem some time.

melpomene
  • 84,125
  • 8
  • 85
  • 148
user136036
  • 11,228
  • 6
  • 46
  • 46
  • By "visible in the workspace", I take it you actually mean "added to the project"? – Lundin Mar 08 '18 at 07:43
  • Yes. There will be several project folders visible - "Sources" with the .c files and now "Resources" with the icon.rc file. If you created the iron.rc file with a text editor outside of CodeBlocks, you have to add it to the workspace via `Project -> Add Files`. – user136036 Mar 08 '18 at 11:37
  • My point is that these are project-specific. Any Windows linker will want a .res file, which is the binary generated from the .rc script. This is a Windows standard and not unique to Mingw. – Lundin Mar 08 '18 at 12:24
  • now how you have to use an image inside an .rc file in your project? – SdSaati Mar 05 '20 at 12:56