I'm having this strange linking problem for a few days now. I have a C++ project (in Ubuntu 16.04) with 2 namespaces. Each namespace has .h and .cpp files in a separate directory compiling into a library .a file. At the end everything is linked into a single executable.
The project is quite large (modification of OpenBTS) so to make it more easy this is basically what I'm trying to do:
//*****directory A:
//file: A.h
namespace1 {
class A {
public:
void functionA();
};
}
//file: A.cpp
#include <B.h>
#include <vector.h>
using namespace1;
using namespace2;
void A::functionA (){
B testB1;
B testB2;
testB2 = testB1; //works
testB1.functionB2(); //defined in B.h, works
testB1.functionB(); //undefined reference to 'namespace2::B::functionB() const'
std::vector<B> testvector;
testvector.push_back(testB1); //undefined reference to 'namespace2::B'
}
//
//******directory B:
//file: B.h
#include "C.h"
//class C was part of the project before I started editing it and should not be the problem because other classes derived from it compile without problem.
namespace 2{
class B: public C{
int varB;
public:
B(){};
~B(){};
void functionB() const; //overrides virtual functionB in class C
int functionB2() { return varB;}
void functionB2() //overrides pure virtual functionB2 in class C
};
}
//file B.cpp
#include "B.h"
using namespace2
void B::functionB(){
//code...
}
//main.cpp
//this creates an instance of A
At the end all files in directory A are compiled in .o files and then linked together in libary A.a, the same for directory B. Also main.cpp is compiled main.o
Then all is linked: g++ -g -O2 -Wall -pthread -rdynamic -o exename main.o ../B/.libs/B.a ../A/.libs/A.a -la53 -lzmq -pthread
This is where I get the errors:
undefined reference to 'namespace2::B' undefined reference to 'namespace2::B::functionB() const'
I've already checked if all virtual functions are overridden in B and this seems OK. Also when I use class B within other code in the namespace2 there is no problem and everything compiles fine. Calling a function defined in B.h is working so it rather seems like the linker cannot access functions defined in B.cpp?
Any suggestions?