Thursday, August 5, 2010

C/C++ development basics on Linux

To develop C/C++ applications under Linux (and other OSs as well) there are some basic steps you need to do to write reliable applications.

When compiling your sources, it's very important to set the -Wall option. This will show all the possible warnings and errors during compiling. The errors should be fixed first, otherwise there will be no executable generated.
It's also important to fix all the warnings, normally you can ignore them, but be aware, there can be hidden bugs which come out when running a release version. So it's a good habit to fix all the warnings and errors before continuing. If you don't, you'll end up with too many warnings and because you're so lazy, you'll never fix them.

Another important note is to test your code a few times before adding a new feature. This makes your code stable and less sensitive for crashes. Also it's much easier to work this way, if you don't you'll end up with a lot of debugging.

What's also important is to use valgrind checking your memory usage. If you do a lot of memory allocations, there's a good chance you forget to free an unused allocated memory. This can result in a memory leak.

Another tip is before writing a functionality like a serial port or bluetooth communication, it's better to do this in a seperate mini project, just to focus on that functionality. If it all works well, you can simply import the code to your big project. This is much more efficient than writing it directly in your main project.

No comments:

Post a Comment

NSNotification example