gem本机扩展到底是什么? [英] What exactly is a gem native extension?

查看:104
本文介绍了gem本机扩展到底是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我觉得本机扩展就像是库,您应该在尝试安装那些依赖于本机扩展的gems之前,将它们安装到系统上.类似于ImageMagic库.那是对的吗?关于本机扩展,还有其他我们应该知道的东西吗?

I feel that a native extension is like libraries that you should install onto your system before trying to install those gems, which depend on the native extensions. Like the ImageMagic library. Is that correct? Is there something else that we should know about native extensions?

推荐答案

gem本机扩展可能链接到需要预先安装的单独库,而RMagick是以下示例那个.

A gem native extension might link to a separate library that needs to be pre-installed, and RMagick is an example of that.

但是,该术语实际上仅表示包括一些需要为您的机器编译的代码".代码经过编译和链接,以便生成的可执行文件可以由Ruby在计算机上require d.

However, the term really just means "includes some code that needs to be compiled for your machine". The code is compiled and linked so that the resulting executable file can be required by Ruby on the machine.

编写Ruby C或C ++扩展的通常原因是:

The usual reasons for writing a Ruby C or C++ extension are:

  • 速度.对于某些CPU密集型任务,C代码可以比Ruby快100倍.在这种情况下,本机扩展可以完全独立,并且gem中包含所有C源代码.

  • Speed. For some CPU-intense tasks, C code can be 100 times faster than Ruby. In this case a native extension can be completely stand-alone with all C source code included in the gem.

已经用C语言编写的第三方库.在这种情况下,gem将具有C源代码,该源代码将库函数绑定到Ruby模块,类和方法中.

Third-party library already written in C. In this case the gem will have C source code that binds the library functions into Ruby modules, classes and methods.

您可以查看具有本地扩展名的gem的C源代码,它与Ruby源代码一起安装.按照惯例,gem内有一个名为ext/gem_name的文件夹,其中包含一个Ruby文件extconf.rb,该文件将信息传递给编译器(从技术上讲,它会创建一个制作文件).另外,C源文件也放在那里.

You can view the C source code for gems with native extensions, it is installed alongside the Ruby source code. By convention, there is a folder inside the gem called ext/gem_name which contains a Ruby file extconf.rb that passes information to the compiler (technically it creates a make file). In addition the C source files are put there as well.

MRI Ruby在C语言中实现为非常扁平"的结构,基本上由大量C函数组成.即使您不太了解C,这也使学习如何实现本机扩展变得相对容易.您可以阅读扩展Ruby 1.9 作为该主题的介绍.

MRI Ruby is implemented as a very "flat" structure in C, basically consisting of a large number of C functions. This makes it relatively easy to learn how to implement a native extension, even if you don't know much C. You can read Extending Ruby 1.9 as an introduction to the topic.

本机扩展程序可能无法安装或正常工作.在Stack Overflow上有很多问题,寻求有关特定失败安装的帮助.常见的问题是:

Native extensions can fail to install or work properly. There are many questions on Stack Overflow asking for help with specific failed installs. The usual problems are:

  • 缺少库.希望gem作者会解释您需要在README中进行预安装的内容,但并不总是很清楚.

  • Missing libraries. Hopefully the gem author will explain what you need to pre-install in the README, but is not always clear.

编译器不匹配.测试所有目标系统很困难,因此有时extconf.rb中的指令在特定系统上不起作用,或者C代码会因不同而发出警告或无法编译.在Windows中,除非您安装 Ruby Devkit

Compiler mismatches. It is difficult to test all target systems, so sometimes the instructions in extconf.rb won't work on a specific system, or the C code will raise warnings or not compile due to differences. In Windows, you probably won't have a working compiler unless you install the Ruby Devkit

不适用于所有版本的Ruby.例如,JRuby 可以使用C本机扩展,如果已启用,但并非总是建议这样做-该主题相当复杂,但通常避免将JRuby和本机扩展混在一起.

Will not work with all versions of Ruby. For instance, JRuby can use C native extensions, if it has been enabled but it is not always advisable - the topic is quite complex, but generally avoid mixing JRuby and native extensions.

这篇关于gem本机扩展到底是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