Compiling and Linking help

Aseem Rastogi
Tue Nov 8 08:43:00 GMT 2005

to keep code clean and simple, generally function implementations are 
not written in .h files. they should only contain declarations and then 
.cpp files should have implementations which can then be linked.

btw, making a () function inline could help. but it should be avoided 
keeping in mind above.


Djekic Dusan wrote:

>I have the the problem related to compiling and linking the following:
>int a( ) { };
>#include a.h
>class B { B( ); };
>#include "b.h"
>B::B( ) { a( ); }
>class C { C( ); };
>#include "c.h"
>C::C( ) { }
>#include "b.h"
>#include "c.h"
>B b;
>C c;
>compiling:  g++ -c b.cpp
>                 g++ -c c.cpp
>                 g++ -c main.cpp
>linking:       g++ -o out b.o c.o main.o
>Here I get no compilation errors, but do get linking errors stating
>multiple definitions of everything regarding a.h. I found out this is
>the product of double compiling everything regarding   a.h in b.o and
>main.o, but could not find the way how to write proper and simple
>Makefile, but to have object files for b, c and main, and afterwards
>linked together. Could you help me with this?

The end is always good. If it's not good, it's not the end.

More information about the Gcc-help mailing list