我在共享库(* .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 并且有所不同
#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);
}
main.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)。