Application with static library runs on simulator

2019-07-25 15:47发布

问题:

I have an monotouch application that includes an objective-c static library. The application runs correctly on the simulator, but when I try run the app on my iPhone 3GS, it crashes on startup. These are the steps that I have taken to try get it working:

  • Compiled the static library in Xcode with Device specified and active architecture set at armv6 and at armv7 (I am not sure which is correct, but I tried both and neither worked).
  • Under the project info I set code signing identity to my developer key.
  • In MonoDevelop I have included the static library in the application project options by setting the additional monotouch arguments under iPhone build to have the following value (this is identical to what is set for iPhoneSimulator):

    -v -v -v -gcc_flags "-lstdc++ -I${ProjectDir}/Ultralite/Include -L${ProjectDir}/Ultralite -lUltralite -force_load ${ProjectDir}/Ultralite/libUltralite.a"

When I try run the application, it crashes on startup (so the application screen does not even appear). In MonoDevelop all that I see is the following exception message:

Exception of type 'Mono.Debugger.Soft.VMDisconnectedException' was thrown.

All that I see in the device log in Xcode is the following:

Exception Type:  EXC_BAD_ACCESS (SIGSEGV)
Exception Codes: KERN_PROTECTION_FAILURE at 0x2fd00f24

If I remove the -gcc_flags option from the project options, then the application starts up, but crashes at the first attempt at accessing the static library. So it is definitely something to do with the static library that is causing the application to crash on startup.

I have no idea where to even begin with solving this, and so really need some help on this one. Anyone got any ideas as to what is wrong with the static library that I am including, or know where I can get more information about what is going wrong? The KERN_PROTECTION_FAILURE message in the crash report is really not giving me much to work with.

Update: I have created a simple Hello World application which has one button, which when clicked calls a method sayHello in a static library. Even with this basic example I encountered the same problem; namely that it runs on the simulator but not on the actual device. I have uploaded my helloworld example to github. I would really appreciate it if someone could help me in getting this working. Here is the Hello World sample:

https://github.com/BruceHill/HelloWorld

This includes a folder for the basic application, a folder with the objective-c static library and then finally a folder for the btouch definition. I call btouch with the parameter -outdir=. to build Messaging.g.cs and UltraliteManager.g.cs which I then include in the MonoTouch application.

回答1:

I had to make two changes to get this working correctly on the iphone:

  1. linker behavior in the build options had to have the option Link all assemblies set.
  2. I had to add -framework Security to the gcc_flags.

So additional monotouch arguments under the build options had to have the following value:

-v -v -v -gcc_flags "-framework Security -lstdc++ -I${ProjectDir}/Ultralite/Include -L${ProjectDir}/Ultralite -lUltralite -force_load ${ProjectDir}/Ultralite/libUltralite.a"

The reason I had to add the Security framework is that it seems MonoTouch includes this framework when Don't link is specified in the build options, but does not include it when the other two options are set. I determined this by comparing the build logs for the different options.



回答2:

Open XCode Organizer. Plug in your device. Then look at the crashes. The data will symbolicate and you'd at least see at what point it failed.



回答3:

I had a similar problem with MonoTouch linking a 3rd party library through BTouch. Same exception types and codes.

You have to get the source code for that 3rd party library and compile it with THUMB disabled. XCode has this option, just do a search for THUMB in the options. libUltralite.a will end up being a little bit larger in size.



回答4:

Try adding "-ObjC" to your linker flags.

Update: There seems to be a some issues regarding static libraties. You may also try to use the -all_load flag: What does the -all_load linker flag do?