Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/136.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sockets/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ JNA程序函数查找失败_C++_Jna - Fatal编程技术网

C++ JNA程序函数查找失败

C++ JNA程序函数查找失败,c++,jna,C++,Jna,我是JNA编程新手,我想完成的任务是: C++库公开了将缓冲区“放入”文件并“查找”缓冲区的功能。我为这个库编译了一个共享对象(.so),头文件提供了“extern”C“”下的函数定义,使它对C编译器友好 测试java程序以访问缓冲区 代码如下所示: C/C++代码: extern "C" { int get(int length, char *buffer); } #include <iostream> #include <string.h> int get(int

我是JNA编程新手,我想完成的任务是:

  • C++库公开了将缓冲区“放入”文件并“查找”缓冲区的功能。我为这个库编译了一个共享对象(.so),头文件提供了“extern”C“”下的函数定义,使它对C编译器友好

  • 测试java程序以访问缓冲区

  • 代码如下所示:

    C/C++代码:

    extern "C"
    {
    int get(int length, char *buffer);
    }
    
    #include <iostream>
    #include <string.h>
    
    int get(int length, char *buffer)
    {
        char *newBuff = new char[length];
        for (int i = 0; i < length; ++i)
        {
            newBuff[i] = 'a';
        }
    
        memcpy(newBuff, buffer, length);
        delete newBuffer;
        return length;
    }
    
    在运行该程序时,我在调用“lib.get()”方法时收到以下错误消息:

    线程“main”java.lang.UnsatifiedLinkError中出现异常:查找函数“get”时出错:dlsym(0x7f8d08d1e7d0,get):找不到符号(根据
    nm
    ),导出的符号已损坏。除了函数定义的声明之外,还需要在函数定义之前添加
    extern“C”
    ,即

    extern "C" get(int length, char* buffer) {
        ...
    }
    

    您使用的第一个
    extern“C”
    语法通常用于头文件中的声明组。您还必须明确地解开定义。

    我可以通过如下修改代码使其工作:

    public static interface TestNative extends Library
        {
            int get(int length, Pointer buffer);
        }
    
    指针是通过以下方式获得的:

    Pointer bfPtr = Native.getDirectBufferPointer(buffer); // buffer points to ByteBuffer allocated as direct NIO buffer.
    

    我从未使用过JNA,但它似乎在抱怨,因为您在
    TestNative
    接口中声明了一个名为
    get
    的函数,但从未定义它语句,因为它是指向arrar.sure的指针,但它不是主要关注点,而Java代码保持不变。将在C++中修复内存泄漏,不管使用什么代码> NM文件名< /CUL>查看从库中导出什么符号。symbol bash-3.2$nm libsample.so 0000000000000 EB0 T_uuuuz3GetIPCU_uuuuzAPVU_uuzNAMU memcpy U dyld_stub_binder bash-3.2$c++filt_uuuz3GetIPCGET(int,char*)
    Pointer bfPtr = Native.getDirectBufferPointer(buffer); // buffer points to ByteBuffer allocated as direct NIO buffer.