Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Determining endianness in extconf.rb

Reply
Thread Tools

Determining endianness in extconf.rb

 
 
Aaron Patterson
Guest
Posts: n/a
 
      12-05-2005
Hi. Hopefully this is the right list for this question!

I am writing an extension for Ruby in C, and I need to determine
the endianness of the machine and set a flag when I compile. What is
the best way to go about this? I've seen examples of determining
endianness but I don't know how to set the flag in extconf.rb

Any help would be greatly appreciated. Thank you!

--Aaron



 
Reply With Quote
 
 
 
 
Daniel Berger
Guest
Posts: n/a
 
      12-05-2005
Aaron Patterson wrote:
> Hi. Hopefully this is the right list for this question!
>
> I am writing an extension for Ruby in C, and I need to determine
> the endianness of the machine and set a flag when I compile. What is
> the best way to go about this? I've seen examples of determining
> endianness but I don't know how to set the flag in extconf.rb
>
> Any help would be greatly appreciated. Thank you!
>
> --Aaron
>
>


Modify $CFLAGS or $CPPFLAGS directly:

# extconf.rb
if [1].pack("I") == [1].pack("N")
$CFLAGS += " -DBIG_ENDIAN" # note the leading space
end

/* In your C code somewhere */
#ifdef BIG_ENDIAN
/* Do something */
#endif

Regards,

Dan


 
Reply With Quote
 
 
 
 
Aaron Patterson
Guest
Posts: n/a
 
      12-05-2005
On Tue, Dec 06, 2005 at 04:21:37AM +0900, Daniel Berger wrote:

[snip]

> Modify $CFLAGS or $CPPFLAGS directly:
>
> # extconf.rb
> if [1].pack("I") == [1].pack("N")
> $CFLAGS += " -DBIG_ENDIAN" # note the leading space
> end
>
> /* In your C code somewhere */
> #ifdef BIG_ENDIAN
> /* Do something */
> #endif
>
> Regards,
>
> Dan
>


Thank you for the help!

--Aaron



 
Reply With Quote
 
nobuyoshi nakada
Guest
Posts: n/a
 
      12-06-2005
Hi,

At Tue, 6 Dec 2005 04:21:37 +0900,
Daniel Berger wrote in [ruby-talk:168948]:
> # extconf.rb
> if [1].pack("I") == [1].pack("N")
> $CFLAGS += " -DBIG_ENDIAN" # note the leading space
> end


It tells the endian of the running platform, but not of the
target platform. They can differ when cross-compiling.

Since config.h defines WORDS_BIGENDIAN for big-endian
platforms, you don't have to test it in extconf.rb.

--
Nobu Nakada


 
Reply With Quote
 
Aaron Patterson
Guest
Posts: n/a
 
      12-06-2005
On Tue, Dec 06, 2005 at 02:00:33PM +0900, nobuyoshi nakada wrote:
[snip]
>
> Since config.h defines WORDS_BIGENDIAN for big-endian
> platforms, you don't have to test it in extconf.rb.
>
> --
> Nobu Nakada


I guess I don't understand how I am supposed to set up my build
environment.... As far as I can tell, config.h is generated while
building Ruby from source. The only config.h on my system is:

/usr/lib/ruby/1.8/i386-linux/config.h

Which does not have WORDS_BIGENDIAN listed in it. I did find
WORDS_BIGENDIAN in /usr/lib/ruby/1.8/i386-linux/defines.h but it just
says to look at __BIG_ENDIAN__.

Can you point me to a small project that I can model my build after, or
an example exconf.rb?

Thanks for the help!

--Aaron



 
Reply With Quote
 
Guillaume Marcais
Guest
Posts: n/a
 
      12-07-2005
On Wed, 2005-12-07 at 07:22 +0900, Aaron Patterson wrote:
> On Tue, Dec 06, 2005 at 02:00:33PM +0900, nobuyoshi nakada wrote:
> [snip]
> >
> > Since config.h defines WORDS_BIGENDIAN for big-endian
> > platforms, you don't have to test it in extconf.rb.
> >
> > --
> > Nobu Nakada

>
> I guess I don't understand how I am supposed to set up my build
> environment.... As far as I can tell, config.h is generated while
> building Ruby from source. The only config.h on my system is:
>
> /usr/lib/ruby/1.8/i386-linux/config.h
>
> Which does not have WORDS_BIGENDIAN listed in it. I did find
> WORDS_BIGENDIAN in /usr/lib/ruby/1.8/i386-linux/defines.h but it just
> says to look at __BIG_ENDIAN__.


That's because you are on Intel, which is little endian.

> Can you point me to a small project that I can model my build after, or
> an example exconf.rb?


What he is saying is that you should not modify your extconf.rb file. In
your C code, just do:

#ifdef WORDS_BIGENDIAN
// Big endian code
#else
// Little endian code
#endif

Guillaume.

>
> Thanks for the help!
>
> --Aaron
>
>




 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Endianness and streams kelvSYC C++ 8 06-06-2005 12:08 AM
Endianness kelvSYC C++ 3 06-03-2005 02:49 PM
memcpy() and endianness Case C Programming 26 05-12-2004 02:47 PM
endianness and sscanf/sprintf pramod C++ 22 01-06-2004 01:02 PM
endianness and sscanf/sprintf pramod C Programming 22 01-06-2004 01:02 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57