| re: How Can I link a .cp file in the "main" header?
Jofio wrote:[color=blue]
> I have a .h (header file) linked using #include<...>. I also have a .cp
> file which i don't know how to include in the header of my "main" file.
> I have the following c++ file.
> The file names are: 1) dArray.h
> 2) dArray.cp
> 3) dArrayTest.cpp
>
> dArray.h file declares and defines a class called DynamicArray.
> dArray.cp defines the functions declared as member functions in
> DynamicArray class in dArray.h. This dArray.h is includded in the
> header of aArray.cp with #include<...> precompiler directive.
>
> Question is: How do I include dArray.cp in the main program called
> dArrayTest.cpp? Do I use the same pre-compiler directive? And do all
> the 3 files need to be in the same folder for the calling file to "see"
> the included file?[/color]
Almost all C++ implementations use two phases. In the first phase,
the compiler compiles every translation unit. A translation unit is
a source file plus all its #include's. Typically, that's one .cpp file,
some standard headers like <algorithm> and your own .h headers.
I.e. .h files are *included*.
In the second phase, the translated unites are *linked* together,
and the tool is of course called a linker. Basically this glues the
..cpp files from phase 1 together.
In your case, the dArray.cpp file should be linked to dArrayTest.cpp
by the linker, not by an include. Unlike #include, the mechanism for
that is left to the implementation. Usually there's some kind of
makefile or project file.
HTH,
Michiel Salters |