How to change ICON of C++ console application

I wand change this program's Icon.
I have my own *.ico file, now how I can compile that soruce with ICO file?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#include <stdio.h>
#include <windows.h>

int main ()
{

	HANDLE hstdout = GetStdHandle( STD_OUTPUT_HANDLE );
	CONSOLE_SCREEN_BUFFER_INFO csbi;
	GetConsoleScreenBufferInfo( hstdout, &csbi );
	SetConsoleTextAttribute( hstdout, 0x07 );


  char str [80];
  char lopp [3];
  int i;

  printf ("Mis su nimi on?: ");
  scanf ("%s",str);
  printf ("Ilus nimi sul");
  SetConsoleTextAttribute( hstdout, 0x0A );
  printf (" %s \n",str);
  SetConsoleTextAttribute( hstdout, 0x07 );
  printf ("Aga ytle");
  SetConsoleTextAttribute( hstdout, 0x0A );
  printf (" %s ",str);
  SetConsoleTextAttribute( hstdout, 0x07 );
  printf ("kui vana sa oled?");
  scanf ("%d",&i);
  if ( i < 10 or i == 10 ) {
      printf ("Sa oled p2ris noor \n");
  }
  else if ( i > 10 and i < 100 ) {
    printf ("Sa oled p2ris vana \n");
  }
  else {
    printf ("Nyyd sa kyll valetad\n \n");
  }
  printf ("Kas soovid l6petada? [Y]");
  scanf ("%s",lopp);
  return 0;
I know of two ways to do it, but there might be better.

You need to create a resource file, something like the following.

resources.rc:
MAINICON ICON DISCARDABLE "data/icon.ico"

You then need to compile it into object code. I'm using MinGW, you'll have to figure out how to do this with your compiler. If you're using Visual C++ I think there is an option to "add resources".

Using MinGW:
windres resources.rc -o resources.o

Then you just compile your program, including resources.o:
[code]g++ myprogram.cpp resources.o -o myprogram.exe[/coded]

The other option that I know of is to use "Resource Hacker" and go to Action -> Add a new resource.

I hope this is helpful.
Last edited on
then how don't let our program modify by resource hacker ?

any setting in VC can do?
thanks
I get error:
1
2
3
4
5
6
7
8
C:\MinGW\bin>g++ resources.rc -o resources.o
..\lib\gcc\mingw32\3.4.5\..\..\..\..\mingw32\bin\ld.exe:resources.rc: file forma
t not recognized; treating as linker script
..\lib\gcc\mingw32\3.4.5\..\..\..\..\mingw32\bin\ld.exe:resources.rc:1: syntax e
rror
collect2: ld returned 1 exit status

C:\MinGW\bin>
Last edited on
As Dodle said you need to use windres:
windres -i resources.rc -o resources.res
Topic archived. No new replies allowed.