我编译了一个静态库,用于定位xcode中的“iOS设备”,并将它链接到monodouch应用程序。当我构建并运行应用程序时,我立即得到一个错误:静态库中断编译
Mono.Debugger.Soft.VMDisconnectedException: Exception of type 'Mono.Debugger.Soft.VMDisconnectedException' was thrown. at Mono.Debugger.Soft.Connection.SendReceive (CommandSet command_set, Int32 command, Mono.Debugger.Soft.PacketWriter packet) [0x00000] in :0 at Mono.Debugger.Soft.Connection.VM_GetVersion () [0x00000] in :0 at Mono.Debugger.Soft.Connection.Connect() [0x00000] in :0 at Mono.Debugger.Soft.VirtualMachine.connect() [0x00000] in :0 at Mono.Debugger.Soft.VirtualMachineManager.ListenInternal (System.Net.Sockets.Socket dbg_sock, System.Net.Sockets.Socket con_sock) [0x00000] in :0
而且没有应用程序输出。它似乎并不重要,如果我签署或不签署库,我已经尝试过各种版本,并建立设置,甚至编译器之间切换(我使用的最后一个编译器是股票GCC 4.2编译器)。当我为模拟器构建库并将其链接到应用程序以在设备上运行时,它实际上在设备上运行,但是一旦函数被p/invoked,应用程序就退出并且我得到系统输出解释该销售无法解决。
这个不运行的原因可能是因为构建设置或某个地方,因为它运行在模拟器上它可能是JIT与AOT问题。
编辑:
这是事物的C#方:我应该提到
:
[DllImport("__Internal")]
private static extern int CreateWorld(b2Vec2 grav, OnIOSContact startContact, ContactOver endContact);
delegate bool OnIOSContact(MyCollisionEvent ev);
delegate bool ContactOver(MyCollisionEvent ev);
[MonoTouch.MonoPInvokeCallback(typeof(OnIOSContact))]
static bool StatContactStart(MyCollisionEvent ev){...}
[MonoTouch.MonoPInvokeCallback(typeof(ContactOver))]
static bool StatContactEnd(MyCollisionEvent ev){...}
[StructLayout(LayoutKind.Sequential, Size=8),Serializable]
struct MyCollisionEvent
{
public IntPtr ActorA;
public IntPtr ActorB;
}
[StructLayout(LayoutKind.Sequential, Size=8),Serializable]
public struct b2Vec2
{
public b2Vec2(float _x, float _y)
{
x = _x;
y = _y;
}
public float x;
public float y;
}
和它背后的C代码:
extern "C"
int CreateWorld(b2Vec2 gravity, bool (*startContact)(Contact), bool (*endContact)(Contact))
//contact struct to match MyCollisionEvent in C# code
typedef struct
{
b2Body *bodyA;
b2Body *bodyB;
}Contact;
/// A 2D column vector.
struct b2Vec2
{
/// Default constructor does nothing (for performance).
b2Vec2() {}
/// Construct using coordinates.
b2Vec2(float32 x, float32 y) : x(x), y(y) {}
/// Set this vector to all zeros.
void SetZero() { x = 0.0f; y = 0.0f; }
/// Set this vector to some specified coordinates.
void Set(float32 x_, float32 y_) { x = x_; y = y_; }
/// Negate this vector.
b2Vec2 operator -() const { b2Vec2 v; v.Set(-x, -y); return v; }
/// Read from and indexed element.
float32 operator() (int32 i) const
{
return (&x)[i];
}
/// Write to an indexed element.
float32& operator() (int32 i)
{
return (&x)[i];
}
/// Add a vector to this vector.
void operator += (const b2Vec2& v)
{
x += v.x; y += v.y;
}
/// Subtract a vector from this vector.
void operator -= (const b2Vec2& v)
{
x -= v.x; y -= v.y;
}
/// Multiply this vector by a scalar.
void operator *= (float32 a)
{
x *= a; y *= a;
}
/// Get the length of this vector (the norm).
float32 Length() const
{
return b2Sqrt(x * x + y * y);
}
/// Get the length squared. For performance, use this instead of
/// b2Vec2::Length (if possible).
float32 LengthSquared() const
{
return x * x + y * y;
}
/// Convert this vector into a unit vector. Returns the length.
float32 Normalize()
{
float32 length = Length();
if (length < b2_epsilon)
{
return 0.0f;
}
float32 invLength = 1.0f/length;
x *= invLength;
y *= invLength;
return length;
}
/// Does this vector contain finite coordinates?
bool IsValid() const
{
return b2IsValid(x) && b2IsValid(y);
}
float32 x, y;
};
编辑2这是我第一次写这篇文章的时候,但是我已经能够让应用在设备上运行几次了。通常它涉及以略微不同的构建设置(例如签署库)来修复项目/库,但是我一直无法辨别导致这些成功构建需要采取的步骤...
每次它运行崩溃了(由于不相关的错误),但即使对代码稍作修改(例如注释掉一行)也会导致同样的错误再次弹出。即使恢复线路也不会使错误消失。
你可以编辑你的帖子,在C#(包括属性)和C中包含p/invoke签名吗?谢谢 – poupou
完成。这就是所谓的特定功能(它也是第一个被粉饰的)。如果库是为模拟器构建的,我不确定它是否应该在设备上工作。 – tweetypi
还请包括非原始类型的托管/本地定义,例如b2Vec2,联系人,OnIOSContact和ContactOver。当托管/本机表示不匹配时,您的随机崩溃很可能是由于堆栈处理不当而导致的。 – poupou