Official website for Linux User & Developer
FOLLOW US ON:
Nov
15

Open source programming for beginners

by Kunal Deo

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 www.threadingbuildingblocks.org/.

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

@code:TBBExample.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;
public:
 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

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 follow us
Pages: 1 2 3 4 5 6
  • Tell a Friend
  • Follow our Twitter to find out about all the latest Linux news, reviews, previews, interviews, features and a whole more.
    • grep

      Personally I’d hesitate telling a beginning programmer to use the system() function call. But if I mentioned it, I’d at least explain that it could have grave system security implications.

      For instance, the command that the system() function call executes may run at the same privileges as the program containing the system() function call. This could be bad news if your program somehow gets “root” privileges (via the Set-User-ID or Set-Group-ID settings, or otherwise).

      Next, the program containing the system() function call may not execute the command that you think it will. For instance, using your example ( system(“ls -l”) ), if a cracker can somehow get you to execute your program with a bogus executable file called “ls” in the current directory, then what’s actually executed will be what the cracker intended, but not what you expected. Probably not the best example, but you get the idea. Using a full path name in the parameter for the system() function call may help mitigate this problem, at the possible expense of cross compatibility, but not necessarily eliminate it.

      Admittedly, there’s a time and place to use any of the hundreds of Linux function calls, but a good programmer will (or should) be aware of the implications of those that they chose.

    • Pingback: LXer: Open Source programming for beginners - oBlurb

    • Ken Jennings

      SQL in a C program is a LOT more simple when using a database that provides an embedded SQL compiler. (PostgreSQL for one.)
      Not the end-all-be-all of perfect examples, but here’s a sample:
      http://www.kenjennings.cc/st/stprgux.html
      http://www.kenjennings.cc/st/prg/ux/test_pgc.tar.gz

    • Pingback: Linux User & Developer issue 93 is out now! | Linux User

    • Pingback: Introduction to Linux / Newbies Guide

    • Pingback: Linux coding tips for newbies « 0ddn1x: tricks with *nix

    • Pingback: Nikon D3100 Dslr Reviews

    • Morgan

      A new book covering the basics: Programming From Scratch by Gary Crandall available here http://www.perfscipress.com/programming-from-scratch/

    • http://www.howtouseachainsaw.com/item-poulan-pro-50cc-gas-22-in-chain-saw-with-case-pp5020av-new_280924888864_US_0.html buy chainsaw

      It really is really a good plus practical little bit of info. I’m fulfilled you provided this handy facts along with us. Remember to keep us current this way. Appreciate your spreading.