This page is a guide to many of the initial (and some intermediate) features of creating and modifying a Code::Blocks project. If this is your first experience with Code::Blocks, here's a good starting point.
Contents
- 1 Project assistant
- 2 Recompose the file
- 2.1 Add an empty file
- 2.2 Add a pre-existing file
- 2.3 Delete the file
- 3 Modify build options
- 3.1 Add a new build target
- 3.2 Virtual Goals
Project assistant
Launch the Project Wizard viaFile->New->Project...to start a new project. There are plenty of pre-configured templates available here for different types of projects, including the ability to create custom templates. To chooseconsole applicationas it is the most common method for general purposes, one clickI.
Note: red text instead of black under any icon means you are using the custom version.wizard script.
Then the console application wizard will appear. Continue browsing the menu by selectingC++when prompted for a language. On the next screen, give your project a name and type or select a destination folder. As you can see below, Code::Blocks will generate the remaining entries of the two.
Finally, the wizard will ask if this project should use the default compiler (usually GCC) and the two default builds:DebugyRelease. All these settings are fine. Press the Finish button and the project will be generated. The main window will turn grey, but that's not a problem, just open the source file. IncludingProjectstabManagementleft pane expand folders and double click source filemain.cppto open it in the editor.
This file contains the following standard code.
main.cpp
#switch onusing namespace std;int main(){ cout << "Hello world!" << end; return 0;}
Recompose the file
A single source file is of little use in programs of any useful complexity. To deal with this, Code::Blocks has some very simple methods for adding additional files to your project.
Add an empty file
In this example, we will split the function
main.cpp
cout << "Hello world!" << end;
in a separate file.
Note: Creating such a small function is generally wrong programming style; it's done here to give a simple example.
To add a new file to your project, open the file template wizard withFile->New->File...oMain toolbar->New file (button)->File...
To chooseuse C/C++and clickI. Navigate through the dialogs in a similar way as when creating the original project by selectingC++when prompted for a language. Several options will be displayed on the last page. The first field will specify the new filename and location (as mentioned, the full path is required). Optionally, you can use...to open a file explorer window and save the file location. VerificationAdd the file to the active projectwill save the file name in the formatFuentesafolderProjectstabManagementplate. When checking any of the build targets, Code::Blocks will notify you that the file should be compiled and linked to the selected targets. This can be useful if, for example, the file contains debug-specific code, as it will allow you to include (or exclude) the appropriate build targets. However, in this example, the greeting function is essential and required for every destination, so check all the boxes and clickfinalizeto generate the file.
The newly created file should open automatically; if not, open it by double clicking its w fileProjectstabManagementplate. Now add the function code.main.cppCalling to.
hi.cpp
#switch onusing namespace std;void hello(){ cout << "Hello world!" <
Add a pre-existing file
Now whenHola()
The function is in a separate file, it must be declaredmain.cppuse it. Launch a plain text editor (for example, Notepad or Gedit) and add the following code.
Hi h
#ifndef HOLA_H_INCLUDED#define HOLA_H_INCLUDEDvoid hola();#endif // HOLA_H_INCLUDED
Save this file as header (Hi h) in the same directory as other source files for this project. Go back to Code::Blocks and clickProject->Add Files...to open the file browser. Here you can select one or several files (using a combination ofControlyChange). (OptionProject->Add files recursively...will search all subdirectories of a given folder and select the appropriate files to place). to chooseHi hand clickopento bring up a dialog asking what build targets the files should belong to. In this example, select both targets.
Note: If the current project has only one build target, this dialog will be skipped.
Back to the main source (main.cpp) includes a header file and replaces the filecutting
function that adapts to new project settings.
main.cpp
#include "hello.h"int main(){ hello(); zwróć 0;}
PressCtrl-F9,Compile->Build, oCompiler toolbar->Compile (button - gear)compile the project. If the following output is generated in the build log (bottom pane), then all steps have been completed correctly.
-------------- Build: Debugging in HelloWorld ---------------
Build: main.cpp
Compilation: hola.cpp
Linking the console executable: bin\Debug\HelloWorld.exe
The output size is 923.25KB
Process terminated with status 0 (0 minutes, 0 seconds)
0 errors, 0 warnings (0 minutes, 0 seconds)
The executable can now be launched by clicking a buttonStartbutton or pressCtrl-F10.
Note: optionF9(to compile and run) combines these commands and may be more useful in some situations.
WerCode::Blocks building processfor what happens behind the scenes during a build.
delete file
Following the steps above, add a new C++ source file,useless.cpp, for project. Removing this unnecessary file from your project is easy. Just right-clickuseless.cppincludingProjectstabManagementpanel and selectDelete the project file.
Note: deleting the file from the project does not do thisNOphysically remove it; Code::Blocks just removes it from project management.
Modify build options
Design goals have come up many times so far. Switch between two generated by default -DebugyRelease- this can be done simply by using the drop down list in the filecompiler toolbar. Each of these targets can be of a different type (e.g. static library, console application), contain a different set of source files, custom variables, different compilation flags (e.g. debug symbols).-side; size optimization-os; link time optimization- voluntary) and various other options.
openProject->Properties...to access the main properties of the active project,Hello World. Most of the settings on the first tab,project settingsthey rarely change.Title:Allows you to rename the project. YesPlatforms:is changed to something other than the default valueAll, Code:Blocks will only allow you to base your project on selected goals. This is useful if, for example, your source code contains a Windows API and therefore won't be valid anywhere other than Windows (or any other OS specific situation). HePlik Makefile:Options are only used when the project should use a makefile instead of the internal Code::Blocks build system (seeCode::Blocks i pliki Makefilefor more details).
Add a new build target
Switch tobuild goalseyelash. ClickTo addto create a new build target and give it a namesmall edition. The highlight in the left column should automatically change to the new target (if not, click on it to change focus). As the default forTyp:- "GUI application" - is invalid forHello Worldprogram, change it to "Console Application" using the drop-down list. Output file nameWitajMundo.exethat's fine except this will build the executable in the parent directory. Add the path "bin\ReleaseSmall\" (Windows) or "bin/ReleaseSmall/" (Linux) in front of you to change the directory (this is relative to the root of the project). HeExecution working directory:It refers to where and when the program will runStartobuild and runare selected. The default "." is fine (refers to the project directory). HeObject output directory:it should be changed to "obj\ReleaseSmall\" (Windows) or "obj/ReleaseSmall/" (Linux) to be consistent with the rest of the project. HeBuild Target Files:You currently have nothing selected. This is a problem because nothing will be compiled if this target is built. Check all boxes.
The next step is to change the lens settings. ClickConstruction options...to access the settings. The first tab that appears contains a series of compiler flags that can be accessed through checkboxes. Select "Remove all symbols from binary" and "Optimize generated code size". The flags here contain many of the most popular options; however, you can pass custom arguments. Switch toother optionssubtab and add the following modifiers.
-fno-rtti
-fno-exceptions
-functional-sections
- trust sections
- voluntary
Now change toLinker configurationeyelash. HeLink Libraries:In this window you can add different libraries (eg.wxmsw28ufor the Windows Unicode versionwxWidgetsmonolithic DLL). This program does not require any such library. The custom switches from the previous step require their link-time counterparts. To add
- voluntary
-os
-wl,--gc-sections
-libgcc-shared
-libstdc++ provided
towardsOther linker options:eyelash. (More details on how these switches work can be found in the GCC documentation atoptimization optionsylinker options.)
virtual targets
ClickOKto accept these changes and return to the previous dialog box. Now that there are two release versions, two separate seriesBuildobuild and runcompile both. Fortunately, Code::Blocks offers the option to combine multiple builds into one. ClickVirtual targets..., SoTo add. Name a virtual goalReleasesand clickOK. to the rightContains build targetschoose bothReleaseylet the little ones out. Close this box and pressOKin the main window.
virtual targetReleaseswill now be available oncompiler toolbar; building this should give the following result.
-------------- Build: Release to HelloWorld ---------------
Build: main.cpp
Compilation: hola.cpp
Linking the console executable: bin\Release\HelloWorld.exe
The output size is 457.50 KB
-------------- Build: Small version on HelloWorld ---------------
Build: main.cpp
Compilation: hola.cpp
Linking the console executable: bin\ReleaseSmall\HelloWorld.exe
The output size is 8.00 KB
Process terminated with status 0 (0 minutes, 1 second)
0 errors, 0 warnings (0 minutes, 1 seconds)