Determining endianness in extconf.rb

Discussion in 'Ruby' started by Aaron Patterson, Dec 5, 2005.

  1. 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
     
    Aaron Patterson, Dec 5, 2005
    #1
    1. Advertisements

  2. 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
     
    Daniel Berger, Dec 5, 2005
    #2
    1. Advertisements

  3. On Tue, Dec 06, 2005 at 04:21:37AM +0900, Daniel Berger wrote:

    [snip]
    Thank you for the help!

    --Aaron
     
    Aaron Patterson, Dec 5, 2005
    #3
  4. Hi,

    At Tue, 6 Dec 2005 04:21:37 +0900,
    Daniel Berger wrote in [ruby-talk:168948]:
    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.
     
    nobuyoshi nakada, Dec 6, 2005
    #4
  5. On Tue, Dec 06, 2005 at 02:00:33PM +0900, nobuyoshi nakada wrote:
    [snip]
    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
     
    Aaron Patterson, Dec 6, 2005
    #5
  6. That's because you are on Intel, which is little endian.
    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.
     
    Guillaume Marcais, Dec 7, 2005
    #6
    1. Advertisements

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 (here). After that, you can post your question and our members will help you out.