PassMakefileInIPhoneCreateDylibThe instance is the content to be introduced in this article. First, I want to declare thatDylibIt cannot be escaped.IphoneOn c. I wrote this article to improve the previous article.
1. Create dylibtest. c and. h.
A test function is written here.
Dylibtest. h
Void test ();
Dylibtest. c
- #include "dylibtest.h"
- #include "stdio.h"
- void test() {
- printf("this is a test\n");
- }
-
- makefile
-
- CC=/Developer/Platforms/iPhoneOS.platform/Developer/usr/bin/gcc
- CFLAGS= -arch armv6 -isysroot /Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS4.2.sdk
- CPP=/Developer/Platforms/iPhoneOS.platform/Developer/usr/bin/cpp
- target:
- $(CC) $(CFLAGS) -dynamiclib -o sotest-iphone.dylib dylibtest.c
Here we use sdk4.2 arch as armv6, and we also need to remind you that if you compile the. m file and use the framework, you can write it like this during compilation.
- -framework Foundation
The following is the test code testdylib.
The key code is as follows:
- NSString *path = [[NSBundle mainBundle] pathForResource:@"sotest-iphone" ofType:@"dylib"];
- void* handle = dlopen([path cStringUsingEncoding:NSUTF8StringEncoding], RTLD_LAZY);
- if (!handle) {
- printf("%s\n", dlerror());
- return;
- }
- void (*test)();
- test = (void (*)())dlsym(handle, "test");
- const char *dlsym_error = dlerror();
- if (dlsym_error) {
- printf("%s\n", dlsym_error);
- dlclose(handle);
- return;
- }
- // use it to do the calculation
- test();
- // close the library
- dlclose(handle);
You can test it, but I believe the result will not be satisfactory to you.
Conclusion: PassMakefileInIPhoneCreateDylibI hope this article will help you with the introduction of the instance!