views:

103

answers:

3

I have the following setup (hopefully this is not too bare an example):

A.h

typedef std::map<unsigned int, float> MyClass;
extern MyClass inst;

A.cpp

MyClass inst;

B.h

#include <A.h>
void foo();

B.cpp

#include <B.h>
void foo {
    inst.myClassFunc();
}

Now, when I use inst in B.cpp I get undefined reference to inst.

Any idea on how to fix this?

A: 

This is too bare an example to work out what's going on. However, based on the above it's entirely possible that when it hits the failing line the compiler has no knowledge of what's actually in MyClass and therefore cannot resolve MyClassFunc.

We would need to see the definition of MyClass and know where it is to answer for sure.

Steve Townsend
updated regarding `MyClass`
Amir Rachum
+1  A: 

You have to compile the above mentioned file A.cpp as

g++ -c A.cpp
g++ -c B.cpp

and then while creating the executable you should write the command as follows:

g++ A.o B.o
hype
Or in one step, `g++ A.cpp B.cpp`.
Mike Seymour
A: 

From the basic example code you posted I'd say you've forgotten to #include <B.h> in your B.cpp file.

Martin