Author: Sun Dongfeng)
In the actual programming process, some common functions are usually made into function libraries for use by other programs. One is to reuse the code, and the other is to keep the core technology confidential. Therefore, in actual project development, function libraries are often used. The function libraries are divided into static and dynamic libraries. Similar to the dynamic and static languages that most people are familiar with, static and dynamic languages are called relative to the compilation and runtime periods: static libraries are linked to the target code during program compilation, when the program is running, you no longer need to change the static library. When the program is compiled, the dynamic library is not linked to the target code. It is loaded only when the program is running, because the dynamic library is also required during the running of the program.
The iPhone only supports static Library Association. This article uses static library as an example to explain the usage of static library in the iPhone. In the actual development process, some Functional Comparison interface engines and network communication engines may be planned into the corresponding function libraries during the project design phase.
Open XCode to create a project, select "Cocoa Touch Static Library" under the Library, and name it "FirstLibrary ". In this New static library project, there are no program files except "FisrtLibrary_Prefix.pch". Right-click the Classes folder and choose "New File ...", Then select "Objective-C Class" under "Cocoa Touch class". Here I will first create an Objective-C style source file and header file and name it "function ", you can see that the function is generated under the Classes directory. h and function. m file. Enter the following content in the header file:
# Import <Foundation/Foundation. h>
@ Interface function: NSObject {
}
-(Int) getMax :( int) a B :( int) B;
-(Int) getMin :( int) a B :( int) B;
@ End
The file function. m is implemented as follows:
# Import "function. h"
@ Implementation function
-(Int) getMax :( int) a B :( int) B
{
Return a>? A: B;
}
-(Int) getMin :( int) a B :( int) B
{
Return a <B? A: B;
}
@ End
It can be seen that the function of this static function library is very simple. It provides two functions to obtain the maximum and minimum values. However, the source code of most existing function libraries may be in the C or C ++ format. It doesn't matter. The iPhone also supports C/C ++ well. Right-click the Classes folder and select "New File ...", This time, select "C and C ++" under Mac OS X, then select "C File" and name it "TestCFunction ", we can see that two files named "TestCFunction" are generated under the Classes directory. h and TestCFunction. c ", TestCFunction. h:
# Ifndef TestCFunction_H _
# Define TestCFunction_H _
Void swapValue (int * a, int * B)
{
Int temp = 0;
Temp = *;
* A = * B;
* B = temp;
}
# Endif
The TestCFunction. c file remains unchanged.
At this point, this static function library has been compiled. Although it is very simple, it contains source code files of the Objective-C style and C style, compile this program and you will see that "libFirstLibrary" is generated under the Products directory. a "static library file.
Create a new "Window-based Application" project named "FirstLibraryTest". The following describes how to use the previously generated static library libFristLibrary. a file in this new project.
First, drag the "FirstLibrary. xcodeproj file to the Frameworks directory of the new project (you can also right-click Frameworks> Add-> Existing Files .. add), such:
Figure 1
Then select the file "FristLibrary. xcodeproj" and select the last item in the editing window on the right to add the static library to the project, for example:
Figure 2
After adding the static library to the project, you need to establish "Direct Dependencies (so-called dependency)" between the project and the static library )". Select "FirstLibraryTest" under the "Targets" directory, and then press the shortcut key Cmd + I (or right-click and select Get Info) to view the project dependency information, for example:
Figure 3
Click the "+" button under "Direct Dependencies". The following page appears:
Figure 4
Select "FirstLibrary" and click "Add Target". In this way, the "dependency" between the project and the static library is established. During the project compilation phase, the program searches for the target file in the static library.
Then, use the function in the static library to modify the header file FirstLibraryTestAppDelegate. h as follows:
# Import <UIKit/UIKit. h>
# Import "function. h"
# Include "TestCFunction. h"
@ Interface FirstLibraryTestAppDelegate: NSObject <UIApplicationDelegate> {
UIWindow * window;
Function * iFunc;
}
@ Property (nonatomic, retain) IBOutlet UIWindow * window;
@ End
Modify the implementation file as follows:
# Import "FirstLibraryTestAppDelegate. h"
@ Implementation FirstLibraryTestAppDelegate
@ Synthesize window;
-(Void) applicationDidFinishLaunching :( UIApplication *) application {
IFunc = [[function alloc] init];
Int a = 3, B = 6;
Int max = 0, min = 0;
Max = [iFunc getMax: a B: B];
Min = [iFunc getMin: a B: B];
NSLog (@ "max = % d, min = % d", max, min );
Int c = 8, d = 22;
SwapValue (& c, & d );
NSLog (@ "after swapValue c = % d, d = % d", c, d );
// Override point for customization after application launch
[Window makeKeyAndVisible];
}
-(Void) dealloc {
[Window release];
[Super dealloc];
}
@ End
After compilation, the project cannot be compiled. The error message is as follows:
Error: function. h: No such file or directory
That is to say, the project cannot find the header file of the corresponding static library, and thus it cannot establish a "dependency" between the project and the static library ", therefore, you need to tell the project the location of the static library header file that it depends on in the "dependency information options" of the project, such as setting the location information of the static library header file:
Figure