Creating a new project

From CodeBlocks
Revision as of 04:21, 18 August 2011 by Alpha (Talk | contribs) (Adding a new build target)

Jump to: navigation, search

This page is a guide to many of the beginning (and some intermediate) features of the creation and modification of a Code::Blocks project. If this is your first experience with Code::Blocks, here is a good starting point.

The project wizard







  1. #include <iostream>
  3. using namespace std;
  5. int main()
  6. {
  8.     return 0;
  9. }

Changing file composition


Adding a blank file

In this example, we will be splitting the function









  1. #include <iostream>
  3. using namespace std;
  5. void hello()
  6. {
  8. }

Adding a pre-existing file



  1. #ifndef HELLO_H_INCLUDED
  2. #define HELLO_H_INCLUDED
  4. void hello();
  6. #endif // HELLO_H_INCLUDED





  1. #include "hello.h"
  3. int main()
  4. {
  5.     hello();
  6.     return 0;
  7. }

Press Ctrl-F9, Build->Build, or Compiler Toolbar->Build (button - the gear) to compile the project. If the following output is generated in the build log (in the bottom panel) then all steps were followed correctly.

-------------- Build: Debug in HelloWorld ---------------

Compiling: main.cpp
Compiling: hello.cpp
Linking console executable: bin\Debug\HelloWorld.exe
Output size is 923.25 KB
Process terminated with status 0 (0 minutes, 0 seconds)
0 errors, 0 warnings (0 minutes, 0 seconds)

The executable may now be run by either clicking the Run button or hitting Ctrl-F10.

Note: the option F9 (for build and run) combines these commands, and may be more useful in some situations.

See the build process of Code::Blocks for what occurs behind the scenes during a compile.

Removing a file

Using the above steps, add a new C++ source file, useless.cpp, to the project. Removing this unneeded file from the project is straightforward. Simply right-click on useless.cpp in the Projects tab of the Management pane and select Remove file from project.RemoveFile.png

Modifying build options


Build targets have come up several times so far. Changing between the two default generated ones - Debug and Release - can simply be done through the drop-down list on the Compiler Toolbar. Each of these targets has the ability to be a different type (for example: static library; console application), contain a different set of source files, custom variables different build flags (for example: debug symbols -p; size optimization -Os; link time optimization -flto), and several other options.

Open Project->Properties... to access the main properties of the active project, HelloWorld. Most of the settings on the first tab, Project settings, are rarely changed. Title: allows the name of the project to be changed. If Platforms: is changed to something other than its default All, Code:Blocks will only allow the project to build on the selected targets. This is useful if, for example, the source code contains Windows API, and would therefore be invalid anywhere but Windows (or any other operating system specific situations). The Makefile: options are used only if the project should use a makefile instead of Code::Blocks' internal build system (see Code::Blocks and Makefiles for further details).

Adding a new build target


Switch to the Build targets tab. Click Add to create a new build target and name it Release Small. The highlight in the left hand column should automatically switch to the new target (if not, click on it to change the focus). As the default setting for Type: - "GUI application" - is incorrect for the HelloWorld program, change it to "Console application" via the drop-down list. The output filename HelloWorld.exe is fine except in that it will cause the executable to be output in the main directory. Add the path "bin\ReleaseSmall\" (Windows) or "bin/ReleaseSmall/" (Linux) in front of it to change the directory (it is a relative path from the root of the project). The Execution working dir: refers to where the program will be executed when Run or Build and run are selected. The default setting "." is fine (it refers to the project's directory). The Objects output dir: needs to be changed to "obj\ReleaseSmall\" (Windows) or "obj/ReleaseSmall/" (Linux) in order to be consistent with the remainder of the project. The Build target files: currently has nothing selected. This is a problem, as nothing will be compiled if this target is built. Check all the boxes.

The next step is to change the target's settings. Click Build options... to access the settings. The first tab the comes up has a series of compiler flags accessible through check boxes. Select "Strip all symbols from binary" and "Optimize generated code for size". The flags here contain many of the more common options, however, custom arguments may be passed. Switch to the Other options sub-tab and add the following switches.


Now switch to the Linker settings tab. The Link libraries: box provides a spot to add various libraries (for example, wxmsw28u for the Windows Unicode version of the wxWidgets monolithic dll). This program does not require any such libraries. The custom switches from the previous step require their link-time counterparts. Add


to the Other linker options: tab. (For further details on what these switches do, see the GCC documentation on optimization options and linker options.)

Virtual Targets


Click OK to accept these changes and return to the previous dialog. Now that there are two release builds, it will take two separate runs of Build or Build and run to compile both. Fortunately, Code::Blocks provides the option to chain multiple builds together. Click Virtual targets..., then Add. Name the virtual target Releases and click OK. In the right-hand Build targets contained box, select both Release and Release small. Close out of this box and hit OK on the main window. Now if the virtual target Releases will be available from the Compiler Toolbar; building this should result in the following output.

-------------- Build: Release in HelloWorld ---------------

Compiling: main.cpp
Compiling: hello.cpp
Linking console executable: bin\Release\HelloWorld.exe
Output size is 457.50 KB

-------------- Build: Release Small in HelloWorld ---------------

Compiling: main.cpp
Compiling: hello.cpp
Linking console executable: bin\ReleaseSmall\HelloWorld.exe
Output size is 8.00 KB
Process terminated with status 0 (0 minutes, 1 seconds)
0 errors, 0 warnings (0 minutes, 1 seconds)