我有一个应用程序代码,它调用具有显式链接(或运行时链接)的 DLL 库来访问导出的类。
DLL.h
#ifdef DLL_EXPORT
#define DLL_API __declspec(dllexport)
#else
#define DLL_API __declspec(dllimport)
#endif
FooDLL.h
#include "DLL.h"
class DLL_API Foo
{
public:
void doSomeThing();
};
extern "C" DLL_API Foo* _getInstance() {
return new Foo();
}
typedef Foo* (*getInstanceFactory)();
Foo* getInstance() {
HINSTANCE dllHandle = LoadLibraryA("Foo.dll");
getInstanceFactory factory_func = (getInstanceFactory)GetProcAddress(dllHandle, "_getInstance");
return factory_func();
}
FooDLL.cpp
#include "FooDLL.h"
Foo::doSomething() {
// .......
}
Application.cpp(调用 DLL)
#include "FooDLL.h"
Foo* obj = getInstance();
obj->doSomething(); // XXX this line can be compiled and linked only when DLL is already in path
只有当 DLL 文件包含在 lib 路径中时,才能构建(例如编译和链接)上述代码。否则我得到未解决的外部符号错误。
error LNK2001: unresolved external symbol "__declspec(dllimport) public: void __thiscall Foo::doSomething()" .....
是否可以在构建期间仅使用 DLL 头文件(即 FooDLL.h)而不使用 DLL/LIB 文件来构建应用程序代码?(ps 类实现必须在 cpp 文件中。)
谢谢!