In an ongoing effort to wean myself off of visual studio, I'm compiling and linking with g++ 4.0.3 and sometimes, it gets just too arcane.
Here's the problem I've run into now.
Let's say there's a header file "a.h" with source "a.cc".
In "a.h" a class has been defined:
Then, there's "b.cc" which holds the main routine.
//some basic declarations
//definitions were placed in a.cc}A; //note the instantiation here
Here's how I run g++ (actually, I'm doing this with GNU make, but since the basics are the same, I guess it doesn't really matter).
int main(... yada yada
Doing this gives me the following error:
g++ -c a.cc -o a.o
g++ -c b.cc -o b.o
g++ -o a a.o b.o
I remembered vaguely that the order of the object files might matter so I switched "a.o" and "b.o" and it still didn't work.
a.o:(.bss+0x0): multiple definition of 'A'
b.o:(.bss+0x0): first defined here
The only way I can get it to work is by getting rid of the instantiation at the end of the class declaration like this:
What I don't understand is why I can't instantiate objects or declare variables in a header file if I want to compile them separately? The GNU g++ manual isn't clear on this, and I'm looking into Stroustrup's book, but his section on linking was pitifully small.
As always, help appreciated.