From charlesreid1

Line 134: Line 134:


</syntaxhighlight>
</syntaxhighlight>
Some important things to notice:
# You still need the header file to use class A
# The source code for class A, <code>A.cc</code>, is not used anywhere.





Revision as of 22:59, 1 December 2010

http://www.codeguru.com/forum/showthread.php?t=

http://www.learncpp.com/cpp-tutorial/121-pointers-and-references-to-the-base-class-of-derived-objects/

Building C++ Programs

Building on the Command-Line

The Hello World page has an example of a simple "hello world" program in C++. You can also visit the page for the 2010 Scientific Computing Summer Workshop page and view the second workshop, which is on creating and building scientific software.


Building via Makefiles

The Make page has an example of how to build a more complex program in C++ using a Makefile to automate the process. Also, as mentioned above, you can visit the 2010 Scientific Computing Summer Workshop page. The second workshop also covers Makefiles.

Building via CMake

Building via Autotools

C++ Libraries

C++ libraries provide a method of encapsulating C++ code and objects, so that they can be used by other programs. Many scientific software packages, such as Hypre or Petsc, provide library interfaces. An example of how to encapsulate C++ code is given below.

Simple library example

(Also see the Scientific Computing Summer Workshop 2.)

This example will show how to encapsulate code for a single C++ class, A.

Creating the library

The directory structure for the library project is as follows:

src/ Source code (.cc)
include/ Include/header files (.h)
lib/ Contains libraries (which we will create)

The code for the project is:

include/A.h src/A.cc
class A {

  public:  
    A( int x ); 
    ~A();  
    void echo(); 

  private:  
    int x_;
};
#include <A.h>
#include <iostream>

A::A( int x ) : x_(x) {}

A::~A() {};

void A::echo() {
  std::cout << "x = " << x_ << "\n";
}

To begin, the class A must be compiled into an object file, but not compiled into an executable (since there's no main() function). To compile code into an object file without creating an executable, use the -c flag for gcc:

$ g++ -g -c src/A.cc -Iinclude/

The -I/path/to/header/files compiler flag tells the compiler where to find the header files that were included using #include "headerfile.h".

After A is compiled into an object file, create a library from A.o using the ar Unix utility, call it libclassA, and put it in the lib/ directory:

$ ar rs lib/libclassA.a A.o

(see the ar man page at http://linux.die.net/man/1/ar)

NOTE: The naming scheme for libraries consists of the prefix "lib" followed by the library name of your choosing (in this case, classA).

Using the library

Compiling a driver that utilizes the library requires a few things:

  1. The header file for class A (but the source code file A.cc is not required)
  2. The compiler must be pointed to the header file location using -I/path/to/header/files
  3. The compiler must be pointed to the library locations using -L/path/to/library/files
  4. The library file libclassA.a; the compiler must be pointed to this library file using -lclassA

A driver that uses class A is given below:

src/Driver.cc
#include <A.h>
#include <iostream>

int main()
{
     A* obj = new A( 5 );
     obj->echo();
     delete obj;
}

This driver may be compiled with g++ like this:

$ g++ src/Driver.cc -o Driver.x -Iinclude/ -Llib/ -lclassA

Some important things to notice:

  1. You still need the header file to use class A
  2. The source code for class A, A.cc, is not used anywhere.