C++: Difference between revisions
From charlesreid1
| Line 25: | Line 25: | ||
== Building via Autotools == | == Building via Autotools == | ||
{{Main|Autotools}} | |||
= C++ Libraries = | = C++ Libraries = | ||
Revision as of 22:53, 1 December 2010
http://www.codeguru.com/forum/showthread.php?t=
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
For more details on building C++ programs via CMake, visit the CMake page.
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/After A is compiled into an object file, create a library from A.o using the ar Unix utility:
$ ar rs lib/libclassA.a A.o(see the ar man page at http://linux.die.net/man/1/ar)
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:
- The header file for class A (but the source code file
A.ccis not required) - The compiler must be pointed to the header file location using
-I/path/to/header/files - The compiler must be pointed to the library locations using
-L/path/to/library/files - 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