Open source programming for beginners

Sometimes even experienced developers can be a bit overwhelmed by Linux’s extensive development capabilities. Sit back and soak up these tips to become a smarter and more productive Linux developer overnight…

Developing multi-core applications
Multi-core processors are very popular now. Most of the CPUs sold today range from two cores up to eight virtual cores on a desktop-class CPU (even more cores are available on the server-class CPU). Traditionally, developers have been using native threading packages such as POSIX threads (pthread.h).

The problem is that a POSIX thread is not very much optimised for multiple CPU cores and is very low-level. Due to the low-level nature of the POSIX library, you will be need to take care of thread creation, synchronisation and thread ending. An easy way to develop multi-core applications is with Intel Threading Building Blocks (TBB). Intel TBB is an open source C++ template library for developing multi-core optimised applications. It provides a high-level interface on top of native threading libraries. Intel TBB is used by many high-profile products, including Adobe Creative Suite 5. Intel TBB can be downloaded and installed from

The following example demonstrates how to parallelise simple loops in order to find a matching substring.

[sourcecode language=”cpp”]#include <iostream>
#include <string>
#include <algorithm>
#include “tbb/parallel_for.h”
#include “tbb/blocked_range.h”
using namespace tbb;
using namespace std;
static const size_t N = 23;
class SubStringFinder {
const string str;
size_t *max_array;
size_t *pos_array;
void operator() ( const blocked_range<size_t>& r ) const {
for ( size_t i = r.begin(); i != r.end(); ++i ) {
size_t max_size = 0, max_pos = 0;
for (size_t j = 0; j < str.size(); ++j)
if (j != i) {
size_t limit = str.size()-max(i,j);
for (size_t k = 0; k < limit; ++k) {
if (str[i + k] != str[j + k]) break;
if (k > max_size) {
max_size = k;
max_pos = j;
max_array[i] = max_size;
pos_array[i] = max_pos;
SubStringFinder(string &s, size_t *m, size_t *p) :
str(s), max_array(m), pos_array(p) { }
int main() {
string str[N] = { string(“a”), string(“b”) };
for (size_t i = 2; i < N; ++i) str[i] = str[i-1]+str[i-2];
string &to_scan = str[N-1];
size_t *max = new size_t[to_scan.size()];
size_t *pos = new size_t[to_scan.size()];
parallel_for(blocked_range<size_t>(0, to_scan.size() ),
SubStringFinder( to_scan, max, pos ) );
for (size_t i = 0; i < to_scan.size(); ++i)
cout << “ “ << max[i] << “(“ << pos[i] << “)” << endl;
delete[] pos;
delete[] max;
return 0;
CMaking instead of just Making
Traditionally the GNU build system (also known as Autotools) has been used for building open source packages. The problem with Autotools is that a lot of boilerplate code is required to get the whole setup working. Also it is not very portable, since it only runs on UNIX systems. Meet CMake, which has its own scripting that is very easy to use. CMake’s extensive features include an automatic dependency analyser, IDE support for all major IDEs on all major platforms (including KDevelop, Eclipse CDT, Visual Studio, Xcode etc), and parallel builds. CMake itself is not a makefile replacement, rather it generates the platform-dependent makefiles and/or IDE project files for easy building of platform-specific files.

@code: Example CMake file for a KDE application
[sourcecode language=”cpp”]project(mykpartapp)
find_package(KDE4 REQUIRED)
include (KDE4Defaults)
include_directories( ${KDE4_INCLUDES} ${QT_INCLUDES} )
add_subdirectory( doc )
add_subdirectory( src )
add_subdirectory( icons )
Eclipse CDT tips
Eclipse CDT (C/C++ development tooling) provides a fully functional C and C++ integrated development environment based on the Eclipse platform. It is immensely popular on Linux and other open source platforms and has gained a huge traction from the development community.
Uncluttered views: Eclipse workspace is fully customisable. The default layout is cluttered and provides less space and hence hinders the overall usability of the IDE. If you feel the same way, you can customise it to suit your needs, such as minimising everything except for the outline, project explorer and the editor. With a little practice you can find your own personal customisation that suits you. Save your custom view as a perspective using the Window>Save Perspective As command. This will give you quick access to your customised perspective from the Window menu.
Super-accurate indexing and syntax colouring: For performance reasons, Eclipse CDT uses a fast indexing option by default. However, if you have a modern CPU (ie one released after Pentium 4), you can safely enable full indexing and semantic highlighting. These options are already enabled in the recently released CDT Version 7.
Power editing: Use Ctrl+O to pop up an incrementally searchable online view. Use Ctrl+Alt+H to open the ‘Call Hierarchy’ view and search within the current selection.

[twitter username=”linuxusermag”]