Re: Compiling and Linking help

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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.

regards,
aseem.

Djekic Dusan wrote:

Hello!
I have the the problem related to compiling and linking the following:

a.h
int a( ) { };

b.h
#include a.h
class B { B( ); };

b.cpp
#include "b.h"
B::B( ) { a( ); }

c.h
class C { C( ); };

c.cpp
#include "c.h"
C::C( ) { }

main.cpp
#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.




[Index of Archives]     [Linux C Programming]     [Linux Kernel]     [eCos]     [Fedora Development]     [Fedora Announce]     [Autoconf]     [The DWARVES Debugging Tools]     [Yosemite Campsites]     [Yosemite News]     [Linux GCC]

  Powered by Linux