将Cocoa标头链接到ruby C扩展

时间:2012-10-14 23:45:59

标签: objective-c c ruby makefile ruby-c-extension

我正在为ruby开发一个C扩展,但我需要包含来自IOBluetooth框架的标头,具体来说:

#import <IOBluetooth/objc/IOBluetoothDeviceInquiry.h>
#import <IOBluetooth/IOBluetoothUserLib.h>

一切编译都很好,但在运行时,扩展错误:

path/to/file.rb:1:in `require_relative': dlopen(/path/to/extension.bundle, 9):
Symbol not found: _OBJC_CLASS_$_IOBluetoothDeviceInquiry (LoadError)

我很确定这与框架没有包含在链接过程中有关,但我不确定为什么。任何帮助将不胜感激

extconf.rb

# Loads mkmf which is used to make makefiles for Ruby extensions
require 'mkmf'

# Give it a name
extension_name = 'bluetooth'

dir_config(extension_name)

create_makefile(extension_name, 'bluetooth')

生成的MakeFile: http://paste.wilhall.com/25

更新 我修改了Makefile,以便库静态链接;仍然遇到相同的错误,但这次链接。

奇怪的是,我have_header文件中的extconf.rb发现这些头文件很好。

更新 我从系统日志文件中提取了以下内容:

Process:         ruby [951]
Path:            /usr/local/bin/ruby
Identifier:      ruby
Version:         0
Code Type:       X86-64 (Native)
Parent Process:  bash [468]
User ID:         501

Date/Time:       2012-10-17 14:06:57.425 -0400
OS Version:      Mac OS X 10.8.1 (12B19)
Report Version:  10

Crashed Thread:  0  Dispatch queue: com.apple.main-thread

Exception Type:  EXC_BAD_ACCESS (SIGABRT)
Exception Codes: KERN_INVALID_ADDRESS at 0x0000000000000010

如果我没记错,EXC_BAD_ACCESS (SIGABRT)通常与GC问题有关?将调查并发布结果

2 个答案:

答案 0 :(得分:1)

这看起来太简单了,所以我可能会离开,但它看起来与你发布的内容相符。看起来问题不是标题,而是你没有链接IOBluetooth框架本身。您是否在任何地方指定-framework IOBluetooth之类的内容?

答案 1 :(得分:1)

确保包含

-framework Foundation System/Library/Frameworks/IOBluetooth.framework/IOBluetooth

在你的makeFile中。确保您在正确的目录中拥有框架。 但如果你可以编译它,链接它,你的IOBluetooth似乎现在在你的二进制文件中。

顺便说一下,您尝试访问地址0x02处的指针,这就是创建崩溃的原因。我不确定它是否与IOBluetooth有关