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…

Running commands from C
Sometimes it is very helpful to run a program or command that is already installed on the system, rather than developing your own solution to do the same job. You can run Linux commands (and programs) by using the ‘system()’ function. It is defined as ‘int system(char *string)’, where ‘string’ can be the name of a UNIX utility, an executable shell script or a user program.

[sourcecode language=”cpp”]#include <stdio.h>
#include <stdlib.h>

void main()
printf (“Files in the Directory are :\n”);
system(“ls -l”);

kunal@ubuntu-vm-kdeo:~$ tips/system
Files in the Directory are :
total 103028
drwxr-xr-x 2 kunal kunal      4096 2010-09-20 10:39 Desktop
drwxr-xr-x 2 kunal kunal      4096 2010-09-20 15:31 Documents
drwxr-xr-x 2 kunal kunal      4096 2010-09-20 08:09 Downloads
-rw-r–r– 1 kunal kunal       179 2010-08-26 16:13 examples.desktop
-rw-r–r– 1 kunal kunal       105 2010-09-20 17:37 main.c
drwxr-xr-x 2 kunal kunal      4096 2010-09-20 13:01 Music
drwxr-xr-x 2 kunal kunal      4096 2010-09-20 13:01 Pictures
drwxr-xr-x 2 kunal kunal      4096 2010-09-20 13:01 Public
drwxr-xr-x 2 kunal kunal      4096 2010-09-20 13:01 Templates
drwxr-xr-x 2 kunal kunal      4096 2010-09-21 10:12 tips
drwxr-xr-x 2 kunal kunal      4096 2010-09-20 13:01 Videos
-rw-r–r– 1 kunal kunal 105445445 2010-08-01 22:11 VMwareTools-8.4.3-282344.tar.gz
drwxr-xr-x 7 kunal kunal      4096 2010-08-01 22:11 vmware-tools-distrib
Creating and applying patches
A Patch is a piece of code which is used to fix problems in a program. Patches are very popular in the open source software world. Most open source projects accept patches from community members, and as they mature they become core members of the project. So in a way patches are tickets to becoming an open source developer. Patch files contain the line-by-line differences between the original and the modified document. These patch files can further be used by the patch utility to apply the changes to the original file.

To create a patch file:
[sourcecode language=”cpp”]$ diff -uE qt_hello_v1.cpp qt_hello_v2.cpp > patch.diff
Sample patch file contents:
[sourcecode language=”cpp”]— qt_hello_v1.cpp     2010-09-21 14:11:39.145133443 +0530
+++ qt_hello_v2.cpp     2010-09-22 07:18:17.064900182 +0530
@@ -6,6 +6,8 @@
QApplication app(argc, argv);
QPushButton hello(“Hello world!”);
hello.resize(100, 30);
+  hello.setFont(QFont(“Times”, 18, QFont::Bold));
+  QObject::connect(&hello, SIGNAL(clicked()), &app, SLOT(quit()));;
return app.exec();
To apply a patch:
[sourcecode language=”cpp”]$ patch -b < patch.diff
This command will also create a backup of the original file with the .orig extension.

Conditional compilation
One of the primary strengths of open source software is its cross-platform compatibility. By making your software cross-compatible, you are not only playing nice with the open source community but also expanding the market for your software. There are multiple ways by which you can achieve cross-platform compatibility, out of which one of the methods is called conditional compilation.

You can use conditional compilation to select particular sections of code to compile, while excluding other sections. The #if, #ifdef, #ifndef, #else, #elif and #endif directives can be used for conditional compilation.
In the following example we are placing platform-specific code which will be compiled differently on each platform.

For example:
@code: ifdef.c
[sourcecode language=”cpp”]#include<stdio.h>
int main()
#ifdef __APPLE__
printf (“Running on an Apple Platform\n”);
#ifdef __GNUC__
printf (“Using the GNU compiler\n”);
#ifdef __WIN32
printf (“Running on a Microsoft Platform\n”);
return 0;
On Linux:
Using the GNU compiler
On Mac:
Running on an Apple Platform
Using the GNU compiler
On Windows (With Visual C++):
Running on a Microsoft Platform
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

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