accessing a library

C

Carmen Sei

the C++ way of accessing a library is using #include, consider the
following include herarichy:

main.cpp -> Demo.h -> Config.h -> Gconfig.h -> ConfigDef.h

the above shows main.cpp include -> Demo.h and Demo.h include ->
Config.h ... and so on

then basically, main.cpp had inheritan everything of the 4 header
files (Demo.h, Config.h, Gconfig.h, ConfigDef.h)

Sometimes, the include chain gets longer and One would need to dig
into deep level to see a function call defined in which header library
file.


On the other hand, in Java, one uses import to access a Library file
like:

import java.io.File

Java doesn't follow the deep chian like C++ include header files does.
If you import a java library like java.io.InputStream, you will have
access to InputStream only and doesn't have C++'s chain header files.

Does C++ programmer found the include chain is hard to manage once the
source base grows big? and most of time, one needs to dig into the
header file like 3 level up to find the object/variable is defined.
 
I

Ian Collins

Carmen said:
Does C++ programmer found the include chain is hard to manage once the
source base grows big? and most of time, one needs to dig into the
header file like 3 level up to find the object/variable is defined.

No, just follow a standard naming convention and you'll know where to look.

How's Eric today?
 
S

Stefan Ram

Carmen Sei said:
the C++ way of accessing a library is using #include

This only declares the names, so that the compiler
can check the code.

The C++ way of accessing a library is to tell the linker
to link with it. (Actually, this is beyond ISO/IEC 14882:2003(E).)
On the other hand, in Java, one uses import to access a Library
file like: import java.io.File

This only defines »File« to abreviate »java.io.File«,
when used as a reference type name in the compilation
unit containing this import declaration.

The Java way of accessing a library is to tell the class
loader where to look for classes.
 
J

James Kanze

the C++ way of accessing a library is using #include, consider the
following include herarichy:
main.cpp -> Demo.h -> Config.h -> Gconfig.h -> ConfigDef.h
the above shows main.cpp include -> Demo.h and Demo.h include ->
Config.h ... and so on
then basically, main.cpp had inheritan everything of the 4 header
files (Demo.h, Config.h, Gconfig.h, ConfigDef.h)

More or less. This is normally irrelevant for the programmer
who writes main. The author of Demo.h has documented what is
defined in that header, any includes that my be in the header
are generally "implementation details", which the user of the
header can ignore. If the author of main wants to use something
defined in Config.h, he includes Config.h. Regardless of
whether Demo.h includes it or not.
Sometimes, the include chain gets longer and One would need to
dig into deep level to see a function call defined in which
header library file.

You've got it backwards. The documentation of a class or a
function should specify what header you need to include in order
to use it. (At the application level, this is often done by
means of a naming convention. To use MyClass, you include
"MyClass.hh", for example. There are only occasionally reasons
to have one header define several different classes.)

Once you know what header to use, you include it.
On the other hand, in Java, one uses import to access a
Library file like:
import java.io.File
Java doesn't follow the deep chian like C++ include header
files does. If you import a java library like
java.io.InputStream, you will have access to InputStream only
and doesn't have C++'s chain header files.

Java's import doesn't (formally) work at the file level, so the
issues are different. But from a user's point of view, the
difference is marginal, at best. If you want to use File, in
Java, you look up the documentation, which tells you, amongst
other things, that it is in the package java.io, so you know
that you need to import java.io.File. If you want to use my
class File, you look up the documentation, which tells you that
it is part of the library Gabi, in namespace Gabi, and the
conventions for the library Gabi is that the include file is
gb/ClassName.hh, so you know that you need to include
"gb/File.hh". About the only real difference is that Java
imposes a certain identity between the namespace (package, in
Java) and class, and the filename, and C++ doesn't. Any decent
C++ library, however, will establish conventions.
Does C++ programmer found the include chain is hard to manage
once the source base grows big? and most of time, one needs to
dig into the header file like 3 level up to find the
object/variable is defined.

No. I don't normally look into the headers in C++ except for
code that I'm actively maintaining myself. And I've never
"followed" an included chain.
 

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,175
Messages
2,570,944
Members
47,491
Latest member
mohitk

Latest Threads

Top