How to add icon to your C++ console application?

Adam Oates picture Adam Oates · Feb 4, 2016 · Viewed 7.4k times · Source

I'm trying to add an icon to my C++ console application but when I try to compile the following code I get these errors. The first is my resources.rc and the second file is my main.cpp.

Resource script:
#ifndef RESOURCE_RC_INCLUDED
#define RESOURCE_RC_INCLUDED

MAINICON  ICON  "icon.ico"

#endif // RESOURCE_RC_INCLUDED`
C++ source:
#include <iostream>
#include <cstdlib>
#include <string>
#include <ctime>
#include <windows.h>
#include <fstream>
#include "resources.rc"

using namespace std;

void SetWindow(int Width, int Height) {
    _COORD coord;
    coord.X = Width;
    coord.Y = Height;

    _SMALL_RECT Rect;
    Rect.Top = 0;
    Rect.Left = 0;
    Rect.Bottom = Height - 1;
    Rect.Right = Width - 1;

    HANDLE Handle = GetStdHandle(STD_OUTPUT_HANDLE);      // Get Handle
    SetConsoleScreenBufferSize(Handle, coord);            // Set Buffer Size
    SetConsoleWindowInfo(Handle, TRUE, &Rect);            // Set Window Size
}


int main() {

    SetWindow(105, 30);
    SetConsoleTitle("Shima Command Line");

    int x;
    char file;
    string name, cmd, selPrg, title;
    string cmds[] = {"(help)", "(information)", "(exit)", "(tell me a joke)", "(open)", "(start)", "(talk dirty to me)", "(what is your favorite color)", "(what is your favourite colour)", "(cls)", "(clear)", "(title)"};
    string jokes[] = {"If your cold you should sit in a corner, it's 90 degrees.", "Knock Knock\nWho's there?\nJustin.\nJustin who?\nJustin time for dinner.", "Why did the chicken cross the road?\nTo get to the other side.", "Jokes aren't really my thing.", "I don't know any good jokes.", "What do you call a cow that just had a baby?\nDe-calf-inated. You know like coffee.", "What do you get from a pampered cow?\nSpoiled milk.", "What do you get if you divide the circumference of a pumpkin by it's diameter?\nPumpkin Pi.", "Did you hear about the two antennas that got married?\nThe ceremony was long and boring, but the reception was great!"};
    string prg[] = {"chrome", "opera", "notepad++", "notepad", "firefox", "ie", "codeblocks", "audacity", "cmd"};
    cout << "What is your name?" << endl;
    getline(cin, name);
    cout << "Hello, " << name << ", my name is Shima Josei. " << endl;

    x = 10;
    do {
        cout << "How may I help you?" << endl;
        getline(cin, cmd);
        if (cmd == cmds[0] || cmd == cmds[1] || cmd == cmds[2] || cmd == cmds[3] || cmd == cmds[4] || cmd == cmds[5] || cmd == cmds[6] || cmd == cmds[7] || cmd == cmds[8] || cmd == cmds[9] || cmd == cmds[10] || cmd == cmds[11]) {
            if (cmd == cmds[0]) {
                cout << "\n\nclear - Shima will clear all text." << endl;
                cout << "cls - Shima will clear the screen." << endl;
                cout << "exit - Shima will exit the program you are currently using." << endl;
                cout << "help - Shima will display the help menu." << endl;
                cout << "information - Shima will display information about her." << endl;
                cout << "open - Shima will open the specified program." << endl;
                cout << "start - Shima will start the specified program." << endl;
                cout << "talk dirty to me - Shima will talk dirty to you." << endl;
                cout << "tell me a joke - Shima will tell you a joke." << endl;
                cout << "title - Shima will change the title." << endl;
                cout << "what is your favorite color - Shima will display her favorite color." << endl;
                cout << "what is your favourite colour - Shima will display her favourite colour in United Kingdom English.\n\n" << endl;
            }

            if (cmd == cmds[1]) {
                cout << "\n\nShima Command Line was developed at\nGigaboy Web Designs by Adam Oates.\nCopyright (C) 2016 Shima Command Line. All rights reserved.\n\n" << endl;
            }

            if (cmd == cmds[2]) {
                exit(0);
            }

            if (cmd == cmds[3]) {
                srand(time(0));

                cout << "\n\n" << jokes[rand() % 9] << "\n\n" << endl;
            }

            if (cmd == cmds[4] || cmd == cmds[5]) {
                cout << "\n\nWhat program would you like to open?" << endl;
                getline(cin, selPrg);
                if (selPrg == prg[0] || selPrg == prg[1] || selPrg == prg[2] || selPrg == prg[3] || selPrg == prg[4] || selPrg == prg[5] || selPrg == prg[6] || selPrg == prg[7] || selPrg == prg[8]) {
                    if (selPrg == prg[0]) {
                        cout << "\n\nOpening Google Chrome..." << endl;
                        ShellExecute(NULL, "open", "C:/Program Files (x86)/Google/Chrome/Application/chrome.exe", NULL, NULL, SW_SHOWDEFAULT);
                        cout << "Successfully opened Google Chrome.\n\n" << endl;
                    }

                    if (selPrg == prg[1]) {
                        cout << "\n\nOpening Opera..." << endl;
                        ShellExecute(NULL, "open", "C:/Program Files/Opera/launcher.exe", NULL, NULL, SW_SHOWDEFAULT);
                        cout << "Successfully opened Opera.\n\n" << endl;
                    }

                    if (selPrg == prg[2]) {
                        cout << "\n\nOpening Notepad++..." << endl;
                        ShellExecute(NULL, "open", "C:/Program Files (x86)/Notepad++/notepad++.exe", NULL, NULL, SW_SHOWDEFAULT);
                        cout << "Successfully opened Notepad++.\n\n" << endl;
                    }

                    if (selPrg == prg[3]) {
                        cout << "\n\nOpening Notepad..." << endl;
                        ShellExecute(NULL, "open", "C:/Windows/notepad.exe", NULL, NULL, SW_SHOWDEFAULT);
                        cout << "Successfully opened Notepad.\n\n" << endl;
                    }

                    if (selPrg == prg[4]) {
                        cout << "\n\nOpening Mozilla Firefox..." << endl;
                        ShellExecute(NULL, "open", "C:/Program Files (x86)/Mozilla Firefox/firefox.exe", NULL, NULL, SW_SHOWDEFAULT);
                        cout << "Successfully opened Mozilla Firefox.\n\n" << endl;
                    }

                    if (selPrg == prg[5]) {
                        cout << "\n\nOpening Internet Explorer..." << endl;
                        ShellExecute(NULL, "open", "C:/Program Files/Internet Explorer/iexplorer.exe", NULL, NULL, SW_SHOWDEFAULT);
                        cout << "Successfully opened Internet Explorer.\n\n" << endl;
                    }

                    if (selPrg == prg[6]) {
                        cout << "\n\nOpening Code::Blocks..." << endl;
                        ShellExecute(NULL, "open", "C:/Program Files (x86)/CodeBlocks/codeblocks.exe", NULL, NULL, SW_SHOWDEFAULT);
                        cout << "Successfully opened Code::Blocks.\n\n" << endl;
                    }

                    if (selPrg == prg[7]) {
                        cout << "\n\nOpening Audacity..." << endl;
                        ShellExecute(NULL, "open", "C:/Program Files (x86)/Audacity/audacity.exe", NULL, NULL, SW_SHOWDEFAULT);
                        cout << "Successfully opened Audacity.\n\n" << endl;
                    }

                    if (selPrg == prg[8]) {
                        cout << "\n\nOpening Command Prompt..." << endl;
                        ShellExecute(NULL, "open", "C:/Windows/System32/cmd.exe", NULL, NULL, SW_SHOWDEFAULT);
                        cout << "Successfully opened Command Prompt.\n\n" << endl;
                    }
                } else {
                    cout << "\n\nI'm sorry, " << name << ", but I don't recognize that program name." << endl;
                }
            }

            if (cmd == cmds[6]) {
                srand(time(0));
                cout << "\n\nDirt, soil earth, rocks, and mud.\n\n" << endl;
            }

            if (cmd == cmds[7]) {
                cout << "\n\nMy favorite color is... well I guess I've never had a favorite color.\n\n" << endl;
            }

            if (cmd == cmds[8]) {
                cout << "\n\nMy favourite colour is... well I guess I have never had a favourite colour.\n\n" << endl;
            }

            if (cmd == cmds[9] || cmd == cmds[10]) {
                system("CLS");
            }

            if (cmd == cmds[11]) {
                cout << "\n\nWhat would you like the title to be?" << endl;
                getline(cin, title);
                SetConsoleTitle(title.c_str());
                cout << "\n\n" << endl;
            }
        } else {
            cout << "\n\nI'm sorry, " << name << ", but I don't understand that command.\n\n" << endl;
        }
    } while (x = 10);

    return 0;

}

