Help Understanding Ruby/C Bindings

B

Bryan Richardson

Hello all,

Can anyone walk me through how, for example, the binding between
libpcap-ruby and libpcap? If I look at pcaplet and pcap_misc, I notice
that 'pcap' is required. Is this another Ruby file (that I can't find)
or is this referencing the libpcap C library directly?

If anyone can explain to me how this works I'd greatly appreciate it.
There's a good chance I'll need to be binding to C code from Ruby in the
near future and I'd like to understand the best way to go about doing
so.
 
J

Jason Roelofs

Hello all,

Can anyone walk me through how, for example, the binding between
libpcap-ruby and libpcap? If I look at pcaplet and pcap_misc, I notice
that 'pcap' is required. Is this another Ruby file (that I can't find)
or is this referencing the libpcap C library directly?

If anyone can explain to me how this works I'd greatly appreciate it.
There's a good chance I'll need to be binding to C code from Ruby in the
near future and I'd like to understand the best way to go about doing
so.

Kernel#require can load both another Ruby file or a Ruby extension.
Ruby extensions are shared libraries that are required to have at
least one certain method in them:

extension.c

void Init_extension() {
... initialization code here ...
}

builds into extension.so (on Linux, extension.dll on Windows)

Then you can

require 'extension'

and start using whatever the library has made available. In the case
of libpcap-ruby, there's a pcap.so file somewhere in your system (I'm
assuming you apt-get installed it, or the related on your system)
that's accessible by Ruby.

That's the gist of it, for more detailed information, go here:
http://www.rubycentral.com/pickaxe/ext_ruby.html

Jason
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top