multiple files

J

john townsley

i have a .cpp file and i want to have my class and member functions in a
seperate file. if i put the class heading file in a .h and the member
functions for the class in a .cpp. how do i compile the 2 .cpp files eg 1
with class stuff and other main(). i know i include the .h file in the .cpp
class member file but i cant compile the project without errors.

how do i set-up and compile a program with class .h class .cpp, and main()
 
D

Dietmar Kuehl

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.
 
H

Howard

john townsley said:
i have a .cpp file and i want to have my class and member functions in a
seperate file. if i put the class heading file in a .h and the member
functions for the class in a .cpp. how do i compile the 2 .cpp files eg 1
with class stuff and other main(). i know i include the .h file in the
.cpp
class member file but i cant compile the project without errors.

how do i set-up and compile a program with class .h class .cpp, and main()

What compiler? For most IDE's, you add all the .cpp files to your project.
They will then get compiled when you build the project. But for specifics
on a given compiler, ask in a newsgroupd dedicated to that compiler.

-Howard
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,201
Messages
2,571,052
Members
47,656
Latest member
rickwatson

Latest Threads

Top