[英]C++ shared libraries with inheritance
I have a library (a.so) with a base classes (MyClassA). 我有一个带有基类(MyClassA)的库(a.so)。 Another library (b.so) has a class MyClassB that inherits from MyClassA (in a.so). 另一个库(b.so)具有从MyClassA(在a.so中)继承的类MyClassB。 I compile MyClassA.h and MyClassA.cpp isolated in a.so. 我编译a.so中孤立的MyClassA.h和MyClassA.cpp。 MyClassB.h and MyClassB.cpp are compiled in isolation (with a reference to MyClassA.h but without adding MyClassA.h to b.so). MyClassB.h和MyClassB.cpp是隔离编译的(具有对MyClassA.h的引用,但未在b.so中添加MyClassA.h)。 I then link b.so to a.so. 然后,我将b.so链接到a.so。
To summarize: 总结一下:
When I try to compile, I get a number of reference errors to MyClassA, caused by b.so. 当我尝试编译时,由于b.so,我收到许多对MyClassA的引用错误。
When I compile b.so and add MyClass.h to it, the library compiles and runs without any errors. 当我编译b.so并将MyClass.h添加到它时,该库将编译并运行而没有任何错误。 Hence: 因此:
Is it possible in C++ to use my first option, or is it required to always include the base headers in subclass library? 在C ++中是否可以使用我的第一个选项,还是要求始终在子类库中包含基本标头?
If you derive ClassB
from ClassA
you should have your ClassA
defined , when deriving, not only declared ( referenced ). 如果从ClassA
派生ClassB
,则应在派生时定义 ClassA
,不仅要声明 ( 引用 )。 That is why you have to include ClassA
header file. 这就是为什么必须包含ClassA
头文件的原因。
But if you implemented ClassA
functions in cpp
file, not in header, actual code of ClassA
will be in a.so
, so, includeing ClassA
header file is not really a problem. 但是,如果你执行ClassA
函数cpp
文件,而不是在头,实际的代码 ClassA
将是a.so
,因此,包括, ClassA
头文件是不是一个真正的问题。
如果class B
是从class A
派生class A
,则必须包括class A
头文件。
All derived classes must #include
base class declarations in compilation time. 所有派生类必须在编译时#include
基类声明。 Base class implementation must be known in linking time. 在链接时必须知道基类的实现。
In your case: 在您的情况下:
g++ -o b.so -la g ++ -o b.so -la
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.