C++ Basic Kit
Description
C++ (“C Plus Plus”, pronounced /ˌsiːˌplʌsˈplʌs/) is a general-purpose programming language. It is a middle-level language with both high-level and low-level language features. It is a multi-paradigm (object-oriented and generic structured/procedural), compiled language where compilation creates machine code for a target machine hardware.
History
The language was developed by Bjarne Stroustrup in 1985 at Bell Labs as an enhancement to the C (Dennis Ritchie 1973). Enhancements started with the addition of classes, followed by, among other features, virtual functions, operator overloading, multiple inheritance, templates, and exception handling. It is an ANSI-ISO standard language since 1998. The next version of the language c++09 to be released in 2009 will include new features to adapt this language to current needs.
C++ Programming process
C++ programs are written in plain text files commonly called source code files. Here is a reference of different extensions of this files:
linux/original cc
windows/linux/more extended cpp
other C,c++,ccc
headers h,hh
Most compilers do not care but some do it. Then it is better to use cpp file extension in order to widden your projects file names compatibility.
This files are not executable programs. To make an executable program the source code has to be translated into machine code to be interpreted by a concrete machine (i.e., i386). This process is called compilation.
Compilation
Linux has two ANSI C compilers, gcc and g++ compilers. Gcc compiles C, C++ and Objective-C programs. Altough gcc is able to compile C++ programs, it doesn’t make automatically all requiered links to class libraries, it has to use g++. Therefore if you are programming in C++ the best option is g++.
To compile your programs you can use: g++ file.cpp -o executable
If they are more complex use a makefile (make commando)
In Linux system you can do cross-compiling or swap between different compilers using the following environment variables (note: you have to check that the compilers are installed and respond to g++-ver):
- CC for C (gcc) —> example: export CC=gcc-3.4
- CXX for C++ (g++) —> example: export CXX=g++-4.2
First Program – Hello world – Basic program
// comment: sample.cpp #include <iostream> using namespace std; int main (){ cout << "Hello World!"; return 0; } |
Hello World! |
Get command-line options
The basic way is trough argc/argv variables but there are libraries to get options in standard GNU.
c (unistd.h) getopt function (oldest). Only for short options. c++ compatible.
c (getopt.h) getopt_long (best). Standard GNU with long options. c++ compatible.
c++ (libgetopt++|gengetopt) getopt libraries to handle command line options in c++. But could be easier to use getopt.h
Basic IO (iostream)
La la la …
