john said:
how do i set-up and compile a program with class .h class .cpp, and
main()
The details of the compilation are actually environment specific and
thus cannot really be answered here. However, if you use a command
line interface, the process is quite similar on most platforms and
even the compiler flags are identical.
Assume you have a simple setup with three files: one header and two
sources:
// file: foo.h
#if !defined(FOO_H)
#define FOO_H
struct foo { void bar(); };
#endif
// file: foo.cpp
#include "foo.h"
#include <iostream>
void foo::bar() { st::cout << "hello, world\n"; }
// file: main.cpp
#include "foo.h"
int main() { foo().bar(); }
The easiest approach to compile these two files and link the result is
to
simply give all files as command line arguments to the compiler (I will
use "CXX" as the name of the compiler executable although this is not
really the name of any C++ compiler I now):
CXX -o tst foo.cpp main.cpp
Since compiling all files may be rather time consuming, you normally
compile the C++ files to object files and link them in a separate step:
CXX -c foo.cpp // use default name for object file
CXX -o main.o -c main.cpp // explicitly name the object file
CXX -o tst foo.o main.o // link the resulting object files
This is unwieldy once you really have a lot of files in which case you
would start to lump some of them together into libraries and/or shared
libraries. However, this becomes even more environment specific than
the above: although I would guess that the above actually works on all
systems, I'm pretty sure the "preferred" options are actually different
and some of "usual" options are not even documented. Thus, you need to
look up the details in your environment's documentation.