Code::Blocks Project Setup

Code::Blocks is a fascinating IDE for cross-platform development. If you create a project file on Linux then you can (with the right setup) compile with it on Windows, too! CB supports IDE variables/macros, build scripts and wildcard includes so creating your project file the right way can make it last hopefully forever without change.

I strongly recommend creating Code::Blocks along Visual Studio projects for maximum cross-platform open-source!

Requirements

To successfully create a Code::Blocks project we use the following components in this tutorial. There should be no requirement to have a specific platform this time.

To install all required components on a Ubuntu install you can try executing the following bash commands:

sudo apt-get install subversion
sudo apt-get install codeblocks

We want to extend on what we learned in the SVN Project Setup tutorial. Please read it first to get familiar with terms, like library template. Steps 1, 2, 3, 4 and 8 have to be done exactly the same.

Step 1: Setting up the Folder Structure

First create a folder for your project anywhere on your system. Then go into that folder and pick either the binary template or library template to create the required folders inside.

Step 2: Creating the Code::Blocks project

Now open up the Code::Blocks IDE. Click on the File menu then select “New” -> “Project…”. You should see a window that has many project types up for selection.

Code::Blocks Project Creation

If you want to create a library then you have the choice between Static library and Dynamic library, otherwise you should select Console application (mind the template you chose). Click on “Go” when decided.

Next up is a project-type-specific wizard. It is recommended to choose the C++ programming language if asked. Type in a project title first. The select the build folder of your project directory as folder to create the project in. Then look into the “Resulting filename:” input box and remove the directory named after your project title so that the project is directly created into the build folder. Do not mind that the “.cbp” extension is removed from the “Project filename:” field.

Code::Blocks Project Directory Config Example

Click on Next to get to the output directory setup of your project. A nice feature of Code::Blocks is that it remembers what you put into here for future project setups. There should be settings for the Release and Debug build configurations.

  • Debug: “Output dir:” = ../bin/linux/Debug/ and “Objects output dir:” = ../obj/linux/Debug/
  • Release: “Output dir:” = ../bin/linux/Release/ and “Objects output dir:” = ../obj/linux/Release/

Mind that you should pick the lib folder instead in case of a library template. Click on “Finish” next.

But the truly last sub-step is removing junk that was created by Code::Blocks. Inside of the IDE open the “Sources” virtual folder and right-click on the “main.cpp” file. Click on “Remove file from project”. Save the project. Then go into the build folder of your project and remove the file from disk aswell.

Step 3: Adding sources, pushing to Version Control

It is a good idea to include all the source files into our project now. For this I recommend wildcard includes using UnitsGlob entries. After saving your project, open up your .cbp file using your favorite text editor.

.cbp file UnitsGlob Example

As seen above navigate down the file just before the Extensions node. Here you should put the following items:

<UnitsGlob directory="../src" recursive="1" wildcard="*.cpp" />
<UnitsGlob directory="../src" recursive="1" wildcard="*.h" />
<UnitsGlob directory="../src" recursive="1" wildcard="*.hxx" />
<UnitsGlob directory="../include" recursive="1" wildcard="*.h" />

Next up is adding the codebase to Version Control if desired. Note that the .layout and .depend files that Code::Blocks automatically created should not be added (add to ignored files).

Step 4: Linking the Dependencies

This process is similar to linking the dependencies for Visual Studio projects. In the Code::Blocks IDE right click on your project and click on “Build options…”. A new window should have opened up with many beautiful compilation options for you to explore.

Code::Blocks Project build options

On the left side you see the Build Target Listbox. Click on the item on top to put settings that affect every Build Target. Now select the “Linker settings” tab.

Under the “Link libraries:” listbox add the library names of all the dependency libraries. This is usually the project name if not specifically changed.

Then go to the “Search directories” tab. Under the “Compiler” subtab you need to add all include folders for your source code. If you created a Visual Studio project before then should copy the include folders from there. Make sure that all the include paths are relative (they start from the location of the .cbp file).

Under the “Linker” subtab you must add all Library search directories. Make sure that you added all the dependencies into your vendor folder already and that they have Code::Blocks projects aswell. Now this is where IDE macros come into play. For each vendor project of the library template add the following entry:

  • ../vendor/*VENDORNAME*/lib/linux/$(TARGET_NAME)

The $(TARGET_NAME) macro resolves into the name of the currently-building build target. Pretty useful to split Release and Debug object files.

NOTE for Static Library projects: you need to specify .cbp relative file paths to your static libraries instead if you want to include static libraries into static libraries. Look here for further details.

Step 5: Creating a Build Script

For more complicated dependencies you might require dynamic Squirrel build scripts. Those can be added through the “Properties…” right-click project option. You should place them into the build folder.

API reference

Step 6: Creating a Workspace

A workspace is a configuration that bundles multiple projects and defines dependencies between them. Thus it is a good idea to create one.

Code::Blocks Workspace Example

By default you always work inside of a workspace. Simply add all the projects you want bundled-together into Code::Blocks. Then add dependencies for each project:

  • right click on the project
  • click on “Properties…” -> “Project’s dependencies…”
  • check all the dependency CB projects

You can rename your workspace by right-clicking on it and selecting “Rename workspace…”. Once finished select “Save workspace as…” and put it into your build folder. Make sure to commit it into Version Control aswell.

Step 7: Finishing up

That’s usually it. If everything was configured properly your project should build, so try doing that. And if it indeed does, consider committing your codebase to Version Control.

There are plenty examples in my codebases (peframework.cbp, FileSystem.cbp and pefrmdllembed.cbp) so try playing around with them.

GTAForums topic: http://gtaforums.com/topic/904191-codeblocks-is-cool/