Programming

From stacky wiki
Revision as of 14:45, 10 March 2012 by Anton (talk | contribs) (Created page with "== hand-rolling a library == References: http://www.learncpp.com/cpp-tutorial/19-header-files/ and http://www.linuxquestions.org/questions/programming-9/how-to-compile-a-tiny-si...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

hand-rolling a library

References: http://www.learncpp.com/cpp-tutorial/19-header-files/ and http://www.linuxquestions.org/questions/programming-9/how-to-compile-a-tiny-simple-library-by-g-105795/

Tested this procedure with three files:

// add.h
int add(int,int);
// foo.cpp
#include "add.h"

int add(int x, int y)
{
    return x + y;
}
// main.cpp
#include <iostream>
#include "add.h"
 
int main()
{
    using namespace std;
    cout << "The sum of 3 and 4 is: " << add(3, 4) << endl;
    return 0;
}

It's important that add.h is included using quotes instead of angle brackets; angle brackets are reserved for header files included with the compiler.

Used the following commands to produce a working executable:

g++ -c foo.cpp 
g++ main.cpp -L. foo.o