c++ - c ++如何在运行时链接函数声明?
问题描述
我的共享库 (*.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++概念。即使 2 nd建立在 1 st之上,他们也有非常不同的方式来处理这种情况。我将讨论C++的榕树。
但是,无论选择哪种方法,代码中都存在一些错误:
- MyClass定义出现了两次并且不同(也 - 个人评论 - 我发现名称以“ My ”开头,脑筋急转弯)
- 该类未导出
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编译器)上这是不可能的。
推荐阅读
- python - 我如何知道我的公钥和 RSA 算法中的私钥是什么?
- c - 如何读取将二进制文件中的元素放入其中的数组?
- python - 使用 Jenkins 同时执行 2 个 python 脚本
- sql-server - 在同一个表TSQL中创建重复行
- sql - 更新查询中的错误 3073 一直在工作
- javascript - 防止输入字段将点转换为逗号
- jquery - 更新线程函数中的隐藏字段值,并从客户端 JQuery 中获取隐藏字段值
- javascript - 使用 .sort 对数字数组进行排序,但我不知道为什么会这样
- c# - 为什么 PropertyInfo[] 没有填充?
- c# - 无法加载文件或程序集“Microsoft.ML.Data,版本=1.0.0.0,文化=中性,PublicKeyToken=cc7b13ffcd2ddd51”