写入标准输出时JNA无效的内存访问 [英] JNA Invalid memory access when writing to stdout

查看:964
本文介绍了写入标准输出时JNA无效的内存访问的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写一个C库JNA包装,在cygwin下用gcc编译。根据我如何执行JNA包装器的Java应用程序要么只是挂起(如果是在日食单元测试执行)或无效的内存异常终止。这个问题似乎只发生在C库写的东西要么stdout或stderr。
这是我最小的(不)工作的例子:

I am writing a jna wrapper for a c library, compiled using gcc under cygwin. Depending on how I execute the jna wrapper the java application either just hangs (if executed as unit test under eclipse) or terminates with an Invalid memory exception. The problem seems to occur only the the c library writes something to either stdout or stderr. Here is my minimal (not) working example:

add.c

#include <stdio.h>

int add (int x, int y)
{
    fprintf(stdout, "hello world\n" );
    return x + y;
}

JNA包装

public interface Add extends Library
{
    Add INSTANCE = (Add) Native.loadLibrary("add", Add.class);
    int add(int x, int y);
}

编译在Cygwin的C文件如下:

Compiling the c file under cygwin as follows:

gcc -g -Wall -c add.c
gcc -shared -o add.dll add.o

如果我删除行fprintf中的一切工作正常。无论是add.dll和cygwin1.dll是Java目标文件夹。

If I remove the fprintf line everything works fine. Both the add.dll and cygwin1.dll are in the java target folder.

推荐答案

您正在使用错误的语法fprintf中。它实际上是

You are using wrong syntax for fprintf. It is actually

INT fprintf中(FILE *流的限制,为const char *限制格式,...);

您需要传递字符串格式说明符(%S )了。

You need to pass string format specifier(%s) too.

这篇关于写入标准输出时JNA无效的内存访问的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