12 Replies - 42564 Views - Last Post: 30 May 2011 - 07:22 PM Rate Topic: -----

#1 Ty Meador   User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 166
  • Joined: 23-May 11

Add icon to .exe w/Code::Blocks

Posted 29 May 2011 - 12:17 PM

I have a simple program which displays the Minimum and Maximum for all c++ Datatypes. but i would like the .exe file to have its own icon. how would i go about doing this? P.S. i want the icon to be applied when the program is compiled, so please do not tell me to right click on the .exe file and click "change icon".I am using Code::Blocks, not microsoft visual studio
/*Header Files
==============*/
#include <iostream> 	//Required for i/o
#include <iomanip>  	//Required for i/o Manipulators
#include <limits>       //Required to  fetch the min/max of datatypes
#include <string>       //Required for Strings
#include <cstdlib>      //Required for System Commands
#include <cmath>	   	//Required for Math Library Functions
#include <cctype>		/*Required for Character
						Analization Library Functions*/
using namespace std;
/*
Programmer: 	Ty Meador
Name:
Purpose:
*/
/*Function Prototypes
=====================*/


/*Globals
===============*/
//double PI = 3.1415926535897

int main()
{
    cout<<"Data Type\t\t\tMinimum\t\t\tMaximum\n";
    for(int i = 0; i<80; i++)
    {
        cout<<"=";
    }
    cout<<"Bool*\t\t\t\t"<<numeric_limits<bool>::min()<<"\t\t\t"<<numeric_limits<bool>::max();
    cout<<"\nUnsigned Short Int\t\t"<<numeric_limits<unsigned short int>::min()<<"\t\t\t"<<numeric_limits<unsigned short int>::max();
    cout<<"\nUnsigned Int\t\t\t"<<numeric_limits<unsigned int>::min()<<"\t\t\t"<<numeric_limits<unsigned int>::max();
    cout<<"\nInt\t\t\t\t"<<numeric_limits<int>::min()<<"\t\t"<<numeric_limits<int>::max();
    cout<<"\nUnsigned Long Int\t\t"<<numeric_limits<unsigned long int>::min()<<"\t\t\t"<<numeric_limits<unsigned long int>::max();
    cout<<"\nLong Int\t\t\t"<<numeric_limits<long int>::min()<<"\t\t"<<numeric_limits<long int>::max();
    cout<<"\nFloat\t\t\t\t"<<numeric_limits<float>::min()<<"\t\t"<<numeric_limits<float>::max();
    cout<<"\nDouble\t\t\t\t"<<numeric_limits<double>::min()<<"\t\t"<<numeric_limits<double>::max();
    cout<<"\nLong Double\t\t\t"<<numeric_limits<long double>::min()<<"\t\t"<<numeric_limits<long double>::max();
    cout<<"\n*The Bool datatype MUST be 1 or 0\n\nPress Enter/Return to Exit\n";
    cin.get();
    return 0;
}




Is This A Good Question/Topic? 0
  • +

Replies To: Add icon to .exe w/Code::Blocks

#2 snoopy11   User is offline

  • Engineering ● Software
  • member icon

Reputation: 1467
  • View blog
  • Posts: 4,726
  • Joined: 20-March 10

Re: Add icon to .exe w/Code::Blocks

Posted 30 May 2011 - 12:12 AM

First you have to create a resource script
name it something like resource.rc from the new file menu.

It should end up looking something like this

#ifndef RESOURCE_RC_INCLUDED
#define RESOURCE_RC_INCLUDED

MAINICON  ICON  "icon1.ico"

#endif // RESOURCE_RC_INCLUDED



then go to the directory where the prog's main.cpp is located in a command prompt window
and type the following.
>windres resource.rc Hello.o

note my resource file is called resource.rc and i have asked the windres compiler to make an object file called Hello

then in the command prompt type
>g++ main.cpp Hello.o

this tells the compiler to build the program with the object file Hello.o
or you can just use Build|Build from the menu.
Was This Post Helpful? 2
  • +
  • -

#3 Ty Meador   User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 166
  • Joined: 23-May 11

Re: Add icon to .exe w/Code::Blocks

Posted 30 May 2011 - 07:17 AM

okay, i got the program to include the following resource file
#ifndef RESOURCE_RC_INCLUDED
#define RESOURCE_RC_INCLUDED

MAIN ICON "icon1.ico"

#endif




But it throws more than 50 errrors saying that things are not defined, the first of which saying that main does not have a type, so i declare it as int

#ifndef RESOURCE_RC_INCLUDED
#define RESOURCE_RC_INCLUDED

int MAIN ICON "icon1.ico"

#endif

