[英]UnsatisfiedLinkError - how to create the c++-DLL?
I am trying to generate a dll which I can access from java via JNA. 我正在尝试生成一个可以通过JNA从Java访问的dll。
I just try a simple one: 我只是尝试一个简单的方法:
CPP: CPP:
void Initialize()
{
std::cout<< "Hello World!";
}
iostream is also included and after compiling I get: CreateDll.dll iostream也包括在内,编译后得到:CreateDll.dll
Via Visual Studio I can generate now a *.dll. 通过Visual Studio,我现在可以生成一个* .dll。
which I try loading into Java like: 我尝试将其加载到Java中,例如:
public class mainRoutine {
public static void main(String[] args) {
NativeWrapper INSTANCE = (NativeWrapper) Native.loadLibrary("CreateDll" , NativeWrapper.class);
INSTANCE.Initialize();
}
static {
System.setProperty("jna.library.path", "C:\\workspace\\JNA");
}
}
There is also another Interface: 还有另一个接口:
import com.sun.jna.Library; 导入com.sun.jna.Library;
public interface NativeWrapper extends Library {
void Initialize();
}
So but now running the Java function I get the error, 因此,但是现在运行Java函数,我得到了错误,
java.lang.UnsatisfiedLinkError:
Error looking up function 'Initialize':
The specified procedure could not be found.
What am I missing? 我想念什么?
PS: I know there are many topics, but trying for a day already I have not found the solution. PS:我知道有很多主题,但是已经尝试了一天,但没有找到解决方案。 Please help me.
请帮我。
Are you exporting the symbol: 您要导出符号吗?
void _declspec(dllexport) Initialize()
{
std::cout<< "Hello World!";
}
You need to both export and (if using C++) un-decorate the function name. 您需要导出和(如果使用C ++)取消修饰函数名称。
On windows, functions are typically made available for export with __declspec(dllexport)
. 在Windows上,通常可以使用
__declspec(dllexport)
导出函数。
On any platform, to ensure a function is exported in unmanagled form, you must use extern "C"
. 在任何平台上,要确保以未处理的形式导出功能,必须使用
extern "C"
。
Specifically: 特别:
extern "C" void __declspec(dllexport) Initialize() { ... }
There are other ways to designate exported functions, but this is probably the most common and straightforward to use. 还有其他方法可以指定导出的函数,但这可能是最常见和最直接的使用方法。 If you don't use
extern "C"
, your function will look something like InitializeZ@ASDF@
, where the additional garbage characters are used by the compiler and linker to make a given function uniquely recognizable based on its calling signature. 如果您不使用
extern "C"
,您的函数将类似于InitializeZ@ASDF@
,在该函数中,编译器和链接器使用其他垃圾字符来使给定函数根据其调用签名唯一地识别。
What if there is a class to be implemented ? 如果要实现一个类怎么办? where the .h file looks like this:
.h文件如下所示:
namespace simpleDLLNS
{
class simpleDLL
{
public:
char giveVoidPtrGetChar(void* param);
int giveIntGetInt(int a);
void simpleCall(void);
int giveVoidPtrGetInt(void* param);
};
}
Where should extern "C" void __declspec(dllexport) be used ? 应该在哪里使用extern“ C” void __declspec(dllexport)? I used it when implementing the functions.
我在实现功能时使用了它。 but when i opened the dll, it looked like this:
但是当我打开dll时,它看起来像这样:
?simpleCall@simpleDLL@simpleDLLNS@@QAEXXZ ?simpleCall @ simpleDLL @ simpleDLLNS @@ QAEXXZ
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.