2

Cython会写C代码,并且cross compile可以iOS

使用Pyobjuscan 调用Objective-C代码,但仅用于运行 on OSX,而不是 on iOS,因为 python 不能import _ctypes

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

kivy/core/image/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 compile但我做不到iOS

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,
4

1 回答 1

1

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

  1. 将生成的 Cython 文件重命名.c.m
  2. 或者,使.hobjective-c免费,即创建C方法并将objective-c复杂性隐藏到.m您的库中。
  3. 或者,直接pybojus?
于 2014-03-13T10:49:19.080 回答