473,769 Members | 6,653 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

including a lib header file

Hi
is it right to have a line like
#include <path/to/header.hfor a library on my system, in my header
file and use some functions provided by this library in the
implementation file (file.cpp) inside a class with out declaring those
functions in the class declaration in the header file?

thanks
Jul 22 '06 #1
11 2811
Gary Wessle wrote:
is it right to have a line like
#include <path/to/header.hfor a library on my system, in my header
file and use some functions provided by this library in the
implementation file (file.cpp) inside a class with out declaring those
functions in the class declaration in the header file?
If it works for you, what is your conccern?

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jul 22 '06 #2
Gary Wessle wrote:
Hi
is it right to have a line like
#include <path/to/header.hfor a library on my system, in my header
file and use some functions provided by this library in the
implementation file (file.cpp) inside a class with out declaring those
functions in the class declaration in the header file?

thanks
Believe it or not, this can be a complicated subject.

If you need something from the library header in order to write your own
header, then this is typical:

mystuff.h:

#include "path/to/somelib.h"

a_type_from_som elib a_function_that _i_provide();

mystuff.cpp

#include "mystuff.h"

a_type_from_som elib a_function_that _i_provide()
{
return a_function_from _somelib();
}

If you do NOT need something from the library header in order to write
your own header, but you do need something from the library in order to
write your implementations then this is typical:

mystuff.h:

void a_function_that _i_provide();

mystuff.cpp

#include "myclass.h"
#include "path/to/somelib.h"

void a_function_that _i_provide()
{
a_function_from _somelib();
}

And of course, if you don't need the library header at all, then this is
typical:

mystuff.h:

void a_function_that _i_provide();

mystuff.cpp

#include "myclass.h"

void a_function_that _i_provide()
{
}

The difference between the first and the second case is that in the
first case you expose your users to somelib.h, while in the second case
you do not. Mildly prefer not to expose them. Don't prefer it strongly
though. For example, don't go this far:

********* A STEP TOO FAR ********
mystuff.h:

// my_own_type is the same as a_type_from_som elib
// I looked that type up and made my own just to avoid
// including somelib.h
typedef int my_own_type;
my_own_type a_function_that _i_provide();

mystuff.cpp

#include "path/to/somelib.h"
#include "mystuff.h"

my_own_type a_function_that _i_provide()
{
return a_function_from _somelib();
}
********* A STEP TOO FAR ********

If you follow those rules, then you are honoring the informal rule that
all of us (c++ programmers) more or less follow: If I need something
from another header to use the stuff in your header, then include it for
me. If I don't, then don't.

There's a flaw in this approach that you should be aware of. Consider
this possibility.

somethingelse.h

typedef int a_type_from_som elib;
typedef bool stealth_type;

somelib.h

#include "path/to/somethingelse.h "

a_type_from_som elib a_function_from _somelib();

mystuff.h

#include "path/to/somelib.h"

a_type_from_som elib a_function_that _i_provide();

mystuff.cpp

#include "mystuff.h"

a_type_from_som elib a_function_that _i_provide()
{
return a_function_from _somelib();
}

void another_functio n()
{
stealth_type i_am_a_stealth_ dependency;
}

The code in mystuff.cpp has a stealth dependency on somethingelse.h .
stealth_type is available to you in mystuff.cpp, so the program
compiles. It's an accident though: the REASON that stealth_type is
available is that a_type_from_som elib is needed.

The guy who maintaind somelib.h would be perfectly within his (informal)
rights to decide that he no longer needs to include somethingelse.h and
rewrite this way:

somelib.h

typedef int a_type_from_som elib;
a_type_from_som elib a_function_from _somelib();

If he did, mystuff.cpp wouldn't compile anymore because of the stealth
dependency. The proper way to write mystuff.cpp is:

mystuff.cpp

#include "mystuff.h"
#include "path/to/somethingelse.h "

a_type_from_som elib a_function_that _i_provide()
{
return a_function_from _somelib();
}

void another_functio n()
{
stealth_type i_am_no_longer_ stealthed;
}

If you are fastidious enough, then you can avoid this mistake in your
own code.

