Code::Blocks
Home ] Up ] Visual C++ V6 ] Visual C++ 2005 ] [ Code::Blocks ] C-Free ]

 

Here's a quick tutorial on how to create a project in the Code::Blocks Interactive Development Environment (IDE).

Creating a New Project

Let's say you're starting out to work on the first assignment for the course.  It makes sense to create a project for this purpose.  So let's see how we can create a project to satisfy our needs.

First, run Code::Blocks on your PC.  After it has shown a "splash screen", you should then see something like the following:

(It will probably be larger;  I've reduced the size of the window to make the images in this web page more reasonable to view.)

Now, click on the File->New... menu item:

Note that this is equivalent to clicking on the leftmost toolbar icon.

This will bring up a dialog that looks as follows:

You must select Console Application.  This is very important:  if you choose any other option, you will probably spend hours trying to figure out lots of irrelevant things about other kinds of applications.  We are not creating Windows-specific applications in this course, nor any other special kinds of applications.

You will see that, when you select Console Application, the Project options and File(s) options will get set for you.  If you do not select the Do not create any files checkbox, Code::Blocks will create a main.cpp file for you.

When you finally click on the Create button, you will be prompted with a file dialog, to specify where all the files for your project will be located.  I recommend that, the first time you do this, you create a directory (folder) -- call it something like CodeBlocks Projects -- and then create a subfolder within that.  Give the subfolder a name that describes the project's purpose -- something like Assignment1.  Once you've chosen an appropriate subfolder, then all the project's files will be stored there.

Note: If you don't choose a separate subfolder for each project, you'll likely run into filename conflicts, such as multiple copies of main.cpp files, etc.

On a college PC, you will need to ensure that the top level directory/folder for all your Code::Blocks projects is somewhere on your P: drive -- something like: 

P:\MyCodeBlocksProjects\

Naturally, once you've set up the top level Code::Blocks projects folder, all you have to do when you create a new project (perhaps for assignment 2, etc.) is to navigate to that top level folder, and create a new subfolder for the new project.

Once you've completed the creation of your project, you should now see something like the following in  your main Code::Blocks window:

where you can see that something new has been introduced, on the left-hand side, under Management.  I've expanded the Sources tree node, and double-clicked on the main.cpp item, to bring up the source code for my main.cpp in the editor pane to the right.  Notice that's the source code was already created for you by Code::Blocks.

Compiling, Linking & Running your Program

Let's go ahead and build this project, to see what it can do for us, so far.  So, click on the Toolbar icon:

The toolbar icon has a tool-tip that indicates 'Build'.  

Clicking on this icon is equivalent to clicking on the Build -> Build menu item.

You should see the following appear in the bottom pane of the IDE:

which indicates that the main.cpp file was compiled and linked successfully.

Now, we can run the program.  To do this, click on the toolbar icon:

The toolbar icon has a tool-tip that indicates 'Run'.  

Clicking on this icon is equivalent to clicking on the Build -> Run menu item.

This causes the following console window to come up:

which shows that the program run successfully, output 'Hello world!', and then prompts the use to press Enter to continue.  It does this, so you can view the results before the window disappears.

Project Settings

Before continuing, I strongly recommend the following:

  • Click on the Project->Properties menu item, which will bring up the following dialog:

  • Now, click on the Project's build options... button, and you should see the following:

  • Click on Produce debugging symbols [-g] in the Compiler Flags tab:

This should tell the compiler to create debugging symbols, so we can use the debugger and ask it about the values of various symbols (such as variable names) by name.

  • Finally, click on the OK button twice to return to the Code::Blocks main window.

Adding Files to Your Project

Adding files to your project is pretty straightforward.  First, click on the toolbar icon:

The toolbar icon has a tool-tip that indicates 'New File'.  

Clicking on this icon is equivalent to clicking on the File -> New File menu item.

which should display a dialog something like the following:

Note that I've entered a file name, second.cpp, and selected a choice in Save as type:.

Now click on the Save button.  This will cause the file to be created in the specified directory/folder, and will cause Code::Blocks to ask:

Click on the Yes button, and you should see something like the following in the main Code::Blocks window:

where you can see that the file has been added to the project, and I have opened the file in the editor pane.

Now, let's add some code.  In the following, you can see that I've added some code to the new file:

and also to the main.cpp file:

So let's build the project again, and see what happens!  Here's the results:

Clearly, we have some problems.  To fix the problems, I did the following:

  • Added a newline at the end of second.cpp
  • Added a new file, second.h, (which I did not add to the project):

     

  • Added the necessary #includes to the main.cpp and second.cpp files:

This time, it builds correctly:

and runs correctly, too:

Note: Although you can (and in most non-trivial cases, should) have multiple C/C++ source files in a project, you can only have a single main entry point.  If you try to have multiple sources, with more than a single main entry point, you will get errors at the link stage.

I have a couple of observations regarding this:

  • You can create a single project for each main entry point you wish to use.  However, this tends to be like cracking a walnut with a sledgehammer, especially for simple programs.
  • You can dynamically add and remove sources from a single project, retaining only a single source with a main entry point.  I find this very useful for simple projects, especially relatively transient ones.

For the assignments in this course, I recommend you place each assignment in its own separate project, and that each such project should be placed in a separate subdirectory/folder.

 
This page was last changed on 16 Jan 2006