g++: compilation of header file requested 
July 23rd, 2005, 05:50 AM
| | | g++: compilation of header file requested
Hi, here is an example I copied from Deitel C++ book. but when I
compile it, always get the above compilation error, no matter how I
change the include order, please help.
here is the files:
Note: this is to practice Proxy classes.
// Implementation.h
class Implementation {
public:
Implementation(int v) { this->v = v; }
void setValue(int v)
{
this->v = v;
}
int getValue() const
{
return v;
}
private:
int v;
};
// Interface.h
class Implementation;
class Interface {
public:
Interface(int);
~Interface();
void setValue(int);
int getValue() const;
private:
Implementation *ptr;
};
/* Interface.cpp */
#include "Implementation.h"
#include "Interface.h"
Interface::Interface(int v):ptr(new Implementation(v))
{ }
Interface::~Interface() { delete ptr; }
void Interface::setValue(int v){
ptr->setValue(v);
}
int Interface::getValue() const {
return ptr->getValue();
}
/* t.cpp */
#include <iostream>
using namespace std;
#include "Interface.h"
#include "Implementation.h"
int main(){
Interface i(5);
cout << i.getValue() << endl;
i.setValue(19);
cout << i.getValue() << endl;
return 0;
}
# ============ this is Makefile
# Makefile
a.out: Implementation.h Interface.h Interface.cpp t.cpp
g++ Implementation.h Interface.cpp t.cpp
clean:
rm -f a.out
cleanall:
rm -f *.h *.cpp a.out | 
July 23rd, 2005, 05:50 AM
| | | Re: g++: compilation of header file requested
blueblueblue2005 wrote:[color=blue]
> Hi, here is an example I copied from Deitel C++ book. but when I
> compile it, always get the above compilation error, no matter how I
> change the include order, please help.[/color]
Well, you might want to read the error message: it clearly says
that the compiler complains about the request that it should
compile a header file.
[color=blue]
> # ============ this is Makefile
> # Makefile
> a.out: Implementation.h Interface.h Interface.cpp t.cpp
> g++ Implementation.h Interface.cpp t.cpp[/color]
The above line should read
g++ Interface.cpp t.cpp
instead.
--
<mailto:dietmar_kuehl@yahoo.com> <http://www.dietmar-kuehl.de/>
<http://www.eai-systems.com> - Efficient Artificial Intelligence | 
July 23rd, 2005, 05:50 AM
| | | Re: g++: compilation of header file requested
Hi,
May be you need to change your makefile. At the same time I dont
understand why r u defining the body of the functions within header
files like u did in Implementation.h. As far as my knowledge goes ".h"
files are for declaring function prototypes and variables only. We dont
write the body of the function within a header file. That might be the
reason why r u getting this compilation error.
blueblueblue2005 wrote:[color=blue]
> Hi, here is an example I copied from Deitel C++ book. but when I
> compile it, always get the above compilation error, no matter how I
> change the include order, please help.
>
> here is the files:
> Note: this is to practice Proxy classes.
>
> // Implementation.h
>
> class Implementation {
> public:
> Implementation(int v) { this->v = v; }
> void setValue(int v)
> {
> this->v = v;
> }
>
> int getValue() const
> {
> return v;
> }
>
> private:
> int v;
> };
>
> // Interface.h
>
> class Implementation;
>
> class Interface {
> public:
> Interface(int);
> ~Interface();
> void setValue(int);
> int getValue() const;
>
> private:
> Implementation *ptr;
> };
>
> /* Interface.cpp */
>
> #include "Implementation.h"
> #include "Interface.h"
>
> Interface::Interface(int v):ptr(new Implementation(v))
> { }
>
> Interface::~Interface() { delete ptr; }
>
> void Interface::setValue(int v){
> ptr->setValue(v);
> }
>
> int Interface::getValue() const {
> return ptr->getValue();
> }
>
> /* t.cpp */
>
> #include <iostream>
>
> using namespace std;
>
> #include "Interface.h"
> #include "Implementation.h"
>
> int main(){
> Interface i(5);
>
> cout << i.getValue() << endl;
> i.setValue(19);
> cout << i.getValue() << endl;
> return 0;
> }
>
> # ============ this is Makefile
> # Makefile
> a.out: Implementation.h Interface.h Interface.cpp t.cpp
> g++ Implementation.h Interface.cpp t.cpp
> clean:
> rm -f a.out
> cleanall:
> rm -f *.h *.cpp a.out[/color] | 
July 23rd, 2005, 05:50 AM
| | | Re: g++: compilation of header file requested
blueblueblue2005 wrote:[color=blue]
> Hi, here is an example I copied from Deitel C++ book. but when I
> compile it, always get the above compilation error[/color]
[snip]
What error? There in no error included in the body of the message. Make
sure to include your problem in the *body* of the message not only in
the subject line.
/Dan | 
July 23rd, 2005, 05:50 AM
| | | Re: g++: compilation of header file requested
blueblueblue2005 wrote:[color=blue]
> Hi, here is an example I copied from Deitel C++ book. but when I
> compile it, always get the above compilation error[/color]
[snip]
What error? There is no error included in the body of the message. Make
sure to include your problem in the *body* of the message not only in
the subject line.
/Dan | 
July 23rd, 2005, 05:50 AM
| | | Re: g++: compilation of header file requested
you are right, g++ Interface.cpp t.cpp works. but why when I include
Implementation.h in g++ command, it complains that it should compile a
header file?? | 
July 23rd, 2005, 05:50 AM
| | | Re: g++: compilation of header file requested
the reason I define the body of functions within header file is the
body is too small, as you see, just one line. and the reason I got
compilation of header file error is my Makefile.
Dietmar's reply indicated the error, I just dont know why I can't
include Implementation.h in g++ command. | 
July 23rd, 2005, 05:50 AM
| | | Re: g++: compilation of header file requested
blueblueblue2005 wrote:[color=blue]
> you are right, g++ Interface.cpp t.cpp works. but why when I include
> Implementation.h in g++ command, it complains that it should compile a
> header file??
>[/color]
You are not supposed to send header files to the compiler. Header files
are generally used to only define an interface to an implementation
file, object file, or library.
It is common to implement small functions and declare them 'inline' in
header files for convenience, or to declare bodies of small class member
functions so they don't get lost among much larger functions declared in
the counterpart cpp files.
Just send 'cpp' and 'o' files to the compiler, it will find the headers
through your #include directives.
--Paul | 
July 23rd, 2005, 05:50 AM
| | | Re: g++: compilation of header file requested
[color=blue]
> Make sure to include your problem in the *body* of the message not only in
> the subject line.[/color]
Ok, :-) | 
July 23rd, 2005, 05:50 AM
| | | Re: g++: compilation of header file requested
Hello,
On Mon, 2005-07-04 at 11:43 -0700, ambar.shome@gmail.com wrote:[color=blue]
> As far as my knowledge goes ".h" files are for declaring function
> prototypes and variables only. We dont write the body of the function
> within a header file. That might be the reason why r u getting this
> compilation error.[/color]
As far as my knowledge goes, this is being done on a pretty common base.
For example, the Spirit parser ( http://spirit.sf.net/ ) is written
entirely in header files.
I know that header files are supposed to be for defining functions, but
isn't this rather for /other/ classes that include this class ? I mean,
as I see it, the strict separation between header files and source files
is because that way a compiler can look at a header, know what functions
a certain class accepts, and then parse one class accordingly... you do
not need a pure definition of the class YET... but what's so much
against it, if it's possible ? Sure, if a class includes a class that
tries to access member functions of the class including it (sheesh, what
a sentence :)), sure, you need separation between header and source so
it can be solved at compile-time... but why do you need to do this all
the time, especially for the simple functions the parent post is using ?
Regards,
Leon Mergen | 
July 23rd, 2005, 05:50 AM
| | | Re: g++: compilation of header file requested
Leon Mergen wrote:[color=blue]
> Hello,
>
> On Mon, 2005-07-04 at 11:43 -0700, ambar.shome@gmail.com wrote:
>[color=green]
>>As far as my knowledge goes ".h" files are for declaring function
>> prototypes and variables only. We dont write the body of the function
>> within a header file. That might be the reason why r u getting this
>> compilation error.[/color]
>
>
> As far as my knowledge goes, this is being done on a pretty common base.
> For example, the Spirit parser ( http://spirit.sf.net/ ) is written
> entirely in header files.[/color]
The Spirit parser is implemented using template meta-programming
techniques, which is a different beast altogether from
header/implementation separation.
The STL is implemented using "header" files, they just contain template
definitions and don't have the .h extension. They also don't need to be
explicitly sent to the compiler (but referenced via #include instead)
because of the way templates work during compile time.
[color=blue]
>
> I know that header files are supposed to be for defining functions, but
> isn't this rather for /other/ classes that include this class ? I mean,
> as I see it, the strict separation between header files and source files
> is because that way a compiler can look at a header, know what functions
> a certain class accepts, and then parse one class accordingly... you do
> not need a pure definition of the class YET... but what's so much
> against it, if it's possible ? Sure, if a class includes a class that
> tries to access member functions of the class including it (sheesh, what
> a sentence :)), sure, you need separation between header and source so
> it can be solved at compile-time... but why do you need to do this all
> the time, especially for the simple functions the parent post is using ?
>
> Regards,
>
> Leon Mergen
>[/color] | 
July 23rd, 2005, 05:50 AM
| | | Re: g++: compilation of header file requested
blueblueblue2005 wrote:[color=blue]
> you are right, g++ Interface.cpp t.cpp works. but why when I include
> Implementation.h in g++ command, it complains that it should compile a
> header file??[/color]
The gcc/g++ determines the kind of file it is supposed to process
according to the extension (and possibly other indicators). It
knows about .c (C source files), .C, .cxx, .cpp, .cc, and a few
more (C++ source files), .o (object files, i.e. binary files
representing the compiled but not linked result of the compiler;
see the -c option for more details), .a (libraries, i.e. collections
of object files), .so (shared libraries, i.e. collections of object
files prepared for swift reference resolution), and a bunch of other
files. However, it does not know how to process .h, .hpp, .hxx, .H,
etc. because these only contain declaration and no definitions and
it thus does not make any sense to compile them (the result would
be empty and if it is not it is either useless or does even cause
problems).
Header files contain declarations which are shared between multiple
source files. It would be equivalent for the compiler to just
provide the declarations in each individual file. Of course, this
would be a maintenance nightmare and it is much more reasonable to
factor common declarations into a single separate file.
--
<mailto:dietmar_kuehl@yahoo.com> <http://www.dietmar-kuehl.de/>
<http://www.eai-systems.com> - Efficient Artificial Intelligence | 
July 23rd, 2005, 05:50 AM
| | | Re: g++: compilation of header file requested
Paul schreef:
[color=blue]
> The Spirit parser is implemented using template meta-programming
> techniques, which is a different beast altogether from
> header/implementation separation.[/color]
Actually, the main difference is that separate template implementations
require the use of 'export', which isn't very well supported (*cough*).
Hence, template funtions are often inlined, and like other inline
functions can be in headers.
[color=blue]
> The STL is implemented using "header" files, they just contain template
> definitions and don't have the .h extension. They also don't need to be
> explicitly sent to the compiler (but referenced via #include instead)
> because of the way templates work during compile time.[/color]
The STL is nowadays (since 98) part of the Standard Library, and
implemented via compiler magic. On some compilers, that magic means
"header files without .h" but this is not universal.
You're often right that they don't need to be sent to the compiler.
At least all compilers I know are smart enough to find them, but I
know some compilers can be configured to select different library
implementations.
HTH,
Michiel Salters | | Thread Tools | Search this Thread | | | |
Posting Rules
| You may not post new threads You may not post replies You may not post attachments You may not edit your posts HTML code is Off | | | | | | What is Bytes?
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 220,662 network members.
|