我试图通过使用JNA使我的.so与java代码一起工作。 首先,我只想在cout中获取我用JAVA提供的参数<<在我的.so(C ++)中,似乎只有第一部作品才能看到我的代码
这是我的.h中的.h
class WestgardInterface {
private:
static WestgardInterface *ms_instance;
protected:
/*CONSTRUCTOR*/
WestgardInterface();
public:
/*DESTRUCTOR*/
~WestgardInterface();
static WestgardInterface *getInstance();
void WGevaluate( Measure m, double mean, double sd);
};
extern "C" void WGevaluate( Measure m, double mean, double sd){
WestgardInterface* lib = WestgardInterface::getInstance();
return lib->WGevaluate( m, mean, sd);
}
然后是我的.cpp
WestgardInterface *WestgardInterface::ms_instance = nullptr;
/** @details Constructor*/
WestgardInterface::WestgardInterface(){
// TODO Auto-generated constructor stub
}
/** @details Destructor*/
WestgardInterface::~WestgardInterface() {
// TODO Auto-generated destructor stub
}
WestgardInterface *WestgardInterface::getInstance(){
if (nullptr == ms_instance)
{
ms_instance = new WestgardInterface();
}
return ms_instance;
}
void WestgardInterface::WGevaluate(Measure m, double mean, double sd) {
cout<<"measure"<<m.getValue()<<endl<<m.getDate();
cout<<"mean"<<mean<<endl<<"sd"<<sd<<endl;
}
现在让我们看看我的java代码
public interface InterfaceLibWestgard extends Library {
void WGevaluateWithListOnly(WGInputs.ByValue input);
static InterfaceLibWestgard INSTANCE = (InterfaceLibWestgard)
Native.loadLibrary("../logic/resources/calculator/libuntitled.so",
InterfaceLibWestgard.class);
}
WGInputs类
public class WGInputs extends Structure {
public Measure.ByValue m;
public double mean;
public double sd;
public static class ByValue extends WGInputs implements Structure.ByValue {}
@Override
protected List<String> getFieldOrder() {
return Arrays.asList(new String[] { "m" , "mean" , "sd" }); // native variables order
}
}
最后是主
public class Westgard {
static {
System.setProperty("jna.library.path","../logic/resources/calculator");
}
public static void main(String[] args) {
// TODO Auto-generated method stub
WGInputs.ByValue jnaInputs = new WGInputs.ByValue();
Measure.ByValue m = new Measure.ByValue();
m.valeur = 18;
Date.ByValue d = new Date.ByValue();
d.m_day = 10;
d.m_month = 10;
d.m_year = 10;
d.m_hour = 12;
d.m_min = 12;
d.m_sec = 12;
m.date = d;
jnaInputs.m = m;
jnaInputs.mean = 7.5;
jnaInputs.sd = 4d;
InterfaceLibWestgard.INSTANCE.WGevaluateWithListOnly(jnaInputs);
}
}
所以当它运行时它应该在终端上写这个
measure18
date = 10/10/10
heure = 12:12:12
mean 7.5
sd4
但我得到了这个
measure18
date = 10/10/10
heure = 12:12:12
mean0
sd3.51276e+151
答案 0 :(得分:0)
最终发现juste需要更改java端并禁止所有ref表示和sd 然后定义这样的功能 void WGevaluateWithListOnly(WGInputs.ByValue input,float mean,float sd);