C++ 编译:包括库:对“vtable for class”的未定义引用`

C++ 编译:包括库:对“vtable for class”的未定义引用`,c++,compilation,dynamic-library,C++,Compilation,Dynamic Library,我正在尝试使用源文件创建一个库,然后在我的程序中使用这个库。但是链接器抛出关于vtable的错误: 代码如下: product.h -------------------------------------------------------------- # ifndef PRODUCT_H_ #define PRODUCT_H_ # include <iostream> # include <string> using namespace std ; class

我正在尝试使用源文件创建一个库,然后在我的程序中使用这个库。但是链接器抛出关于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;
}
你能告诉我上面出了什么问题吗


感谢创建共享的.so文件,您可以这样做:

$g++ -shared -fPIC newproduct.cc -o libprodlib.so
$g++ main.cc -o daemon -L ./ -lprodlib
或者更简单:

$g++ -c newproduct.cc -o newproduct.o
$g++ main.cc -o daemon newproduct.o

非常感谢,但是,我正在努力理解如何创建动态库,然后在我的程序中使用它们。我明白了。/有什么不同。我已将“.”(当前目录)添加到LD_LIBRARY_路径。那不行吗?你的`-L lprodlib.so`没有意义-L后面需要跟path,-lprodlib,而不是lib.so。顺便说一句,您的
export LD\u LIBRARY\u PATH=$LD\u LIBRARY\u PATH:。
是正确的。
$g++ -c newproduct.cc -o newproduct.o
$g++ main.cc -o daemon newproduct.o