This short tutorial is intended to lead the reader through the basics of writing, compiling, linking and running simple console C++ programs using Dev-C++.
Configuration of the linker
Although standard settings should be completely sufficient in our sample, it is generally a good idea to ask the linker to include debugging information into the programs we are planning to compile, so as to easien the debugging phase. Here are the steps to recreate in order to alter this setting:
- After having started Dev-C++, open the "Tools" menu, and select "Compiler Options". This will bring up the "Compiler Option" Dialog allowing you to set the way the compiler behaves in most of cases.
- Pick the tab Settings, to access detailed setup of the compiler, and pick "Linker" category from the left hand side list.
- On the right hand side, click on the end of the line "Generate debugging information" to bring up the drop down list allowing you to select the option's value. Select "Yes" to ask the linker to include debugging information (it will include such things as procedure names, source code references, ... into the generated executables), and finally click "OK" to validate your choice. This should bring you back to the main Dev-C++ screen.
Dev-C++ needs a project folder in order to store most of the data related to the compilation and linking (object files, executables, resources, ...). Therefore, the first thing you will want to do is create a project, even if your source code is extremely simple. Here is how you will create your project:
- After having started Dev-C++, Open the "File" menu, and select "New >" - "Project" to bring up the "New Project" setup window, which is used by Dev-C++ as a Wizards dedicated to the creation of new projects.
- In the "Basic" tab (that should be the one selected by default), select "Console Application" since that is the type of project this tutorial is focused on. You will be able to experiment with the other types of projects later, but this is out of the scope of this tutorial.
- Give a meaningful name to your project, then click "OK" to proceed to the Project Location Selection.
- Select the location where you would like the project file (*.dev) to be stored, and validate the file dialog to process. Dev-C++ will create a new source code file containing some starting code that will be possibly expanded to form the final executable.
The project having been created, we will now start adding files to it. Dev-C++ gives the possibility to add existing files to a project, but we will in our tutorial restrict ourselves to the creation of a new empty source code file. The one provided automatically by the New Project Wizard will be an excellent starting point.
- Open the "File" menu, then select "Save All". This will save all resources that have been modified since the last save. In our case, only the newly created file has been modified, and since it is a new file, Dev-C++ will bring up a "Save File" dialog to let you specify where you would like this new file to be saved.
- After having Selected the location and specified a meaningful name to the file, you might click OK to close the dialog and proceed to the modification of the initial file.
- Since the default source code does not do much more than ask the user to press a key before terminating, we will start having it display some text. We will need the string class from the C++ Standard Template Library to build and store the string to be displayed. Add the following line to the included headers: "#include<string>".
- We will then need to declare a variable supposed to hold the string having to be displayed, and add the appropriate instruction to display this string on the console. Add the following at the beginning of "main" :
string s = "text";
cout << s << endl << endl;
- You can of course save the modified file again, but this is done automatically when you start the compilation of the project, which is why we will skip this step.
The following instructions work for most of Dev-C++, except rare exceptions. The "Execute" menu is the place where you will find everything related to Compiling, Building and Running the program generated by your source code. Here are the steps to go through to achieve this:
- Open "Execute" menu, then select Compile (Or use the key combination Control + F9). Please note that this will save all the modifications to project files before proceeding. This step is done automatically before running the resulting executable (see next step for running the executable), if it had not been compiled already. Dev-C++ will bring up a progress dialog showing you the different states (mostly compilation and linking) of the compilation process.
- Once finished, click "Close" to terminate the compilation process.
- If Dev-C++ did not report any compilation error, that means the program has been compiled successfully, and you can now start it from within Dev-C++. To do so, open "Execute" menu, and select "Run" (or use the key combination Control + F10). Please note that this will compile the program before proceeding, if it had not been compiled already. From this point on, Dev-C++ will launch a command-line shell, and start running the program into it. You can set additional parameters (Command line parameters for example) for the program, using Project Settings.
- After having ran the program, the command system("PAUSE") included into the source file before main() return will pause the execution until you press a key, so that you get the possibility to view eventual results before the execution window closes itself.
As a short summary for this section, simply note that it is usually enough to press Control + F10 to have Dev-C++ sequentially save the project, try to compile it (it will alert on errors so that you can correct them before proceeding), try to build it (it will alert on errors), and finally run it.
Finally, you can close project files using the "File" menu, and selecting the option "Close Project".