Board index » cppbuilder » include

include

hi,

This is a question about what we actually have to #include when doing
our own lib. Lets say I have prog.cpp, and prog.h; then I have my lib
called myLib.cpp and myLib.h.

For some reason I have found BCB does not always work they way I expect
based on C++.  To get my library included do I not just include the .h
file in prog.h?  It seems I need to stick it everywhere.  THEN to top it
of, I often need to include or useunit myLib.cpp in prog.cpp.  And why
do some libraries get put in the .cpp file, while others are included
from the .h file?  Is the header not where all this stuff should go?

Anyone care to outline where those files need to be included.  And what
that USEUNIT is for? I don't think it's a standard C++ convention (in
fact it's often does not work).

TThanks,
    JJason

--
.............
......... Jason C. Leach
...... University College of the Cariboo
... j...@mail.ocis.net
.. http://www.ocis.net/~jcl
.

The Search for Extraterrestrial Intelligence from Home:
http://setiathome.ssl.berkeley.edu

 

Re:include


Quote
Jason wrote:
> This is a question about what we actually have to #include when doing
> our own lib. Lets say I have prog.cpp, and prog.h; then I have my lib
> called myLib.cpp and myLib.h.

> Anyone care to outline where those files need to be included.  And what
> that USEUNIT is for? I don't think it's a standard C++ convention (in
> fact it's often does not work).

Jason,

        That had me confused too when I first got started using C++ about a
year ago.  If you need to use functions from myLib.cpp in your project,
use Project - Add to Project and select myLib.cpp to add to your
project.  The IDE will add the USEUNIT statements to your project .cpp
file as necessary - you should rarely (if ever) have to
modify/add/whatever the USEUNIT statements yourself.
        Now, in any units where you need to use the functions from myLib.cpp,
just add #include "myLib.h" to that unit's .cpp file.  You don't need to
include the cpp file - the .h file holds the declarations for the
classes/functions in the .cpp file, and BCB knows to look in the .cpp
file for the actual implementation of the functions.
        If you get in the habit of including stuff in header files, or of
including .cpp files, you can end up creating a mess that takes forever
to compile (if it compiles at all).  Been there, done that. :)

Hope this makes sense,
Alan McIntyre
a...@norfolkgraphics.com
http://norfolkgraphics.com

Re:include


The header is the file where you put the declarations of functions, data
structures,... usualy no definitions of any kind.
The definitions go to a cpp file (lib,obj) which is included in the project
to be linked together with the other files.
See this example

//////File a.h
void MyFunction(int a);

//////File a.cpp
void MyFunction(int a){/*do something*/};

//////File b.cpp
#include "a.h"
// here the compiler knows about the existance of the function MyFunction
void OtherFunction()
{
    // bla
    MyFunction(3);
    // bla bla

Quote
};

Now you can compile b.cpp, but to you need to link the definition of
MyFunction with it, so you put it in the project

Hope this helps

Quote
Jason wrote:
> hi,

> This is a question about what we actually have to #include when doing
> our own lib. Lets say I have prog.cpp, and prog.h; then I have my lib
> called myLib.cpp and myLib.h.

> For some reason I have found BCB does not always work they way I expect
> based on C++.  To get my library included do I not just include the .h
> file in prog.h?  It seems I need to stick it everywhere.  THEN to top it
> of, I often need to include or useunit myLib.cpp in prog.cpp.  And why
> do some libraries get put in the .cpp file, while others are included
> from the .h file?  Is the header not where all this stuff should go?

> Anyone care to outline where those files need to be included.  And what
> that USEUNIT is for? I don't think it's a standard C++ convention (in
> fact it's often does not work).

> TThanks,
>     JJason

> --
> .............
> ......... Jason C. Leach
> ...... University College of the Cariboo
> ... j...@mail.ocis.net
> .. http://www.ocis.net/~jcl
> .

> The Search for Extraterrestrial Intelligence from Home:
> http://setiathome.ssl.berkeley.edu

Re:include


Oops, almost forgot - you can get the IDE to add the #include's by using
the File - Include Unit Header menu option.

--
Alan McIntyre
a...@norfolkgraphics.com
http://norfolkgraphics.com

Re:include


Any compilation unit that uses code from another unit has to declare the
prototype, and one way to do that is to include the header for that code.
There is nothing strange about that.

Do NOT include the lib's .cpp file in your project, it is compiled into a
.lib.  Adding the .cpp for the lib is not using a lib at all, but simply
compiling it directly into your application.  Add the .lib to your project
directly the same way you would add a .cpp file.

The USE... macros are for the IDE's project manager.  They are macros, and
therefore legal C++, but they are quite lame IMHO, and any real project
manager would never modify source files to manage a project, but the BCB
project manager is not its strong point.  I'm hoping every day that they
dedicate a large number of resources to improve the PM in general.

Chris (TeamB)

Quote
Jason wrote in message <37BC1CCA.15805...@mail.ocis.net>...
>hi,

>This is a question about what we actually have to #include when doing
>our own lib. Lets say I have prog.cpp, and prog.h; then I have my lib
>called myLib.cpp and myLib.h.

>For some reason I have found BCB does not always work they way I expect
>based on C++.  To get my library included do I not just include the .h
>file in prog.h?  It seems I need to stick it everywhere.  THEN to top it
>of, I often need to include or useunit myLib.cpp in prog.cpp.  And why
>do some libraries get put in the .cpp file, while others are included
>from the .h file?  Is the header not where all this stuff should go?

>Anyone care to outline where those files need to be included.  And what
>that USEUNIT is for? I don't think it's a standard C++ convention (in
>fact it's often does not work).

>TThanks,
>    JJason

>--
>.............
>......... Jason C. Leach
>...... University College of the Cariboo
>... j...@mail.ocis.net
>.. http://www.ocis.net/~jcl
>.

>The Search for Extraterrestrial Intelligence from Home:
>http://setiathome.ssl.berkeley.edu

Re:include


ok,

If I open BCB it pops up with a new prog.  In unit1.cpp there is
#include vcl.h, and in the unit1.h there are several #include .hpp.
What's going on here? Why include vcl in one, and the others on the .h?
Secondly, why are some denoted with .hpp, and finally I was told that
using #include <iostream> uses the most current version of the lib,
where as #include <iostream.h> uses an older one.  Any truth to this?

TThanks,
    Jason

--
.............
......... Jason C. Leach
...... University College of the Cariboo
... j...@mail.ocis.net
.. http://www.ocis.net/~jcl
.

The Search for Extraterrestrial Intelligence from Home:
http://setiathome.ssl.berkeley.edu

Re:include


The .h file only tells the files that #include it HOW to call a function
provided elsewhere.  The .cpp or .lib provides the actual function code.
Quote
Jason wrote:
> hi,

> This is a question about what we actually have to #include when doing
> our own lib. Lets say I have prog.cpp, and prog.h; then I have my lib
> called myLib.cpp and myLib.h.

> For some reason I have found BCB does not always work they way I expect
> based on C++.  To get my library included do I not just include the .h
> file in prog.h?  It seems I need to stick it everywhere.  THEN to top it
> of, I often need to include or useunit myLib.cpp in prog.cpp.  And why
> do some libraries get put in the .cpp file, while others are included
> from the .h file?  Is the header not where all this stuff should go?

> Anyone care to outline where those files need to be included.  And what
> that USEUNIT is for? I don't think it's a standard C++ convention (in
> fact it's often does not work).

> TThanks,
>     JJason

> --
> .............
> ......... Jason C. Leach
> ...... University College of the Cariboo
> ... j...@mail.ocis.net
> .. http://www.ocis.net/~jcl
> .

> The Search for Extraterrestrial Intelligence from Home:
> http://setiathome.ssl.berkeley.edu

Re:include


You want to include as few as possible in the header, and as many as
possible in the cpp file.  Of course if the header NEEDS the other headers,
then obviously it should include them.

But why?  Precompiled headers are only built from headers included from a
.cpp file.  Also, since a header is likely included in many places, the
fewer nested includes it has, the fewer files the preprocessor must open and
scan.  It multiplies across every unit that includes your header.

On the other hand, a .cpp file is only compiled once.  If a class is
referenced in a header, a forward declaration may be all that is required,
rather than including its header.  If that class is USED in the header, the
its header must be included, but if a pointer to it is declared in the
header, a prototype is all you need.  The .cpp file will include its header
because it is used there.

This can make an incredible difference in the amount of time it takes to
compile.

Chris (TeamB)

Quote
Jason wrote in message <37BCDA2B.DF235...@mail.ocis.net>...
>ok,

>If I open BCB it pops up with a new prog.  In unit1.cpp there is
>#include vcl.h, and in the unit1.h there are several #include .hpp.
>What's going on here? Why include vcl in one, and the others on the .h?
>Secondly, why are some denoted with .hpp, and finally I was told that
>using #include <iostream> uses the most current version of the lib,
>where as #include <iostream.h> uses an older one.  Any truth to this?

>TThanks,
>    Jason

>--
>.............
>......... Jason C. Leach
>...... University College of the Cariboo
>... j...@mail.ocis.net
>.. http://www.ocis.net/~jcl
>.

>The Search for Extraterrestrial Intelligence from Home:
>http://setiathome.ssl.berkeley.edu

Other Threads