Recently received a C-written SDK, encountered a lot of problems.
IOS Static Class Library compilation c,c++
As we all know, OC Native support C, in the creation of the OC class in the. m, you can write the C code directly;
Also Xcode supports OC, C + + mixed, at this time, we usually put OC created. m files, manually modified to. mm files to support OC C + +
Based on the above features, we can compile c,oc,c++ and three mixed code when we package static classes;
Because most of the platform's Algorithmic library code is written in C or C + +, we use Xcode to compile a static class library, can be very safe for others to use
The following example:
One: Static library packaging C code
1. Xcode creates a static class library project, which is the cocoa Touch static library;
2. After creating the project, file New,c file, create a A.C.
In A.C, write a test code, if the Hello () method, is our core code, do not want to let others know, just want to give the outside world a method name; Hello ()
void Hello (void) { printf ("Hello world!"); }
At this point, we can then create a Header file, named A.h, in the file, write
void Hello (void);
The method name of the. c file above, then in the Static class library options, Add. C to the compilation, Add. h to the output;
In this way, others are getting packaged. A, then configure the header file, you can use the Hello () method, without worrying about exposing the internal implementation of Hello ();
Note: When using this static library, do not use. mm, with. m; Because this compiles the C method, OC native supports C;
Second: Static class Library packaging C + + code
1. Xcode creates a static class library project, which is the cocoa Touch static library;
2. New File Creates a C + + Class in which the project will generate a name of A.cpp and A.h;
Can see in the future C + + code, method of the declaration into the A.h inside, the implementation of the method put into the A.cpp inside, can be achieved;
In the packaging provided to others, also put the CPP in the compilation, put. h into the output, you can;
As we also put a C in the A.h inside the test hello (); Method declaration
void Hello (void);
In A.cpp, write the realization of the method
void Hello (void) { printf ("Hello World");}
Packaging and compiling, when used, we will use. mm, and can not use. m,
Although the two ways of writing the same is the C method, but the meaning is not the same, the compiler is pure C, and the following, although the C code is written, but the compiler is C + +;
So when calling this package, use oc,c++ blends, i.e.. mm files;
Three: summary note
1. If you compile a pure c,.c file, use. m is oc,c mixed;
2. If compiling the C++,.cpp file, use. mm is the OC C + + mixed;
3.. cpp file inside, you can call write. c files, with. mm that is oc,c,c++ mixed;
IOS-Static Class library package c,c++ file and OC Mixed