title: iOS 6.1 Dynamic Library build and link
body: <p>I am trying to create a dynamic library for iOS and load it at runtime. After taking a look at <a href="https://stackoverflow.com/questions/4733847/can-you-build-dynamic-libraries-for-ios-and-load-them-at-runtime">this question</a> and <a href="https://stackoverflow.com/a/10509453/2161791">this answer</a>, I have been doing it using iOSOpenDev and deploying everything on my iPhone. The xCode project for the dylib is called KDylibTwo and the files I modiefied are:</p>
  
  <p><strong>KDylibTwo.h</strong></p>
  
  <pre><code>#import &lt;Foundation/Foundation.h&gt;
  
  @interface KDylibTwo : NSObject
  -(void)run;
  @end
  </code></pre>
  
  <p><strong>KDylibTwo.m</strong></p>
  
  <pre><code>#import "KDylibTwo.h"
  
  @implementation KDylibTwo
  
  -(id)init
  {
      if ((self = [super init]))
      {
      }
  
      return self;
  }
  
  -(void)run{
      NSLog(@"KDylibTwo loadded.");
  }
  
  @end
  </code></pre>
  
  <p>In order to test if my library works, after building it for profiling (the way iOSOpenDev deploys it on iPhone), I can find it stored on my device at <code>/usr/lib/libKDylibTwo.dylib</code> and built a tweak (again using iOSOpenDev), hooking the SpringBoard as follows:</p>
  
  <pre><code>#include &lt;dlfcn.h&gt;
  
  %hook SBApplicationIcon
  
  -(void)launch{
      NSLog(@"\n\n\n\n\n\n\nSBHook For libKDylibTwo.dylib");
  
      void* dylibLink = dlopen("/usr/lib/libKDylibTwo.dylib", RTLD_NOW);
  
      if(dylibLink == NULL) {
          NSLog(@"Loading failed.");
      } else {
          NSLog(@"Dylib loaded.");
  
          void (*function)(void);
          *(void **)(&amp;function) = dlsym(dylibLink, "run");
          if (function) {
              NSLog(@"Function found.");
              (*function)();
          } else {
              NSLog(@"Function NOT found");
          }
      }
  
      NSLog(@"End of code");
      %log;
      %orig;
  }
  
  %end
  </code></pre>
  
  <p>After installing the tweak on the device and tapping on an icon (that would fire the hooked code), the Console output looks like:</p>
  
  <pre><code>Aug 28 13:03:35 Pudge SpringBoard[18254] &lt;Warning&gt;: SBHook For libKDylibTwo.dylib
  Aug 28 13:03:35 Pudge SpringBoard[18254] &lt;Warning&gt;: Dylib loaded.
  Aug 28 13:03:35 Pudge SpringBoard[18254] &lt;Warning&gt;: Function NOT found
  Aug 28 13:03:35 Pudge SpringBoard[18254] &lt;Warning&gt;: End of code
  Aug 28 13:03:35 Pudge SpringBoard[18254] &lt;Warning&gt;: -[&lt;SBApplicationIcon: 0x1d5008c0&gt; launch]
  </code></pre>
  
  <p>My question is what am I doing wrong and the the library's function is not called or executed! I think I should clarify that I am only talking about jailbroken devices and not App Store development, so please don't go ahead posting that it cannot be done!</p>
  
  <p>Thank you in advance,<br>
  Panagiotis.</p>