Unhappily, the people who use your code (include mystuff.h) may not be
so fastidious, and they might build in a stealth dependency on
somethingelse.h . When their code blows up, of course, they will blame you.
Jul 22 '06 #3
Howard Gardner <hg******@rawbw .comwrites:
Gary Wessle wrote:
Hi
is it right to have a line like
#include <path/to/header.hfor a library on my system, in my header
file and use some functions provided by this library in the
implementation file (file.cpp) inside a class with out declaring those
functions in the class declaration in the header file?
thanks

Believe it or not, this can be a complicated subject.

If you need something from the library header in order to write your
own header, then this is typical:

mystuff.h:

#include "path/to/somelib.h"

a_type_from_som elib a_function_that _i_provide();

mystuff.cpp

#include "mystuff.h"

a_type_from_som elib a_function_that _i_provide()
{
return a_function_from _somelib();
}

If you do NOT need something from the library header in order to write
your own header, but you do need something from the library in order
to write your implementations then this is typical:

mystuff.h:

void a_function_that _i_provide();

mystuff.cpp

#include "myclass.h"
#include "path/to/somelib.h"

void a_function_that _i_provide()
{
a_function_from _somelib();
}

And of course, if you don't need the library header at all, then this
is typical:

mystuff.h:

void a_function_that _i_provide();

mystuff.cpp

#include "myclass.h"

void a_function_that _i_provide()
{
}

The difference between the first and the second case is that in the
first case you expose your users to somelib.h, while in the second
case you do not. Mildly prefer not to expose them. Don't prefer it
strongly though. For example, don't go this far:

********* A STEP TOO FAR ********
mystuff.h:

// my_own_type is the same as a_type_from_som elib
// I looked that type up and made my own just to avoid
// including somelib.h
typedef int my_own_type;
my_own_type a_function_that _i_provide();

mystuff.cpp

#include "path/to/somelib.h"
#include "mystuff.h"

my_own_type a_function_that _i_provide()
{
return a_function_from _somelib();
}
********* A STEP TOO FAR ********

If you follow those rules, then you are honoring the informal rule
that all of us (c++ programmers) more or less follow: If I need
something from another header to use the stuff in your header, then
include it for me. If I don't, then don't.

There's a flaw in this approach that you should be aware of. Consider
this possibility.

somethingelse.h

typedef int a_type_from_som elib;
typedef bool stealth_type;

somelib.h

#include "path/to/somethingelse.h "

a_type_from_som elib a_function_from _somelib();

mystuff.h

#include "path/to/somelib.h"

a_type_from_som elib a_function_that _i_provide();

mystuff.cpp

#include "mystuff.h"

a_type_from_som elib a_function_that _i_provide()
{
return a_function_from _somelib();
}

void another_functio n()
{
stealth_type i_am_a_stealth_ dependency;
}

The code in mystuff.cpp has a stealth dependency on
somethingelse.h . stealth_type is available to you in mystuff.cpp, so
the program compiles. It's an accident though: the REASON that
stealth_type is available is that a_type_from_som elib is needed.

The guy who maintaind somelib.h would be perfectly within his
(informal) rights to decide that he no longer needs to include
somethingelse.h and rewrite this way:

somelib.h

typedef int a_type_from_som elib;
a_type_from_som elib a_function_from _somelib();

If he did, mystuff.cpp wouldn't compile anymore because of the stealth
dependency. The proper way to write mystuff.cpp is:

mystuff.cpp

#include "mystuff.h"
#include "path/to/somethingelse.h "

a_type_from_som elib a_function_that _i_provide()
{
return a_function_from _somelib();
}

void another_functio n()
{
stealth_type i_am_no_longer_ stealthed;
}

If you are fastidious enough, then you can avoid this mistake in your
own code.

Unhappily, the people who use your code (include mystuff.h) may not be
so fastidious, and they might build in a stealth dependency on
somethingelse.h . When their code blows up, of course, they will blame
you.

thank you

you did not explicitly mention whether I can place
a_function_from _somelib in the public scope of a
a_class_that_i_ provide but I take it that you implied it. if so, then
why I getting this error which appear to be a linker error?

*************** * start error *************** *
fred@debian:~/myPrograms/common$ make
g++ -c -o read_data.o read_data.cpp
g++ -c -o read_data_test. o read_data_test. cpp
g++ -Wall -o proj read_data.o read_data_test. o -lgsl
/usr/lib/gcc/i486-linux-gnu/4.0.4/../../../../lib/libgsl.so: undefined
reference to `cblas_dsdot'
/usr/lib/gcc/i486-linux-gnu/4.0.4/../../../../lib/libgsl.so: undefined
reference to `cblas_dswap'
.... a long list of them
*************** * end error *************** *
for this project

