1. Home
  2. Computing & Technology
  3. Linux

Linux Newbie Administrator Guide

From Authors, for About.com

8.2.6 C++

g++ filename.C

GNU C++ compiler. The capital "C" is often used for C++ sources. If you need an "integrated development environment" (IDE), kdevelop is really something you would probably like to look at.

How do I compile a simple C++ program?

Just like in c, I open a text editor and write my program. For example, using pico, I write the following program:

//This is a comment (to the end of line, C++ style)

#include <stdio.h>

#include <math.h>

#include <iostream.h>

#include <stdlib.h>

//define a function

double wheeldrop (double dGap, double dDiameter) {

double dDrop, dRadius, dNotDrop;


dRadius = dDiameter * 0.5;

dDrop = dRadius - sqrt( (dRadius*dRadius)-(0.25*dGap*dGap) ) ;


return (dDrop) ;

} //end of the function


//The function main is the entry point to the program

void main(void) {

double dGap, dDiameter, dDrop, dRadius, dNotDrop; //variables


for (;;) { //infinite loop

cout << "Please enter gap between track segments and \n"

<< "diameter of train wheel in inches (-1 -1 to exit): ";

cin >> dGap >> dDiameter;


if ((dGap == -1) && (dDiameter == -1))

break;

else if (dGap < dDiameter) { //do calculations

dDrop = wheeldrop (dGap, dDiameter) ;

printf ("The wheel will drop %f inches.\n\n", dDrop) ;

}

else {

printf ("Error, your train is going to crash.\n Gap bigger then wheel!\n\n") ;

}

}

}

I save the source to the file "train.c", and then invoke the GNU C++ compiler to compile the file "train.c" to an executable called "traincalc":

g++ -o traincalc train.c

I can then run the executable by typing:

./traincalc


kdevelop

(type in X-terminal) Integrated development environment for K. It is really worth downloading (if it does not come with your distribution).


glade

(type in X-terminal) A graphical builder of user interfaces.

"Glade is an interface builder developed by Damon Chaplin. It allows graphical and interactive construction of Gnome/Gtk graphical user interfaces. From Glade, the generated interface can be saved in a xml file or directly exported to C code to be included in a C source tree. Glade also allows to define the name of the handlers - functions - to be attached to the various event of the interface. For example the function (name) to be called when a specific menu item is pressed." (From: http://linuxtoday.com/news_story.php3?ltsn=2000-07-16-013-04-PS-GN)

What "C" functions are available for programming under Linux?

Too many for a newbie like myself. I started by studying the header files (*.h) in the directory /usr/include and all its subdirectories. To find a header file that contains a prototype for a given function (e.g., cosh) I would do something like:

cd /usr/include

grep -H "cosh" *.h

There are also many interesting libraries that are not a part of a typical distribution, e.g., GNU libraries for scientific computing (GSL): http://sources.redhat.com/gsl/. Also check: http://www.phy.duke.edu/~hsg/sci-computing.html.

* License

* Linux Newbie Administrator Guide Index

Explore Linux

More from About.com

  1. Home
  2. Computing & Technology
  3. Linux
  4. Linux Documentation
  5. Newbie Administrator Guide
  6. Linux Newbie Administrator Guide - 8.2 Simple Programming under Linux

©2008 About.com, a part of The New York Times Company.

All rights reserved.