Official website for Linux User & Developer

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…

This article originally appeared in issue 93 of Linux User & Developer magazine.Open source programming for beginners Subscribe and save more than 30% and receive our exclusive money back guarantee – click here to find out more.

This article is designed to look at a large range of tips and techniques – here’s a breakdown of what this article touches on and where:

Page 2
Running commands from C
Creating and applying patches
Conditional compilation

Page 3
Developing for the Text User Interface
Building multimedia applications

Page 4
Using an embedded SQL database

Page 5
Developing multicore applications
CMaking instead of just Making
Eclipse CDT tips

Page 6
KDevelop 4 Playground plug-ins
Best practices for writing cross platform applications

Deciding your path
There are multiple ways by which you can program in Linux. Linux supports most of the modern types of programming environment available today. Based on your interest and objective, you can choose from the following programming methods…

Shell programming: A shell is the userland portion of Linux which provides the command-line interface for UNIX-like operating systems, including Linux. In addition to providing a command-line interface, shells are also capable of executing scripts called shell scripts. These scripts can be used for a wide variety of tasks, such as system configuration, file manipulation and troubleshooting. Shell scripting supports basic programming constructs such as maths, looping variables etc, but what makes it different from a traditional programming language is its ease of use and availability. Incidentally, shell programming is required for any Linux certification program, or for that matter any UNIX certification program.

Interpreted languages: A significant portion on Linux is made up of interpreted languages such as Perl, Python and Ruby. The same can also be used by you to program Linux. These languages are often dynamic and are less restrictive than the traditional programming languages, while providing the same level of functionality. Hence they became popular in many fields, such as system administration and web programming.

High-level compiled languages: They are the big boss of Linux development environments. More than 95% of the Linux OS and its applications are built using compiled languages. Linux supports most of the compiled languages – including the most popular, C and C++. Use this route if you want to conqueror the Linux development world the good old traditional way.

This article is more inclined towards C/C++ due to the enormous open source adoption.

Finding programming languages supported by GCC
GCC (GNU Compiler Collection) is the mother of all compilers that is also native to the Linux programming environment. As the name suggests, GCC is not just a compiler for a language but rather a compiler collection that supports many programming languages. Depending upon the specific Linux distribution you are using, GCC’s support for languages could vary. To find out which languages are supported, perform the following command:
[sourcecode language=”cpp”]$ gcc -v
Using built-in specs.
Target: i486-linux-gnu
Configured with: ../src/configure -v –with-pkgversion=’Ubuntu 4.4.3-4ubuntu5’ –with-bugurl=file:///usr/share/doc/gcc-4.4/README.Bugs –enable-languages=c,c++,fortran,objc,obj-c++ –prefix=/usr –enable-shared –enable-multiarch –enable-linker-build-id –with-system-zlib –libexecdir=/usr/lib –without-included-gettext –enable-threads=posix –with-gxx-include-dir=/usr/include/c++/4.4 –program-suffix=-4.4 –enable-nls –enable-clocale=gnu –enable-libstdcxx-debug –enable-plugin –enable-objc-gc –enable-targets=all –disable-werror –with-arch-32=i486 –with-tune=generic –enable-checking=release –build=i486-linux-gnu –host=i486-linux-gnu –target=i486-linux-gnu
Thread model: posix
gcc version 4.4.3 (Ubuntu 4.4.3-4ubuntu5)
This command gives a bunch of information, including the configure options used when building this compiler. ‘–enable-languages’ indicates the languages supported by this compiler. In this case it is ‘–enable-languages=c,c++,fortran,objc,obj-c++’, so the supported languages are C, C++, FORTAN, Objective-C and Objective-C++.

Coloured GCC output

GCC provides excellent information when your code is not compiling or linking properly. But when you are compiling a large project, GCC’s output may not be very helpful. The colorgcc tool can come very handy in this situation. It is a Perl script to colourise the terminal output of GCC, making it easier for you to locate error messages, syntax errors and linking errors within longer compiler outputs. Search for ‘colorgcc’ in your distribution’s package manager, or you can download it from If you want to use colorgcc for all of your GCC-based projects, you will need to set an alias mapping GCC to colorgcc.

Be alarmed
By default GCC prints a rather restrictive set of warnings and errors that inform you about the potential and fatal issues with your code. To get an even better perspective of the code, you can increase the level of warnings generated by GCC, by using the option ‘-Wall’ .

For example, for the following code :
@code: System.c
[sourcecode language=”cpp”]//#include <stdio.h>
//#include <stdlib.h>
void main()
printf (“Files in Directory are :\n”);
system(“ls -l”);

when compiled without -Wall option
kunal@ubuntu-vm-kdeo:~/tips$ gcc system.c -o system
system.c: In function ‘main’:
system.c:6: warning: incompatible implicit declaration of built-in function ‘printf’

when compiled with -Wall option
kunal@ubuntu-vm-kdeo:~/tips$ gcc -Wall system.c -o system
system.c:4: warning: return type of ‘main’ is not ‘int’
system.c: In function ‘main’:
system.c:6: warning: implicit declaration of function ‘printf’
system.c:6: warning: incompatible implicit declaration of built-in function ‘printf’
system.c:7: warning: implicit declaration of function ‘system’
Be disciplined
Experienced programmers will tell you that ignoring warnings is a bad practice. One way to get rid of warnings from your code is to pay attention to the warnings and fix the code. But if you get too lazy, this may method may not work for you. Meet the second option… You ask the compiler to treat warnings as errors. Hence, the compiler won’t even compile your program until you make your code warning-free. This compiler forces a discipline of writing warning-free code.
To treat warnings as errors, you can use the GCC option ‘-Werror’.

[sourcecode language=”cpp”]kunal@ubuntu-vm-kdeo:~/tips$ gcc -Wall -Werror system.c -o system
cc1: warnings being treated as errors
system.c:4: error: return type of ‘main’ is not ‘int’
system.c: In function ‘main’:
system.c:6: error: implicit declaration of function ‘printf’
system.c:6: error: incompatible implicit declaration of built-in function ‘printf’
system.c:7: error: implicit declaration of function ‘system’
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:

    • 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

    • 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.