首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >我怎么才能叫目标-c从cython?

我怎么才能叫目标-c从cython?
EN

Stack Overflow用户
提问于 2013-08-16 05:57:38
回答 1查看 1K关注 0票数 2

Cython可以编写C代码,也可以cross compileiOS

使用Pyobjus可以调用Objective-C代码,但只用于在OSX上运行,而不是在iOS上运行,因为python不能import _ctypes

我知道kivy可以通过CythonCoreGraphices.Framework打电话。

kivy/core/image/img_imageio.pyx

代码语言:javascript
复制
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

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

pyobjc.m

代码语言:javascript
复制
#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

代码语言:javascript
复制
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

代码语言:javascript
复制
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,
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-03-13 10:49:19

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

  1. 将生成的Cython文件从.c重命名为.m
  2. 或者,使.h目标-c免费,即创建C方法,并将objective复杂性隐藏到库的.m中。
  3. 或者直接去比布朱斯?
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/18266716

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档