You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Linux环境下JNA映射C语言.so库updateTarget函数出错求助

JNA调用C .so库updateTarget函数映射错误排查与修复

问题场景

在Linux环境下通过JNA调用C语言.so库时,updateTarget函数映射出现错误,该C函数会修改busPosition、busVelocity、pip和tgo的参数值。

C语言函数定义

void updateTarget(
    int idcar,
    double *carPosition,
    double *carVelocity,
    double isABT,
    double *busPosition,
    double *busVelocity,
    double *pip,
    double *tgo
);

原Java映射代码

void updateTarget(
    int idcar,
    double[] carPosition,
    double[] carVelocity,
    double isABT,
    double[] busPosition,
    double[] busVelocity,
    double pip,
    double tgo
);

错误原因分析

  1. 输出参数类型不匹配:C函数中pip和tgo是double*类型,属于指针参数,用于输出修改后的值;但原Java映射中定义为double基本类型,Java基本类型按值传递,无法将C函数修改后的值回传给Java层,同时类型不匹配会直接导致JNA调用时的内存访问错误。
  2. 补充说明:carPosition、carVelocity、busPosition、busVelocity这几个double*参数如果对应C侧的数组,Java用double[]映射是正确的,但要确保传入的数组长度与C函数预期一致。

修正方案

将Java映射中的pip和tgo改为double[]类型(用长度为1的数组模拟C的指针输出),这样C函数修改数组元素的值后,Java层可以直接读取到更新后的数据。

修正后的Java映射代码

void updateTarget(
    int idcar,
    double[] carPosition,
    double[] carVelocity,
    double isABT,
    double[] busPosition,
    double[] busVelocity,
    double[] pip,
    double[] tgo
);

调用示例

// 初始化输出参数数组
double[] pip = new double[1];
double[] tgo = new double[1];
// 调用函数
yourLibraryInstance.updateTarget(idcar, carPos, carVel, isABT, busPos, busVel, pip, tgo);
// 获取修改后的值
double pipValue = pip[0];
double tgoValue = tgo[0];

内容的提问来源于stack exchange,提问作者techtomo

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.12 17:54:56