and it throws one error "expected initializer before 'ICON'
Was This Post Helpful? 0
  • +
  • -

#4 kaa   User is offline

  • D.I.C Addict

Reputation: 225
  • View blog
  • Posts: 805
  • Joined: 15-April 11

Re: Add icon to .exe w/Code::Blocks

Posted 30 May 2011 - 07:24 AM

I really don't know anything about this but I noticed that snoopy11's post said MAINICON (no space) and your's says MAIN ICON. Maybe that's your problem.
Was This Post Helpful? 0
  • +
  • -

#5 Ty Meador   User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 166
  • Joined: 23-May 11

Re: Add icon to .exe w/Code::Blocks

Posted 30 May 2011 - 07:28 AM

#ifndef RESOURCE_RC_INCLUDED
#define RESOURCE_RC_INCLUDED

MAINICON ICON "icon1.ico"

#endif


The error it now throws is "'MAINICON' does not name a type"

This post has been edited by Ty Meador: 30 May 2011 - 07:41 AM

Was This Post Helpful? 0
  • +
  • -

#6 kaa   User is offline

  • D.I.C Addict

Reputation: 225
  • View blog
  • Posts: 805
  • Joined: 15-April 11

Re: Add icon to .exe w/Code::Blocks

Posted 30 May 2011 - 08:43 AM

Hah. Forget about that windres stuff. I just tried this with a "helloworld" program in Codeblocks. Just add a resource.rc file to your Codeblocks project: File/New/Empty file and click "yes" when it asks about adding it to the project. And put the .ico file in the project folder along with the .cpp file and the resource.rc file.

resource.rc:
#ifndef _resource_rc
#define _resource_rc

MAINICON ICON "NAMEOFICON.ICO"

#endif


and after you build the project the icon will show up next to the exe file in Windows Explorer.
Was This Post Helpful? 0
  • +
  • -

#7 snoopy11   User is offline

  • Engineering ● Software
  • member icon

Reputation: 1467
  • View blog
  • Posts: 4,726
  • Joined: 20-March 10

Re: Add icon to .exe w/Code::Blocks

Posted 30 May 2011 - 09:24 AM

Yes you could do it that way too.

But i thought i would teach you about how the resource compiler works
and a little about the main compiler.

But whatever you find easier.
Was This Post Helpful? 1
  • +
  • -

#8 kaa   User is offline

  • D.I.C Addict

Reputation: 225
  • View blog
  • Posts: 805
  • Joined: 15-April 11

Re: Add icon to .exe w/Code::Blocks

Posted 30 May 2011 - 11:30 AM

View Postsnoopy11, on 30 May 2011 - 04:24 PM, said:

Yes you could do it that way too.

But i thought i would teach you about how the resource compiler works
and a little about the main compiler.

But whatever you find easier.

