include the cxx file at the end of header file

Y

Yi Gao

Hi All,

Seems that adding a
#include "foo.cxx"
line at the end of the header file foo.h
makes life easier since we don't have to compile the foo library and
link it into the code using it.

But before I change all my previous code, could I know if doing that
is safe?

In detail, assume we have a main program in which the foo function is
called:
---------------------------------------------------------------
Way1:
in a.cxx:
#include "foo.h"
int main()
{ foo(); return 0;}

In foo.h:
void foo();

in foo.cxx
#include "foo.h"
voif foo() { // blah blah blah }
---------------------------------------------------------------

To compile that, I need to compile the foo library first and compile
a.cxx and link them.

Because I'm lazy, so I tried the Way2:

---------------------------------------------------------------
Way2, only add an include at the end of foo.h file

a.cxx is the same
foo.cxx is the same

in foo.h:
void foo();
#include "foo.cxx"
---------------------------------------------------------------

If doing this, then I just need to g++ a.cxx at a single shot.

This seems to be convenient, but is there any potential danger in
doing this vs Way1?


Thanks!
Yi
 
V

Victor Bazarov

Yi said:
Hi All,

Seems that adding a
#include "foo.cxx"
line at the end of the header file foo.h
makes life easier since we don't have to compile the foo library and
link it into the code using it.

But before I change all my previous code, could I know if doing that
is safe?

What meaning do you attach to 'safe'? In what way do you think it can
be "unsafe"?
In detail, assume we have a main program in which the foo function is
called:
---------------------------------------------------------------
Way1:
in a.cxx:
#include "foo.h"
int main()
{ foo(); return 0;}

In foo.h:
void foo();

in foo.cxx
#include "foo.h"
voif foo() { // blah blah blah }
---------------------------------------------------------------

To compile that, I need to compile the foo library first and compile
a.cxx and link them.

Because I'm lazy, so I tried the Way2:

---------------------------------------------------------------
Way2, only add an include at the end of foo.h file

a.cxx is the same
foo.cxx is the same

in foo.h:
void foo();
#include "foo.cxx"
---------------------------------------------------------------

If doing this, then I just need to g++ a.cxx at a single shot.

This seems to be convenient, but is there any potential danger in
doing this vs Way1?

Not if you want to keep everything in one [huge] object file. For small
utilities there is no problem. As soon as you start/join a project in
which there are several parts, you will find yourself wanting to split
your development into components... Read about this in John Lakos'
"Large-Scale C++ Software Design".

The ability to compile parts separately and link them later is there for
a reason. You don't have to do it if the reason does not apply to you.

V
 
N

Noah Roberts

But before I change all my previous code, could I know if doing that
is safe?

In detail, assume we have a main program in which the foo function is
called:
---------------------------------------------------------------
Way1:
in a.cxx:
#include "foo.h"
int main()
{ foo(); return 0;}

In foo.h:
void foo();

in foo.cxx
#include "foo.h"
voif foo() { // blah blah blah }
---------------------------------------------------------------

To compile that, I need to compile the foo library first and compile
a.cxx and link them.

Because I'm lazy, so I tried the Way2:

---------------------------------------------------------------
Way2, only add an include at the end of foo.h file

a.cxx is the same
foo.cxx is the same

in foo.h:
void foo();
#include "foo.cxx"
---------------------------------------------------------------

If doing this, then I just need to g++ a.cxx at a single shot.

This seems to be convenient, but is there any potential danger in
doing this vs Way1?

Yep. To illustrate let us create b.cxx:
 
J

Juha Nieminen

Yi said:
Seems that adding a
#include "foo.cxx"
line at the end of the header file foo.h
makes life easier since we don't have to compile the foo library and
link it into the code using it.

I don't understand how that makes life significantly easier.

If you have, for example "main.cxx" and "foo.cxx", you simply compile
and link both (or add both to the project if you are using some
graphical IDE) and that's it. What's the problem?

If you get into the habit of doing what you propose, you are only
going to encounter problems, as well as making your compile times longer.
But before I change all my previous code, could I know if doing that
is safe?

Certainly not. Definitions (in other words, function implementations
and things like static members) must only appear once in the program. If
they appear in more than one compilation unit, you will get a linker error.

Moreover, in certain situations you won't get a linker error, but you
will be duplicating code and variables (eg. ones in nameless
namespaces), which is usually not what you want.
 
Y

Yi Gao

Thanks! this is a great example.
btw if foo are templated in foo.h, then this b.cxx won't cause such
problem. This is nice
 

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

No members online now.

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,810
Latest member
Kassie0918

Latest Threads

Top