0

I am using Code::Blocks and I would like to compile and link the icon for my program with the rest of the program. The icon file is a PNG image called icon.png and is in the Others folder of the project folder in the workspace. I right clicked on it and checked "Compile file" and "Link file" in the Options menu. In case this is a bit unclear, I've made a screenshot which shows exactly what I've done:

screenshot

Anyway, the problem is that the compiler gives me an error message that says "No such file or directory". If I check "Compile file" but not "Link file", the compiling works fine but the image isn't compiled.

I would like to know how to compile an image file without getting an error message.

Donald Duck
  • 8,409
  • 22
  • 75
  • 99
  • 1
    Image files should not be compiled... a C/C++ compiler will only compile C and C++. Not PNG (or any other image) – Toby Jul 15 '16 at 16:14
  • @Toby Do you mean that it's impossible or that it's not recommended? (if it's not recommended I won't do it, I just want to know if it's possible) – Donald Duck Jul 15 '16 at 16:58
  • I had the same problem - How to add an executable icon to a C program in CodeBlocks - and I answered it here: https://stackoverflow.com/q/49164595/2441026 – user136036 Mar 08 '18 at 02:53

1 Answers1

4

Let's clear up the misunderstanding first: :)

Compilation is the act of turning your source code into object files containing machine code. Linking is resolving the dependencies between these object files and outputting a usable executable/library.

So, it doesn't make sense to compile or link an image.


There are of course ways to get an image into your program:

  • Distribute your program as an archive and use file IO
  • Convert the image into a byte array and place that in a C file. The xxd -i command does just that
  • Turn it into an object file and link it. See answers to this question

The thing is, what you want is telling the file manager, that there is an icon and where it is. The file manager won't know about your byte array or extra section.

On Windows, PE executables can contain the predefined .rsrc section that among others can specify the icons to use. You write a resource file, which is then compiled by a special compiler into an object file you can link against. Here's an answer about doing that using MinGW.

Community
  • 1
  • 1
a3f
  • 8,517
  • 1
  • 41
  • 46