Utilization of Code::Blocks
Creating a new project
1. Open Code::Blocks.
2. Click on Create a new project.
The following window should appear:
3. Double-click on Console application.
4. If the following page appears, check the check-box and click on the button Next >.
5. Here, we must choose between creating a C or C++ project. Select C++ (It should be selected by default) and click on the button Next >. Note that the following steps would be the same even if we chose C.
6. Enter the name of the project to create inside the first text box (From the top).
7. Choose the path inside your file system where the project will be saved by clicking on the button with the 3 dots (…) at the right of the second text box.
Then, click on the button Next >.
8. Let the next page as is. Click on the button Finish. The selection box allows to choose the compiler to use for the project (Must be installed) and the check boxes allow to decide to create or not a debug/release compilation mode configuration. The text boxes below the check boxes are used to change where the executable and binary object files produced by the compilation process of the project will be saved (Depending on the compilation mode), relatively to the root of the project folder.
9. Your project should now be created and opened.
The files of the project can be seen on the left. By clicking on the plus (+) button before a folder, we can expand it to see the files and folders inside it. By default, a file named main.cpp is created inside the folder Sources and contains a simple program. By expanding the folder Sources we can see the file main.cpp and double-click on it to open it in the code editor at the right of the window.
Compiling and executing the project
The compilation mode can be changed (Between Debug and Release) by using the following selection box:
We can compile the project by clicking on the button with a gear picture.
The previously compiled executable file can be started by clicking on the button with the start symbol:
We can compile and start the project by clicking on the button with a gear and start symbol:
When a file has not changed since the last compilation, it is usually not recompiled. The button following the compile-and-run button is used to recompile all the files of the project.
Adding a new file to the project
Click on File from the menu bar, then go on New and click on Empty file.
2. If the following window appears, click on the button Yes.
3. In the window that appears, choose the location where to save the file (It should be inside the project folder), enter the name of the file to create (With its extension) and click on Save. Here, we create a header file (.hpp). By default, the location is inside the root folder of the project.
4. Click on the button Ok inside the window that appears.
5. A new empty file is now created and added to the project.
In that case, since the file created is a header file, it is added inside the virtual Headers folder. All source files are added in the virtual folder Sources and all header files inside Headers.
Adding an already existing file to the project
1. Right-click on the project name on the left of the window (Where there is the list of the files of the project).
2. Click on Add files….
3. In the window that appears, select the files to add to the project and then click on the button Open.
4. Click on the button Ok in the window that appears.
5. The file(s) are/is now added to the project.
The project folder content
Initially (Before any compilation of the project has been done), the project folder contains only the code files (.cpp, .hpp, .h), the project file (.cbp) and a file that contain the layout of the interface of Code::Blocks for the project (With the extension .layout)(The layout file appears only once the project is saved).
Now, let us compile (Click on the button with a gear symbol) the project in debug mode (By default, it is already in debug mode).
Two new folders has been created (bin and obj).
The bin folder is where the executable file of the project is saved after the compilation process. When we open it, we can see a folder named Debug.
When the project is compiled in debug mode, the executable produced is saved in the folder Debug. If it is compiled in release mode, the folder Release is created and the executable is saved inside it.
By opening the folder Debug, we can see the executable that has been created earlier when we compiled the project.
The folder obj also contains a Debug and Release folder like bin.
The folder obj contains the binary object files (.o). Those files contain the result of the compilation of each source file of the project. A program called linker is then used to assemble them together in order to create the executable file in the folder bin (More on that later).
Executing the executable of the project
Let us click on the button with the start symbol to execute the executable file created earlier by the compilation process.
The following window should appear:
That window is called a console window. It actually is a window that contains a console. A console is an interface from which we can write and read text. The default program inside main.cpp has the effect of writing "Hello world!" inside the console and then it stops. By default, when a console program, which has been started from Code::Block, ends, it waits for the user to press a key before the window of the console closes. However, by default with some IDEs (and when we execute the program directly from the executable), the window of the console automatically closes at the end of the execution of the program (Therefore, we do not have the time to see what is printed into the console).
You should now know enough about Code::Blocks to get started with learning to code in C/C++!