*************** * start read_data.h *************** *
#ifndef READ_DATA_H
#define READ_DATA_H
#include <string>
#include <cstdio>
#include <gsl/gsl_matrix.h>
class read_data
{
int nRows, nCol;
std::string file_name;
void set_No_of_Rows_ Cols();
gsl_matrix * m; // from gsl_matrix.h
void matrix_the_file ();

public:
read_data(std:: string const& fileName);
~read_data();

};
#endif
*************** * end read_data.h *************** *
*************** * start read_data.cpp *************** *
#include <string>
#include <fstream>
#include <iostream>
#include <sstream>
#include <cstdlib>
#include <cstdio>
#include "read_data. h"
#include <gsl/gsl_matrix.h>

using namespace std;

read_data::read _data( string const& fileName )
: file_name( fileName ), nCol(0), nRows(1) {
set_No_of_Rows_ Cols();
matrix_the_file ();
}

read_data::~rea d_data() {}

void read_data::matr ix_the_file(){
// allocate memory for the matrix
// needs to be freed later using
// void gsl_matrix_free (gsl_matrix * m)
m = gsl_matrix_allo c (nRows, nCol);
FILE * f = fopen(file_name .c_str(), "rb");
gsl_matrix_fsca nf (f, m);
fclose(f);

}

void read_data::set_ No_of_Rows_Cols () {
ifstream in(file_name.c_ str());
string line;
getline(in, line);
stringstream input( line.c_str() );

string word;
while(input >word)
nCol++; // init'd by constructor

while (getline(in, line))
nRows++; // init'd by constructor
}

/*
std::string command = "wc -l";
std::system( ( command + " " + file_name ).c_str() );
}
*/

*************** * end read_data.cpp *************** *
*************** * start read_data_test. cpp *************** *
#include <fstream>
#include <iostream>
#include <string>
#include "read_data. h"

using namespace std;

int main() {
string f = "../../data/ZB/Jun06/20060405";
read_data data1( f ); // space delimited
}
*************** * end read_data_test. cpp *************** *
*************** * start makefile *************** *
OBJS := $(patsubst %.cpp,%.o,$(wil dcard *.cpp))
COMP = g++

proj: $(OBJS)
$(COMP) -Wall -o proj $(OBJS) -lgsl

#-Wall turns on all warnings
*************** * end makefile *************** *
Jul 22 '06 #4
Gary Wessle wrote:
you did not explicitly mention whether I can place
a_function_from _somelib in the public scope of a
a_class_that_i_ provide but I take it that you implied it. if so, then
why I getting this error which appear to be a linker error?

*************** * start error *************** *
fred@debian:~/myPrograms/common$ make
g++ -c -o read_data.o read_data.cpp
g++ -c -o read_data_test. o read_data_test. cpp
g++ -Wall -o proj read_data.o read_data_test. o -lgsl
/usr/lib/gcc/i486-linux-gnu/4.0.4/../../../../lib/libgsl.so: undefined
reference to `cblas_dsdot'
/usr/lib/gcc/i486-linux-gnu/4.0.4/../../../../lib/libgsl.so: undefined
reference to `cblas_dswap'
... a long list of them
*************** * end error *************** *
[snip]

It is a linker error, and it appears that you are not linking all the
libraries you need to build this program. However, this is not a C++
*language* question (the topic of this forum). You should ask on
newsgroup that deals with your development platform or the library in
question. See this FAQ for what is on-topic here and for some ideas of
where else you could ask:

http://parashift.com/c++-faq-lite/ho...t.html#faq-5.9

Cheers! --M

Jul 22 '06 #5
Gary Wessle wrote:
>
thank you

you did not explicitly mention whether I can place
a_function_from _somelib in the public scope of a
a_class_that_i_ provide but I take it that you implied it. if so, then
why I getting this error which appear to be a linker error?

