Posts Tagged ‘C++’

GCC optimization flags

October 1, 2009

Recently I was talking to a friend of mine who is doing his thesis on parallelizing software on the CELL processor (the one used in the PS3). When benchamrking they found a program that runs nearly four times faster if compiled with the -O3 option. I’ve always specified optimization flags in my projects, and I was aware that they speed up the code a lot, by I was surprised by that figure: four times faster, and without changing the source code!

So, I decided to write a blog post about it. First, let’s start with GCC basics. To compile a C++ file you usually type:

g++ -c file.cpp
g++ -o file file.o

The first line compiles the source code int an object file (file.o), and the second performs the linking step.

To specify an optimization flag, you add -OX to the compiler, were X can be 0,1,2,3 or s; like this:

g++ -O3 -c file.cpp
g++ -o file file.o

Ok, but what optimization flags to choose?

When debugging code, it is necessary to use -O0, which is the equivalent of passing no optimization flags. This is because optimizations confuse debuggers that have difficulties in single stepping your code. (Of course, if you want to debug your code with gdb, you’ll want to enable debugging data, by passing the -g option to the compiler)

When you are releasing your application, it is time to enable optimizations. My advice is to choose the optimization flags in this way:

  • By default, use -O2. It makes your code run fast enough for most applications.
  • If your application need to run very fast, use -O3. This increases the size of the executable file (for example, because it might perform loop unrolling), but it makes it run faster.
  • If you need to minimize the executable size, use -Os plus the -s option that strips from the executable everything except what’s really necessary.

Miosix kernel released

September 23, 2009

After one year of development in the spare time, I released the Miosix kernel under an open source license.

I published the kernel source code, the schematic of the Miosix board, the doxygen documentation, instructions for compiling the kernel, and my thesis.

Read about it here: http://www.webalice.it/fede.tft/miosix/index.html.

First steps with CMake

September 16, 2009

When building something I usually either let an IDE handle the build process, or write makefiles manually. Both solution work, but have drawbacks. IDEs allow you to forget about how code is built, but create some sort of IDE lock-in, since switching to another IDE becomes difficult. Makefiles solve this problem, but are a bit too low level, and writing makefiles for complex projects is tedious.

So it’s a while I’ve been searching a better way. The most known alternative is autoconf and automake. If you’ve compiled some software on Linux you’ll be familiar with the

./configure <options>
make
make install

way of doing things. However, these tools have the main problems of being built mostly with shell scripts. If you plan to support only Linux this is not an issue, but it becomes a bigger trouble if your program needs to be compiled on windows too, where bash is not available by default.

Recently I’ve found CMake, and it looks interesting. First it’s the tool used to build KDE4, and this means it’s well suited even for large projects, second it claims to work equally well on Linux, Mac and Windows. And lastly, its syntax seems simple and high level.

I’ve started with the documentaton on its website, and in 15 minutes I’ve been able to compile an example program. The example program is made of three files, main.cpp, foo.cpp and foo.h.

The first step to create a CMake project is to write a file named CMakeLists.txt. Its content is this:

cmake_minimum_required (VERSION 2.6)
project (HELLO)

add_executable (hello main.cpp foo.cpp)

The first line is a requirement for a new version of CMake. The second specifies the name of the project, and the third tells CMake to create an executable named hello by compiling the files main.cpp and foo.cpp. Really simple.

Like autoconf and automake, CMake doesn’t compile the code directly, instead it generates a makefile. The steps to compile the example program are to open a shell in the directory where the source files and CMakeLists.txt are, and type:

mkdir build
cd build
cmake ../
make

This is because with CMake it is recomended to keep the build directory separated from the source directory. To remove the generated files it is either possible to type “make clean” or simply to delete the build directory.

It is still early to say if I’ll use CMake for my projects, after all I’ve just started using it for 15 minutes, but it surely looks interesting.

Serial ports and C++

September 14, 2009

Update Jun 22, 2011: Fixed a bug for compiling under visual studio 2010.

I’ve added a new article to my website. You can read it here. This article explains how to interface with serial ports from C++. Applications include communicating with an Arduino, the Fonera or any microcontroller. It presents four classes to access serial ports, starting from simple string read/write to advanced asyncronous I/O.

All the code is based on the Boost.asio library, so the code is portable across operating systems.

If you have comments, or find bugs, post them as comment to this blog post.

Measuring time durations

September 14, 2009

Today on the poul‘s mailing list someone asked: “How to measure the time spent in a function?”. I replied with the code that I use for that purpose, but later I thought that it was an interesting question, so I’m posting the solution here on the blog.

The solution is to use the boost date_time library. It is simple, portable (works on Linux, Win and Mac) and precise, since the time is printed in microseconds.

Here is an example code that shows how to profile a function:

#include <iostream>
#include <boost/date_time.hpp>

void f()
{
 for(volatile int i=0;i<100000;i++) ;
}

int main()
{
 using namespace std;
 using namespace boost::posix_time;

 ptime t1(microsec_clock::local_time());
 f();
 ptime t2(microsec_clock::local_time());

 cout<<t2-t1<<endl;
}

Just replace f() with the function you want to profile. Of course you can profile the time spent running your entire program by putting the whole program between the declaration of t1 and t2.

The time is printed in this format:

./test
00:00:00.000620

To be able to compile the program, you need to link with the boost_system library. With the g++ compiler you must add -lboost_system when linking.

Of course you need to have the boost libraries installed. On Linux type:

sudo apt-get install libboost-dev libboost-thread-dev

While on Windows, get this compiler that comes with boost precompiled.