Using libraries in apps.

Discussion in 'iOS Development' started by gojohnnyboi, Aug 5, 2008.

  1. gojohnnyboi

    gojohnnyboi Well-Known Member

    Joined:
    Jan 25, 2008
    Messages:
    3,339
    Likes Received:
    55
    Ok, i'm gonna show everyone how to use a dynamic link library in your code. I am doing this in my apps here, as a framework type thing, so i can code less and make easier apps. Below is the source code for a sample app i made that just resprings using a function from a dynamic link library (.dylib)

    Application.mm
    Code:
    #import "Library.h"
    
    int main()
    {
    	respring();
    	return 0;
    }
    Library.h
    Code:
    #import <stdio.h>
    #import <stdlib.h>
    
    void respring();
    Library.mm
    Code:
    #import <stdio.h>
    #import <stdlib.h>
    
    void respring()
    {
    	system("killall SpringBoard");
    }
    Makefile
    Code:
    CC=arm-apple-darwin9-gcc
    dylib=librespring.dylib
    app=respring
    
    libflags= -dynamiclib
    appflags= -L./ -lrespring
    
    
    all: $(dylib) $(app) SignBinary
    
    $(app):
    		$(CC) -multiply_defined suppress $(appflags) -o $(app) Application.mm
    		
    $(dylib):
    		$(CC) -multiply_defined suppress $(libflags) -o $(dylib) Library.mm
    
    SignBinary: 
    		ldid -S $(app)
    		ldid -S $(dylib)
    
    clean:
    		rm -f *.o $(app)
    		rm -f *.o $(dylib)

    We use .mm here, as .m is for objective-c code, .h is for header file, and .mm allows c, c++, and objective-c combined, which in this case we must use for the library.

    The product:
    librespring.dylib
    respring

    you will notice that ./respring in terminal will not work unless you have the dylib, proving its dependence on it, and how it works. Note that compiling a dylib requires no main() function, and is created by passing the compiler flag "-dynamiclib". We include the current directory when compiling the app with "-L./", so that we can then link against the library we just created by passing the flag "-lrespring". It automatically looks for librespring.dylib as your dylib must have the prefix "lib" or else you will have to use dlopen, which is a more compicated alternative.


    This makes for easy coding in certain ways! Try it out and have fun!

    -John
  2. Faktor4

    Faktor4 Active Member

    Joined:
    Dec 26, 2007
    Messages:
    597
    Likes Received:
    34

Share This Page