2013-08-16 54 views
1

Cython可以编写C编码,并且可以编号为cross compileiOS;如何从cython中调用objective-c?

使用Pyobjus可以调用Objective-C代码,而只是对OSX运行, 不是iOS,因为Python不能import _ctypes

我知道kivy可以通过Cython呼叫CoreGraphices.Framework

kivy /核心/图像/ img_imageio.pyx

cdef extern from "CoreGraphics/CGDataProvider.h": 
    ctypedef void *CFDataRef      
    unsigned char *CFDataGetBytePtr(CFDataRef) 

    ctypedef struct CGPoint:      
     float x         
     float y         

    ctypedef struct CGSize:      
     float width        
     float height        

    ctypedef struct CGRect:      
     CGPoint origin       
     CGSize size        

    CGRect CGRectMake(float, float, float, float) 

这是我objective-c代码

pyobjc.h

#import <Foundation/Foundation.h> 
@interface PyObjc : NSObject 
-(char *)send:(NSString *)message; 
-(char *)recv; 
@property(nonatomic, strong) NSMutableArray *messageQueue; 
@end 

pyobjc.m

#import "pyobjc.h" 
@implementation PyObjc 
-(id)init{ 
NSLog(@"Init"); 
self.messageQueue = [[NSMutableArray alloc] init]; 
return self; 
} 

-(char *)send:(NSString *)message{ 
NSLog(@"Send: %@", message); 
[self.messageQueue addObject:message]; 
return [message UTF8String]; 
} 

-(char *)recv{ 
NSString *stringback = [self.messageQueue objectAtIndex:0]; 
NSLog(@"Recv: %@",stringback); 
[self.messageQueue removeObjectAtIndex:0]; 
return [stringback UTF8String]; 
} 
@end 

img_ble.pyx

cdef extern from "ble/pyobjc.h":    
    ctypedef char *PyObjcRef     
    char *PySend(PyObjcRef pyoc, char *message) 
    char *PyRecv(PyObjcRef pyoc)    

def send():          
    cdef PyObjcRef pychar      
    p = send("Hello Python")     
    pychar = p         

def recv():          
    cdef PyObjcRef pchar      
    p = recv()         
    pchar = p 

,但我不能cross compileiOS

building 'kivy.core.image.img_ble' extension 
    /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/usr/bin/arm-apple-darwin10-llvm-gcc-4.2 -DNDEBUG -g -O3 -Wall -Wstrict-prototypes -march=armv7 -mcpu=arm176jzf -mcpu=cortex-a8 -pipe -no-cpp-precomp -miphoneos-version-min=6.1 -O3 -g -I/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS6.1.sdk -I/Users/ygmpkk/Documents/Source_Learn/kivy-ios/tmp/Python-2.7.1/Include -I/Users/ygmpkk/Documents/Source_Learn/kivy-ios/tmp/Python-2.7.1 -c kivy/core/image/img_ble.c -o build/temp.macosx-10.8-x86_64-2.7/kivy/core/image/img_ble.o -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS6.1.sdk 
    In file included from /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS6.1.sdk/System/Library/Frameworks/Foundation.framework/Headers/Foundation.h:8, 
        from kivy/core/image/ble/pyobjc.h:9, 
        from kivy/core/image/img_ble.c:254: 
    /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS6.1.sdk/System/Library/Frameworks/Foundation.framework/Headers/NSObjCRuntime.h:409: error: stray ‘@’ in program 
    /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS6.1.sdk/System/Library/Frameworks/Foundation.framework/Headers/NSObjCRuntime.h:409: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘NSString’ 
    /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS6.1.sdk/System/Library/Frameworks/Foundation.framework/Headers/NSObjCRuntime.h:411: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘*’ token 
    /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS6.1.sdk/System/Library/Frameworks/Foundation.framework/Headers/NSObjCRuntime.h:412: error: expected ‘)’ before ‘*’ token 
    /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS6.1.sdk/System/Library/Frameworks/Foundation.framework/Headers/NSObjCRuntime.h:414: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘*’ token 
    /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS6.1.sdk/System/Library/Frameworks/Foundation.framework/Headers/NSObjCRuntime.h:415: error: expected ‘)’ before ‘*’ token 
    /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS6.1.sdk/System/Library/Frameworks/Foundation.framework/Headers/NSObjCRuntime.h:417: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘*’ token 
    /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS6.1.sdk/System/Library/Frameworks/Foundation.framework/Headers/NSObjCRuntime.h:418: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘*’ token 
    /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS6.1.sdk/System/Library/Frameworks/Foundation.framework/Headers/NSObjCRuntime.h:422: error: expected ‘)’ before ‘*’ token 
    /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS6.1.sdk/System/Library/Frameworks/Foundation.framework/Headers/NSObjCRuntime.h:423: error: expected ‘)’ before ‘*’ token 
    In file included from /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS6.1.sdk/System/Library/Frameworks/Foundation.framework/Headers/NSObject.h:6, 
        from /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS6.1.sdk/System/Library/Frameworks/Foundation.framework/Headers/NSArray.h:5, 
        from /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS6.1.sdk/System/Library/Frameworks/Foundation.framework/Headers/Foundation.h:10, 

回答

0

没有检测到你的编译器要编译的代码是objective- C。您可以:

  1. .c生成的用Cython文件重命名为.m
  2. 或者,使.h Objective-C的自由,即创建C方法和隐藏的Objective-C复杂到你的库的.m只。
  3. 或者,pybojus直接?
+0

Objective-C可以写C,我正在修复它 – Timothy