我正在尝试使用源文件创建一个库,然后在我的程序中使用这个库。但是链接器抛出关于 vtable 的错误:
以下是代码:
product.h
--------------------------------------------------------------
# ifndef PRODUCT_H_
#define PRODUCT_H_
# include <iostream>
# include <string>
using namespace std ;
class Product {
public:
virtual ~Product () {}
virtual string GetProductCode () = 0 ;
} ;
# endif
newproduct.h
--------------------------------------------------------------
# ifndef NEWPRODUCT_H_
#define NEWPRODUCT_H_
# include "product.h"
# include <string>
using namespace std ;
class NewProduct : public Product {
public:
NewProduct () {cout<<"Creating New product"<<endl;}
virtual string GetProductCode () ;
} ;
# endif
newproduct.cc
--------------------------------------------------------------
# include "newproduct.h"
string NewProduct::GetProductCode () {
return "New Product" ;
}
main.cc
--------------------------------------------------------------
# include "product.h"
# include "newproduct.h"
# include <iostream>
using namespace std;
int main ()
{
Product * prod = new NewProduct ();
prod->GetProductCode () ;
delete prod ;
return 0;
}
我正在尝试执行以下步骤:
1) 导出 LD_LIBRARY_PATH=$LD_LIBRARY_PATH:。
2) g++ -o libprodlib.so newproduct.o -shared
3) g++ -o 演示 main.cc -L lprodlib.so
但这给了我错误:
/tmp/ccqI60q9.o: In function `NewProduct::NewProduct()':
main.cc:(.text._ZN10NewProductC2Ev[_ZN10NewProductC5Ev]+0x17): undefined reference to `vtable for NewProduct'
collect2: ld returned 1 exit status
你能建议上面出了什么问题吗?
谢谢