These are the errors I get.

||=== Build: Debug in Shima (compiler: GNU GCC Compiler) ===|
C:\Users\Adam\Desktop\C++ Tutorial\Shima\resources.rc|4|error: 'MAINICON' does not name a type|
C:\Users\Adam\Desktop\C++ Tutorial\Shima\main.cpp||In function 'int main()':|
C:\Users\Adam\Desktop\C++ Tutorial\Shima\main.cpp|35|error: 'string' was not declared in this scope|
C:\Users\Adam\Desktop\C++ Tutorial\Shima\main.cpp|35|note: suggested alternative:|
c:\program files (x86)\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.7.1\include\c++\bits\stringfwd.h|65|note:   'std::string'|
C:\Users\Adam\Desktop\C++ Tutorial\Shima\main.cpp|35|error: expected ';' before 'name'|
C:\Users\Adam\Desktop\C++ Tutorial\Shima\main.cpp|36|error: expected ';' before 'cmds'|
C:\Users\Adam\Desktop\C++ Tutorial\Shima\main.cpp|37|error: expected ';' before 'jokes'|
C:\Users\Adam\Desktop\C++ Tutorial\Shima\main.cpp|38|error: expected ';' before 'prg'|
C:\Users\Adam\Desktop\C++ Tutorial\Shima\main.cpp|39|error: 'cout' was not declared in this scope|
C:\Users\Adam\Desktop\C++ Tutorial\Shima\main.cpp|39|note: suggested alternative:|
c:\program files (x86)\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.7.1\include\c++\iostream|62|note:   'std::cout'|
C:\Users\Adam\Desktop\C++ Tutorial\Shima\main.cpp|39|error: 'endl' was not declared in this scope|
C:\Users\Adam\Desktop\C++ Tutorial\Shima\main.cpp|39|note: suggested alternative:|
c:\program files (x86)\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.7.1\include\c++\ostream|562|note:   'std::endl'|
C:\Users\Adam\Desktop\C++ Tutorial\Shima\main.cpp|40|error: 'cin' was not declared in this scope|
C:\Users\Adam\Desktop\C++ Tutorial\Shima\main.cpp|40|note: suggested alternative:|
c:\program files (x86)\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.7.1\include\c++\iostream|61|note:   'std::cin'|
C:\Users\Adam\Desktop\C++ Tutorial\Shima\main.cpp|40|error: 'name' was not declared in this scope|
C:\Users\Adam\Desktop\C++ Tutorial\Shima\main.cpp|40|error: 'getline' was not declared in this scope|
C:\Users\Adam\Desktop\C++ Tutorial\Shima\main.cpp|40|note: suggested alternative:|
c:\program files (x86)\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.7.1\include\c++\bits\basic_string.h|2792|note:   'std::getline'|
C:\Users\Adam\Desktop\C++ Tutorial\Shima\main.cpp|46|error: 'cmd' was not declared in this scope|
C:\Users\Adam\Desktop\C++ Tutorial\Shima\main.cpp|47|error: 'cmds' was not declared in this scope|
C:\Users\Adam\Desktop\C++ Tutorial\Shima\main.cpp|74|error: 'jokes' was not declared in this scope|
C:\Users\Adam\Desktop\C++ Tutorial\Shima\main.cpp|79|error: 'selPrg' was not declared in this scope|
C:\Users\Adam\Desktop\C++ Tutorial\Shima\main.cpp|80|error: 'prg' was not declared in this scope|
C:\Users\Adam\Desktop\C++ Tutorial\Shima\main.cpp|158|error: 'title' was not declared in this scope|
C:\Users\Adam\Desktop\C++ Tutorial\Shima\main.cpp|165|warning: suggest parentheses around assignment used as truth value [-Wparentheses]|
C:\Users\Adam\Desktop\C++ Tutorial\Shima\main.cpp|34|warning: unused variable 'file' [-Wunused-variable]|
||=== Build failed: 17 error(s), 2 warning(s) (0 minute(s), 1 second(s)) ===|

Answer

Sergio picture Sergio · Mar 30, 2016

In the source file you must include the header file resource.h and not the resource script resource.rc. Then recompile the project.

Header file:

    #define MAINICON      101    

Source C++ file:

#include <iostream>
#include ...
#include "resource.h"

using namespace std;