我的共享库 (*.dll) 中有一个函数声明,定义在另一个应用程序中,这是一个最小的代码。但是如何告诉编译器找到符号set_x
(在 mylib.cpp 中)在库加载到应用程序后的运行时。 extern 关键字在链接时搜索它。或者有没有其他方法可以调用 application.cpp 中定义的方法?
// mylib.cpp compiles to mylib.dll
class MyClass{
public:
extern void set_x(int); // need to tell the compiler to search at runtime
}
extern "C"{
EXPORT_API void lib_func(MyClass* o){
printf("setting o's x value");
o->set_x(42);
}
}
// application.cpp compiles to application.exe
class MyClass{
private:
int x = 0;
public:
extern void set_x(int _x){ x = _x; }
}
typedef void (*func_ptr)(MyClass*);
...
func_ptr lib_func = (lib_func_ptr)dlopen_and_get_func_ptr("mydll.lib", "lib_func");
MyClass cls;
lib_func(&cls);
...
最佳答案
问题是您混合了 C 和 C++ 概念。即使第二个是建立在第一个之上的,他们处理这种情况的方式也大不相同。我将讨论 C++ 的榕树。但是,无论选择哪种方法,代码中都有一些错误:
dll00.h:
#pragma once
#if defined(_WIN32)
# if defined(DLL00_EXPORTS)
# define DLL00_EXPORT_API __declspec(dllexport)
# else
# define DLL00_EXPORT_API __declspec(dllimport)
# endif
#else
# define DLL00_EXPORT_API
#endif
class DLL00_EXPORT_API BaseClass {
public:
virtual void setX(int x) = 0; // Pure virtual
// Some other method declarations might go here. Their implementation should be in dll00.cpp
};
DLL00_EXPORT_API void callSetX(BaseClass *pClass, int value);
dll00.cpp:
#define DLL00_EXPORTS
#include "dll00.h"
#include <iostream>
using std::cout;
void callSetX(BaseClass *pClass, int value) {
if (!pClass) {
cout << "NULL pointer!\n";
return;
}
cout << "Setting x\n";
pClass->setX(value);
}
主.cpp:
#include "dll00.h"
#include <iostream>
using std::cout;
class DerivedClass : public BaseClass {
public:
void setX(int x) override;
private:
int x;
};
void DerivedClass::setX(int x) {
std::cout << "setX(" << x << ")\n";
this->x = x;
}
int main() {
DerivedClass *pInst = new DerivedClass;
callSetX(pInst, 255);
return 0;
}
输出 :
[cfati@CFATI-5510-0:e:\Work\Dev\StackOverflow\q061152778]> sopr.bat *** Set shorter prompt to better fit when pasted in StackOverflow (or other) pages *** [prompt]> "c:\Install\pc032\Microsoft\VisualStudioCommunity\2017\VC\Auxiliary\Build\vcvarsall.bat" x64 ********************************************************************** ** Visual Studio 2017 Developer Command Prompt v15.9.21 ** Copyright (c) 2017 Microsoft Corporation ********************************************************************** [vcvarsall.bat] Environment initialized for: 'x64' [prompt]> dir /b dll00.cpp dll00.h main.cpp [prompt]> cl /nologo /MD /DDLL /EHsc dll00.cpp /link /NOLOGO /DLL /OUT:dll00.dll dll00.cpp Creating library dll00.lib and object dll00.exp [prompt]> cl /nologo /MD /EHsc main.cpp /link /NOLOGO /OUT:main.exe dll00.lib main.cpp Creating library main.lib and object main.exp [prompt]> dir /b dll00.cpp dll00.dll dll00.exp dll00.h dll00.lib dll00.obj main.cpp main.exe main.exp main.lib main.obj [prompt]> main.exe Setting x setX(255)
有关从 .dll 导出内容的更多详细信息,您可以查看 [SO]: Linker error when calling a C function from C++ code in different VS2010 project (@CristiFati's answer) .
备注 :在 Nix 上,我经常看到这种行为(在 C 中):一个 .so 引用在加载它的可执行文件中定义的符号(一个这样的例子是静态 Python(其中核心在可执行文件中),并且引用了这些符号通过扩展模块),但在 Win(至少是 VStudio 编译器)上这是不可能的。
关于c++如何在运行时链接函数声明?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61152778/