Tag Archives: IDE

How to add header and source files in Code::Block

Problem

An exercise (from Accelerated C++ Chapter 4) that I have come across recently requires me to partition the program. i.e. instead of writing the entire program in one main.cpp file, I need to split to over multiple C++ source files (with extension .cpp) and header files (with extension .h).

Solution

To add C++ source (.cpp) or header (.h) files, do the followings in Code::Block.

  1. File –> New –> File
  2. Select C/C++ source (or C/C++ header)
  3. Click Go
  4. Click “next”
  5. Select C++, then “next”
  6. Set the file path of the C++ source file accordingly (e.g. same location as the main.cpp file). Name the file to xxx.cpp (or xxx.h)
  7. To make the source file compilable, select “Debug” and “Release” under the Add file option. (If forgotten this step, see the “How to set build target” section below)
  8. Now the source file should appear under Workspace –> ProjectName –> Sources
  9. To compile the file, simply right-click the filename, and click “Build file”.

How to set build target

Assuming we have forgotten to perform step 7 above (or for whatever other reasons), we can always set that target again to enable smooth file build.

  1. Right-click the project name
  2. Properties
  3. Build targets tab
  4. Under the Build target files section in bottom right, check the (currently not checked) files, click OK.
  5. Next time when you right-click the file and build file, Code::Block should build the file accordingly.

Accelerated C++ Solution to Exercise 0-8

Exercise 0-8

Is this a valid program? why or why not?

#include <iostream>
int main{}
{
    // This is a comment that extends over several lines
    // by using // at the beginning of each line instead of using /*
    // or */ / to delimit comments.
       std::cout << "Does this work?" << std::endl;
       return 0;
}

Solution

Yes. This is a valid C++ Program.

Line comment starts the comment by beginning the line with the //. Whatever follows is a comment, as long as within the same line. The

Once a new line is created, the // is required at the beginning to start another comment.

You can also use the line comment in the middle of a line (so the first part of the line is not a comment, and the second part is a comment). It looks something like this:

std::cout << "Hello World!" << std::endl;  // this is a comment

What I sometimes do is to try out writing the code in an IDE like Code::Block. The auto syntax highlighting within the IDE should give us hints whether the parts in the code are comment or not, indicated by the different colour highlighting. (I would not rely 100% on this though. But it is a good and quick guide I’ve found).

Reference

Koenig, Andrew & Moo, Barbara E., Accelerated C++, Addison-Wesley, 2000

Add new Code::Block Editor Theme: Son of Obsidian

The Code::Block IDE by default only comes with one editor scheme called “default”, which is a standard white background with code syntax highlighting. I wasn’t a fan of the default theme and wondered whether there might be a way to import a readily built alternative theme without having to go through the trouble to manually create one myself.

After doing some “Google-ing” I came across this very good Code::Block Forum which led me to Pizaro’s instruction on Exporting and Importing Code::Blocks Themes and the Son of Obsidian theme config file. So I had a go using both the instruction and the theme config file.

The end result? It worked! Now in my Code::Block I have a new Son of Obsidian Theme which is much more user friendly (and cool) to work with. It has a dark background with sharper syntax highlighting.

If you are looking to do the similar I highly recommend you to check out these articles.