*************** * start error *************** *
fred@debian:~/myPrograms/common$ make
g++ -c -o read_data.o read_data.cpp
g++ -c -o read_data_test. o read_data_test. cpp
g++ -Wall -o proj read_data.o read_data_test. o -lgsl
/usr/lib/gcc/i486-linux-gnu/4.0.4/../../../../lib/libgsl.so: undefined
reference to `cblas_dsdot'
/usr/lib/gcc/i486-linux-gnu/4.0.4/../../../../lib/libgsl.so: undefined
reference to `cblas_dswap'
... a long list of them
*************** * end error *************** *
I don't use g++, so I don't know the command line syntax. I also don't
use the gsl library, so I don't know how it's structured. What I am
going to say next is, therefore, a guess.

cblas_dsdot, cblas_dswap, and the rest of the things on that list are
used from the library gsl, but they aren't actually in gsl. They are in
another library (or an object file). You need to find that library (or
object file) and tell your compiler about it. Once you've done that, you
may have new link errors that send you in search of more libraries files.

There's an excellent chance that the documentation or the faq for the
gsl library will tell you which other libraries you must link to.

Jul 22 '06 #6
mlimber wrote:
Gary Wessle wrote:
It is a linker error, and it appears that you are not linking all the
libraries you need to build this program. However, this is not a C++
*language* question (the topic of this forum). You should ask on
newsgroup that deals with your development platform or the library in
question. See this FAQ for what is on-topic here and for some ideas of
where else you could ask:
He's struggling through the standard C++ interface to a library:
includes and links. It *is* a c++ language issue.

The answer is the same for every library/compiler combination in the
known universe: "The linker is missing library files. RTF compiler M to
learn what library files are and how to link them, then RTF library M to
learn which files you need for this library." Even if he were to find a
group dedicated to using this library with this compiler, the answer is
still "RTFMs."
Jul 22 '06 #7
Howard Gardner <hg******@rawbw .comwrites:
Gary Wessle wrote:
thank you
you did not explicitly mention whether I can place
a_function_from _somelib in the public scope of a
a_class_that_i_ provide but I take it that you implied it. if so, then
why I getting this error which appear to be a linker error?
*************** * start error *************** *
fred@debian:~/myPrograms/common$ make
g++ -c -o read_data.o read_data.cpp
g++ -c -o read_data_test. o read_data_test. cpp
g++ -Wall -o proj read_data.o read_data_test. o -lgsl
/usr/lib/gcc/i486-linux-gnu/4.0.4/../../../../lib/libgsl.so: undefined
reference to `cblas_dsdot'
/usr/lib/gcc/i486-linux-gnu/4.0.4/../../../../lib/libgsl.so:
undefined
reference to `cblas_dswap'
... a long list of them
*************** * end error *************** *

I don't use g++, so I don't know the command line syntax. I also don't
use the gsl library, so I don't know how it's structured. What I am
going to say next is, therefore, a guess.

cblas_dsdot, cblas_dswap, and the rest of the things on that list are
used from the library gsl, but they aren't actually in gsl. They are
in another library (or an object file). You need to find that library
(or object file) and tell your compiler about it. Once you've done
that, you may have new link errors that send you in search of more
libraries files.

There's an excellent chance that the documentation or the faq for the
gsl library will tell you which other libraries you must link to.
indeed, you are right, after I did RTM, I fixed my makefile, now it
looks like this
*************** *************** *************** *************** ****
OBJS := $(patsubst %.cpp,%.o,$(wil dcard *.cpp))
COMP = g++

#### compiler section ####
proj: $(OBJS)
$(COMP) -Wall -I/usr/include -c $(OBJS)
#-Wall turns on all warnings

#### linker section ####
proj: $(OBJS)
$(COMP) -L/usr/local/lib $(OBJS) -lgsl -lgslcblas -lm

clean:
rm -rf *.o a.out
*************** *************** *************** *************** ****
may what a lesson I will NEVER forget, the problem was that even that
I was RTM, I did not understand all what I was reading.

now I have a little problem with the following warning that I am
working on. help is welcome.

:~/myPrograms/common$ make
makefile:13: warning: overriding commands for target `proj'
makefile:6: warning: ignoring old commands for target `proj'
g++ -c -o read_data.o read_data.cpp
g++ -L/usr/local/lib read_data.o read_data_test. o -lgsl -lgslcblas -lm
Jul 22 '06 #8

"Victor Bazarov" <v.********@com Acast.netwrote in message
news:2o******** *************** *******@comcast .com...
Gary Wessle wrote:
>is it right to have a line like
#include <path/to/header.hfor a library on my system, in my header
file and use some functions provided by this library in the
implementati on file (file.cpp) inside a class with out declaring those
functions in the class declaration in the header file?

