I have trapped in "undefined reference to vtable..."
for a whole day.
Actually, I have seen many answer deal with "undefined reference to vtable..."
For example:
undefined reference to vtable "Transaction"
https://gcc.gnu.org/faq.html#vtables
Some people's problem is forget to write virtual function, others are forget to add the .cpp file into build directory. But I think I have notice that.
I want to do the follow step:
- I want compile class
A_1
andA_2
to a shared librarylibA
.A_2
is derived fromA_1
.
A_1.h
#ifndef include_A_1_h
#define include_A_1_h
class A_1
{
public:
A_1() {}
virtual ~A_1() {} // not pure-virtual function has defined
virtual void print() = 0;
private:
};
#endif
A_2.h
#ifndef include_A_2_h
#define include_A_2_h
#include "A_1.h"
class A_2 : public A_1
{
public:
A_2() {}
~A_2() {}
virtual void print();
private:
};
#endif
A_2.cpp
#include "A_2.h"
void A_2::print()
{
// empty
}
- I want compile class
B_1
andB_2
to a shared librarylibB
.B_1
andB_2
is independent.
B_1.h
#ifndef include_B_1_h
#define include_B_1_h
#include <iostream>
#include <string.h>
class B_1
{
public:
B_1(const std::string &path);
~B_1();
private:
};
#endif
B_1.cpp
#include "B_1.h"
B_1::B_1(const std::string &path)
{
}
B_1::~B_1()
{
}
B_2.h
#ifndef include_B_2_h
#define include_B_2_h
class B_2
{
public:
B_2() {}
~B_2() {}
void fun();
private:
};
#endif
B_2.cpp
#include "B_2.h"
#include "A_1.h"
#include "A_2.h"
void B_2::fun()
{
A_1 *ptr = new A_2;
}
- I want to using two shared library in
main.cpp
. At here, some thing goes wrong.
main.cpp
#include "B_1.h"
int main()
{
B_1 b1("name");
}
I am using the follow command to compile:
g++ A_2.cpp -fPIC -shared -o libA.so
g++ B_1.cpp B_2.cpp -fPIC -shared -o libB.so
g++ main.cpp -L . -lA -lB
The compiler said that:
./libB.so: undefined reference to `vtable for A_2'
You can see many empty functions, because I ignore some irrelevant code. But in this case, It still have the error.
Could any one help me? Thanks.