C++ |DLL / EXE - 如何从导出的类调用另一个类方法?
C++ | DLL / EXE - How to call another class method from exported class?
我有一个项目,我想在其中使用 DLL。
我正在将工厂函数导出到我的 exe 中:
extern "C" __declspec(dllexport)
BaseInit* __cdecl CreateInterface( void )
{
return new Initializer;
}
这非常有效。在我的 Init 类中,我有一个方法可以创建另一个类,我想从我的初始值设定项类方便地使用它:
class IAnotherClass {
public:
virtual void TestFunction();
...
class AnotherClass : public IAnotherClass {
public:
void TestFunction();
...
class Initializer : public BaseInit
{
IAnotherClass* Create(void)
{
return new AnotherClass;
}
...
这似乎也有效。我得到一个非空指针。但是,当尝试从此类(在我的exe程序中(调用TestFunction时,我得到:
LNK2001未解析的外部符号"公共:虚拟虚空__cdecl OtherClass::TestFunction(void(" (?TestFunction@AnotherClass@@UEAAXXZ(
void AnotherClass::TestFunction
-body 在我的 DLL 项目中位于单独的 .cpp -文件中
我是否做错了,我实际上需要为每个不同的类实例提供单独的工厂函数?甚至有可能这样做吗?
您需要将__declspec(dllexport)
添加到您希望在 dll 之外可用的每个类和函数,只要导出包含类,就不需要标记方法。
请注意,在类中,declspec 介于class
和类名之间:
class __declspec(dllexport) Exported
{
};
您还需要定义一个宏,该宏根据您构建的是 dll 还是 exe,在__declspec(dllexport)
和__declspec(dllimport)
之间切换标头,例如:
#ifdef BUILDING_MYDLL
#define MYDLL_EXPORT __declspec(dllexport)
#else
#define MYDLL_EXPORT __declspec(dllimport)
#endif
class MYDLL_EXPORT Exported
{
};
相关文章:
- C++ |DLL / EXE - 如何从导出的类调用另一个类方法?
- 在模板化的 Rcpp 函数中调用另一个 cpp 函数
- 编译器在C++中调用另一个函数时,在参数中查找已删除的构造函数
- 编译问题 C++ 同时,尝试通过调用另一个对象中的成员函数来创建 std:: 线程
- 如何修改类及其方法,让我调用另一个类的必要方法?
- 通过引用常量函数调用另一个类的非常量函数
- 我如何使用 GTK3 在 C 中调用另一个"window"
- 通过智能指针调用另一个类的成员函数
- 显示 matlab 程序的输出,调用另一个 C++ 程序
- 类设计以调用另一个类的成员函数
- 在 android ndk 中的同一.cpp文件上调用另一个 C++ 函数
- 如何从 c++ 调用另一个文件中定义的 python 函数?
- 如何使数组调用另一个类中的函数
- 修改类中调用另一个类函数的变量
- 如何让一个C++模板类调用另一个类的方法?
- 如果不同的线程调用另一个不同线程的相同信号,则需要静音
- 如何在Wait()C++期间同时调用另一个函数
- C++调用另一个类构造函数
- 从另一类调用另一个类的函数
- 函数调用另一个函数会给出错误的输出 C++