If it works for you, what is your conccern?
Sounds like he's asking for advice as to best
practices but he doesn't really give enough
information. For example, whether he's using
functions in the library or objects...
Jul 23 '06 #9
Gary Wessle wrote:
indeed, you are right, after I did RTM, I fixed my makefile, now it
looks like this
*************** *************** *************** *************** ****
OBJS := $(patsubst %.cpp,%.o,$(wil dcard *.cpp))
COMP = g++

#### compiler section ####
proj: $(OBJS)
$(COMP) -Wall -I/usr/include -c $(OBJS)
#-Wall turns on all warnings

#### linker section ####
proj: $(OBJS)
$(COMP) -L/usr/local/lib $(OBJS) -lgsl -lgslcblas -lm

clean:
rm -rf *.o a.out
*************** *************** *************** *************** ****
may what a lesson I will NEVER forget, the problem was that even that
I was RTM, I did not understand all what I was reading.

now I have a little problem with the following warning that I am
working on. help is welcome.

:~/myPrograms/common$ make
makefile:13: warning: overriding commands for target `proj'
makefile:6: warning: ignoring old commands for target `proj'
g++ -c -o read_data.o read_data.cpp
g++ -L/usr/local/lib read_data.o read_data_test. o -lgsl -lgslcblas -lm
You've RTMed; now RTFAQ to see what this group is about:
<http://parashift.com/c++-faq-lite/how-to-post.html#faq-5.9>. You'll
find that makefile questions are also off-topic here because they
pertain only to a particular build environment, but I'll give you a
hint: you have two "proj:"'s in your makefile. You'll want to ask in,
say, a UNIX development newsgroup if you need further help on this
issue.

Cheers! --M

Jul 23 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
2774
by: Jim Mesara | last post by:
I have the following file structure: / header.asp test.asp /subDir testpage.asp /images I would like to have header.asp to be usable from all directories.
11
1942
by: cppaddict | last post by:
Say that your CustomClass.h header files requires #include <string> Now say that your CustomClass.cpp file also requires string. Is it good form to repeat the <string> include to make the dependency explicit, or do just allow the include to be make implicitly through the .h include? That is, should the header of your .cpp file be: #include "CustomClass.h"
3
1785
by: Miguel | last post by:
I have a header file that I want to share among various files, and I use: #ifndef UTIL_H_ #define UTIL_H_ file contents #endif
8
3050
by: nrhayyal | last post by:
Hi c++ Gurus, Need your blessing. while testing few aspects with respect to header file inclusions, i observed few things which i would like to share with you. i have a file sqlca.h in which a structure sqlca is declared. i included this file as a soft link in /usr/include. the soft link is as follows: sqlca.h -> /usr/opt/db2_08_01/include64/sqlca.h
6
1616
by: Al-Burak | last post by:
I have a class which only purpose is to provide services to a variety of classes in other files. The 'manipulator' class is aware of the other classes only because the header files have been include in its header file. However, there are times when some of the other classes are not and will not be dealt with, thus the need to include the header files does not arrive. To handle this, I have used compiler preprocessors to prevent the...
4
6956
by: 'Mani | last post by:
Hi, This is just a generic question, where i want to know what is the difference in including a header file in a .h file and .cpp file. I have a class called MyClass (MyClass.h & MyClass.cpp). There is another class (OtherClass.h & OtherClass.cpp) OtherClass.cpp has a forward declaration to a class called 'Calc' which is in the namespace called 'Utils' like below:
1
1756
by: Martin Mücke | last post by:
I got a website consisting of about 150 php pages. The site uses a frameless table based design. Header and menu are always the same and therefore should be extracted. At the moment I got a "frame" (not a real html frame, more like "framework"), that contains my menu and header content. In this php page I dynamically include all content pages. Downsides: All pages got the same meta tags. Now I am looking for a better solution. I...
8
4594
by: nguillot | last post by:
Hello. If I have the following classes: class B {}; typedef B tB; if A is: class A
3
2574
by: KIRAN | last post by:
Hello all, My question is about the way of including header files(*.h) in source files (*.c) I have three folders, -build ( for project makefiles) -include ( for *.h files) -src (for *.c files). I know that there are two ways of specifying include path of header files
0
9589
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
1
9997
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9865
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6675
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5309
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5448
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3965
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3565
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2815
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.