Recent Changes - Search:

Coding C++

Computing

Start-up notes on Programming in C++

(import in progress from http://www.src.wits.ac.za/groups/psi/linux/c/index.html by Simon Connell)

Trail Index | Conclusion | Start-up notes on Programming in C++

Introduction

If you arrived to this page first, and you are not familiar with Linux, you may want to check the Linux Introduction? first. This page focuses on how to write, compile, link and execute a C++ program in Linux, and assumes a minimum of familiarity with it.

Editors

Here you will find some suggestions of programs you can use to edit source code. You can also see Writing Code for general considerations on writing source code.

Kate

kate is a simple window-based editor that has syntax highlighting, and is more familiar if you are accustomed to Windows.
Usage:

 kate <filename>

You right click in the text window and select the highlighting style you want. Kate' has two serious drawbacks: it does not offer automatic indentation, and it is not integrated with the compiler.

Emacs

Emacs (or its variant XEmacs) is an advanced editor with C++ aware syntax highlighting, auto-indenting, integration with compiler, debugger and versioning, and sophisticated macro capabilities. Another advantage of Emacs is that it can also be used in a terminal window, in text mode, which is very useful if you are working on a remote host.
Usage

 emacs <filename>
 xemacs <filename>
  • You may have to go to the menu Help/Options/Global font lock to switch on the syntax highlighting, or type ALT-x font-lock-mode
  • Typing TAB at the beginning of a line will auto-indent the line.
  • You can select a text block ("region", in Emacs jargon) using CTRL-SPACE and moving the cursor. CTRL-w cut, ESC-w copy, CTRL-y paste
  • You can also use the normal Unix mouse keys for selecting and pasting
  • CTRL-a to go to beginning of line, CTRL-e to end of line
  • You can indent a whole selected block with CTRL-ALT-* The Tools/Compile menu item gives you access to the integration with the compiler (using makefiles); it will open another frame in the window with the output of the compiler, where you can click on an error to open the relevant file, at the exact line.
  • The Tools/Compare offers a powerful help for reviewing the differences between two or three text files.
  • Typing ALT-x speedbar will open a panel with a tree view of directory and files, which is very helpful to navigate in large source codes.

Simply play around in the editor to learn more of its capabilities.

Others

Or, use vi or a related product .... this is a scary editor, but will be the fastest if you learn it well. It is also an editor that you can always find, even in minimal installations of Linux.

If you have used before an IDE (Integrated Development Environment) like Visual Studio, you may find yourself most comfortable with Eclipse. I would anyway note that usually IDEs are not practical for writing small pieces of code like those for data analysis.

Compiler

For this tutorial we use the GCC compiler suite. You can check its installation on your Linux machine using

 $ rpm -qa | grep gcc

Most of the examples in this tutorial can be compiled simply with a command like

 $ g++ helloworld.cpp -o helloworld

which produces a binary executable file filename - it's a convention in Unix that executable files have no extension. You can run it by typing

 $ ./helloworld

You can see Compiler for some background, available options and more details.

Make utility and Makefiles

The make utility automatically determines which pieces of a large program based on (multiple object files and library files) need to be recompiled, and issues commands to recompile them. You need a file called a Makefile to tell make what to do.

You can see Makefiles for some background, available options and more details.

Hello World Program

helloworld.cc

/*************************************************************************
 * Program Name : Helloworld            Written by : S.H.Connell         *
 *************************************************************************/


/****************************
 *     Include files        *
 ****************************/


#include <iostream>

/*************************************************
 * Function : main                               *
 *************************************************/

int main(void)
{
    std::cout << "\n\n Hello World   \n\n " ;
    return 0;
}

Makefile (You will have to put in the tabs yourself if you cut and paste this code segment)

CXX = g++
CC = g++
CXXFLAGS= -I. -Wall -Wno-deprecated
# Use -O3 if you trust your optimizer, or
# -g to make a debugging version of this program
LDLIBS=-lm

TARGET=helloworld

all : $(TARGET)

helloworld: helloworld.o

clean:
-rm -f *.o *~ .*~ core .\#*
realclean: clean
-rm -f $(TARGET)

Once you have the files, you can run

$ make
$ ./helloworld

Conclusion

  • Now ..... try more tutorial examples from the C++ Web Guide, ... has tutorials under the sources link, or use your own favourite, for example. See here for I/O issues. Input/Output in C++
  • Now .... try to do the projects from the Computational Physics Course, especially the ones where I have provided the solutions.
  • and then you can continue the Programming Tutorials?...

< Conclusion | Trail Index

Edit - History - Print - Recent Changes - Search
Page last modified on February 26, 2009, at 04:33 pm