开发者

C/C++ functions returning NaN in MonoTouch when p/invoked

I have two source files:

File.h:

class Numbers
{
public:
    int Get10();
    int Get192();   
    float GetFloat();
};

File.cpp:

int Numbers::Get10()
{
    return 10;
}
int Numbers::Get192()
{
    return 192;
}
float Numbers::GetFloat()
{
    return 3213.1294124f;
}

extern "C"
int Get10()
{
    Numbers n;
    return n.Get10();
}

extern "C"
int Get192()
{
    Numbers n;
    return n.Get192();
}

extern "C"
int GetFloat()
{
    Numbers n;
    return n.GetFloat();
}

I compile these into a static library (libNumbers.a) and link it in with MonoTouch via the following mtouch arguments:

-v -v -v -gcc_flags "-cxx -framework Security -L${ProjectDir} -lNumbers -force_load ${ProjectDir}/libNumbers.a"

Then in the C# source file I have this:

    [DllImport("__Internal",EntryPoint="GetFloat")]
    public static extern float GetFloat();
开发者_如何学C

When I call GetFloat() from inside the C# source file the value returned in 'NaN' instead of 3213.1294124f. If I call Get10 however I actually get the correct value returned.


Maybe changing the return type of this function to float is the way to go?

extern "C"
int GetFloat()
{
    Numbers n;
    return n.GetFloat();
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