Notice: Undefined index: order_next_posts in /nas/content/live/gadgetmag/wp-content/plugins/smart-scroll-posts/smart-scroll-posts.php on line 194

Notice: Undefined index: post_link_target in /nas/content/live/gadgetmag/wp-content/plugins/smart-scroll-posts/smart-scroll-posts.php on line 195

Notice: Undefined index: posts_featured_size in /nas/content/live/gadgetmag/wp-content/plugins/smart-scroll-posts/smart-scroll-posts.php on line 196

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…

This article originally appeared in issue 93 of Linux User & Developer magazine.

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 username=”linuxusermag”]