OK, thanks. I just tried your way & that works too (with my hello world and with Ty Meador's program too). :)

Don't know why he's having that problem.
Was This Post Helpful? 0
  • +
  • -

#9 Ty Meador   User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 166
  • Joined: 23-May 11

Re: Add icon to .exe w/Code::Blocks

Posted 30 May 2011 - 03:51 PM

View Postkaa, on 30 May 2011 - 11:30 AM, said:

View Postsnoopy11, on 30 May 2011 - 04:24 PM, said:

Yes you could do it that way too.

But i thought i would teach you about how the resource compiler works
and a little about the main compiler.

But whatever you find easier.

OK, thanks. I just tried your way & that works too (with my hello world and with Ty Meador's program too). :)

Don't know why he's having that problem.

Here is my program
/*Header Files
==============*/
#include <iostream> 	//Required for i/o
#include <iomanip>  	//Required for i/o Manipulators
#include <limits>       //Required to  fetch the min/max of datatypes
#include <string>       //Required for Strings
#include <cstdlib>      //Required for System Commands
#include <cmath>	   	//Required for Math Library Functions
#include <cctype>		/*Required for Character
						Analization Library Functions*/
#include "resource.rc"  //Icon Resource File
using namespace std;
/*
Programmer: 	Ty Meador
Name:
Purpose:
*/
/*Function Prototypes
=====================*/


/*Globals
===============*/
//double PI = 3.1415926535897

int main()
{
    cout<<"\nData Type\t\t\tMinimum\t\t\tMaximum\n";
    cout<<"\nInteger:\n";
    for(int i = 0; i<80; i++)
    {
        cout<<"=";
    }
    cout<<"Bool*\t\t\t\t"<<numeric_limits<bool>::min()<<"(False)\t\t"<<numeric_limits<bool>::max()<<"(True)";
    cout<<"\nUnsigned Short Int\t\t"<<numeric_limits<unsigned short int>::min()<<"\t\t\t"<<numeric_limits<unsigned short int>::max();
    cout<<"\nUnsigned Int\t\t\t"<<numeric_limits<unsigned int>::min()<<"\t\t\t"<<numeric_limits<unsigned int>::max();
    cout<<"\nInt\t\t\t\t"<<numeric_limits<int>::min()<<"\t\t"<<numeric_limits<int>::max();
    cout<<"\nUnsigned Long Int\t\t"<<numeric_limits<unsigned long int>::min()<<"\t\t\t"<<numeric_limits<unsigned long int>::max();
    cout<<"\nLong Int\t\t\t"<<numeric_limits<long int>::min()<<"\t\t"<<numeric_limits<long int>::max()<<endl;
       for(int i = 0; i<80; i++)
    {
        cout<<"=";
    }
    cout<<"\nFloat:\n";
       for(int i = 0; i<80; i++)
    {
        cout<<"=";
    }
    cout<<"\nFloat\t\t\t\t"<<numeric_limits<float>::min()<<"\t\t"<<numeric_limits<float>::max();
    cout<<"\nDouble\t\t\t\t"<<numeric_limits<double>::min()<<"\t\t"<<numeric_limits<double>::max();
    cout<<"\nLong Double\t\t\t"<<numeric_limits<long double>::min()<<"\t\t"<<numeric_limits<long double>::max()<<endl;
       for(int i = 0; i<80; i++)
    {
        cout<<"=";
    }
    cout<<"*The Bool datatype MUST be 1 or 0\n\nPress Enter/Return to Exit\n";
    cin.get();
    cin.sync();
    return 0;
}




Here is my resource file

#ifndef _resource_rc
#define _resource_rc

MAINICON ICON "icon1.ico"

#endif


But when i try to compile, it throws more than 50 errors. what have i done wrong here?
Was This Post Helpful? 0
  • +
  • -

#10 Ty Meador   User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 166
  • Joined: 23-May 11

Re: Add icon to .exe w/Code::Blocks

Posted 30 May 2011 - 04:24 PM

Okay, i (kind of) got it to compile, i get this error now...


-------------- Build: Debug in DataTypes ---------------

WARNING: Can't read file's timestamp: K:\Programs\DataTypes\icon.rc
Linking console executable: bin\Debug\DataTypes.exe
mingw32-g++.exe: obj\Debug\icon.res: No such file or directory
Process terminated with status 1 (0 minutes, 0 seconds)
0 errors, 0 warnings
Was This Post Helpful? 0
  • +
  • -

#11 #define   User is offline

  • Duke of Err
  • member icon

Reputation: 1853
  • View blog
  • Posts: 6,671
  • Joined: 19-February 09

Re: Add icon to .exe w/Code::Blocks

Posted 30 May 2011 - 04:44 PM

Did you add the resource file to the project?
Did you use build to compile all of your project?

Re: How to change icon of C++ console app
Was This Post Helpful? 0
  • +
  • -

#12 NickDMax   User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2255
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

Re: Add icon to .exe w/Code::Blocks

Posted 30 May 2011 - 04:51 PM

Keep in mind that the resource file IS NOT a C/C++ program and is meaningless to your C/C++ compiler. The compiler will say that all of this stuff: MAINICON, ICON, are undefined.

It is the resource compiler (windres) that compiles this file and creates an object file. The object file can be read by the linker when creating the final PE file.


So line #11 of your project is not right it is? You are trying to #include a resrouce script (which may LOOK like a header file but IS NOT) into your program. But the resource file is not C or C++ it is a resource script (an entirely different language).

Remove line 11 and see how things go.
Was This Post Helpful? 0
  • +
  • -

#13 Ty Meador   User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 166
  • Joined: 23-May 11

Re: Add icon to .exe w/Code::Blocks

Posted 30 May 2011 - 07:22 PM

View PostNickDMax, on 30 May 2011 - 04:51 PM, said:

Keep in mind that the resource file IS NOT a C/C++ program and is meaningless to your C/C++ compiler. The compiler will say that all of this stuff: MAINICON, ICON, are undefined.

It is the resource compiler (windres) that compiles this file and creates an object file. The object file can be read by the linker when creating the final PE file.


So line #11 of your project is not right it is? You are trying to #include a resrouce script (which may LOOK like a header file but IS NOT) into your program. But the resource file is not C or C++ it is a resource script (an entirely different language).

Remove line 11 and see how things go.

This worked, thank you. I included the .rc file in the projects file, as well as the icon, and VUALLLAA! all done! thanks everyone!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1