Any Method to Determine Endianness at Compile Time ?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • perry.yuan

    Any Method to Determine Endianness at Compile Time ?

    How could I determine the endianness of my compile environment at
    compile time, instead of run time? I need a macro ("some_expressi on"),
    i.e.

    #if some_expression
    #define TARGET_IS_LITTL E_ENDIAN
    #else
    #define TARGET_IS_BIG_E NDIAN

    No way or some way? TIA.
  • Richard Tobin

    #2
    Re: Any Method to Determine Endianness at Compile Time ?

    In article <27c855f8-9938-49d6-9667-485205abfbc7@c1 9g2000prf.googl egroups.com>,
    perry.yuan <perry.yuan@gma il.comwrote:
    >How could I determine the endianness of my compile environment at
    >compile time, instead of run time?
    You can't write a compile-time expression to determine it, because
    endianness is a property of representations , not values, and there
    are no objects whose representations can be examined at compile
    time.

    One solution is to run a program at compile time that determines
    the endianness and outputs a suitable #define to a file which
    you then include.

    -- Richard
    --
    :wq

    Comment

    • Ben Pfaff

      #3
      Re: Any Method to Determine Endianness at Compile Time ?

      "perry.yuan " <perry.yuan@gma il.comwrites:
      How could I determine the endianness of my compile environment at
      compile time, instead of run time? I need a macro ("some_expressi on")
      One way this is commonly done is to build in two stages. First,
      compile and run a test program that tests for the
      implementation' s endianness. Then use the test program's output
      to configure macros to be defined while building the rest of the
      program.
      --
      "I've been on the wagon now for more than a decade. Not a single goto
      in all that time. I just don't need them any more. I don't even use
      break or continue now, except on social occasions of course. And I
      don't get carried away." --Richard Heathfield

      Comment

      • Eric Sosman

        #4
        Re: Any Method to Determine Endianness at Compile Time ?

        perry.yuan wrote:
        How could I determine the endianness of my compile environment at
        compile time, instead of run time? I need a macro ("some_expressi on"),
        i.e.
        >
        #if some_expression
        #define TARGET_IS_LITTL E_ENDIAN
        #else
        #define TARGET_IS_BIG_E NDIAN
        >
        No way or some way? TIA.
        This is Question 10.16 in the comp.lang.c Frequently
        Asked Questions (FAQ) list <http://www.c-faq.com/>. You've
        been around this newsgroup long enough to have seen the
        FAQ mentioned several dozens of times; shame on you for
        not bothering to read it.

        --
        Eric.Sosman@sun .com

        Comment

        • jacob navia

          #5
          Re: Any Method to Determine Endianness at Compile Time ?

          perry.yuan wrote:
          How could I determine the endianness of my compile environment at
          compile time, instead of run time? I need a macro ("some_expressi on"),
          i.e.
          >
          #if some_expression
          #define TARGET_IS_LITTL E_ENDIAN
          #else
          #define TARGET_IS_BIG_E NDIAN
          >
          No way or some way? TIA.
          #include <stdio.h>
          int main(void)
          {
          union {
          char c;
          int i;
          } u;
          u.i = 0;
          u.c = 1;

          if (u.i == 1)
          printf("little endian\n");
          else
          printf("big endian\n");
          }

          This printsd "little endian" in the intel processor,
          "big endian" in the power pc. I think it should work.



          --
          jacob navia
          jacob at jacob point remcomp point fr
          logiciels/informatique

          Comment

          • jacob navia

            #6
            Re: Any Method to Determine Endianness at Compile Time ?

            jacob navia wrote:
            perry.yuan wrote:
            >How could I determine the endianness of my compile environment at
            >compile time, instead of run time? I need a macro ("some_expressi on"),
            >i.e.
            >>
            >#if some_expression
            >#define TARGET_IS_LITTL E_ENDIAN
            >#else
            >#define TARGET_IS_BIG_E NDIAN
            >>
            >No way or some way? TIA.
            >
            #include <stdio.h>
            int main(void)
            {
            union {
            char c;
            int i;
            } u;
            u.i = 0;
            u.c = 1;
            >
            if (u.i == 1)
            printf("little endian\n");
            else
            printf("big endian\n");
            }
            >
            This printsd "little endian" in the intel processor,
            "big endian" in the power pc. I think it should work.
            >
            >
            >
            Sorry, I missed the "preprocess or" part.


            --
            jacob navia
            jacob at jacob point remcomp point fr
            logiciels/informatique

            Comment

            • Willem

              #7
              Re: Any Method to Determine Endianness at Compile Time ?

              jacob wrote:
              ) perry.yuan wrote:
              )How could I determine the endianness of my compile environment at
              )compile time, instead of run time? I need a macro ("some_expressi on"),
              ^^^^^^^ ^^^

              ) int main(void)
              ) {
              ) union {
              ) char c;
              ) int i;
              ) } u;
              ) u.i = 0;
              ) u.c = 1;
              )
              ) if (u.i == 1)
              ) printf("little endian\n");
              ) else
              ) printf("big endian\n");
              ) }

              Isn't that a run time solution ?


              SaSW, Willem
              --
              Disclaimer: I am in no way responsible for any of the statements
              made in the above text. For all I know I might be
              drugged or something..
              No I'm not paranoid. You all think I'm paranoid, don't you !
              #EOT

              Comment

              • Ben Pfaff

                #8
                Re: Any Method to Determine Endianness at Compile Time ?

                jacob navia <jacob@nospam.c omwrites:
                perry.yuan wrote:
                >How could I determine the endianness of my compile environment at
                >compile time, instead of run time? I need a macro ("some_expressi on"),
                >
                This printsd "little endian" in the intel processor,
                "big endian" in the power pc. I think it should work.
                I don't think you actually read the OP's question.
                --
                Ben Pfaff

                Comment

                • Richard Tobin

                  #9
                  Re: Any Method to Determine Endianness at Compile Time ?

                  In article <slrnfu2lm3.1uh 2.willem@snail. stack.nl>,
                  Willem <willem@stack.n lwrote:
                  >) int main(void)
                  >) {
                  >) union {
                  >) char c;
                  >) int i;
                  >) } u;
                  >) u.i = 0;
                  >) u.c = 1;
                  >)
                  >) if (u.i == 1)
                  >) printf("little endian\n");
                  >) else
                  >) printf("big endian\n");
                  >) }
                  >Isn't that a run time solution ?
                  Not if you run it at compile time :-)

                  -- Richard
                  --
                  :wq

                  Comment

                  • jacob navia

                    #10
                    Re: Any Method to Determine Endianness at Compile Time ?

                    Richard Tobin wrote:
                    In article <slrnfu2lm3.1uh 2.willem@snail. stack.nl>,
                    Willem <willem@stack.n lwrote:
                    >
                    >) int main(void)
                    >) {
                    >) union {
                    >) char c;
                    >) int i;
                    >) } u;
                    >) u.i = 0;
                    >) u.c = 1;
                    >)
                    >) if (u.i == 1)
                    >) printf("little endian\n");
                    >) else
                    >) printf("big endian\n");
                    >) }
                    >
                    >Isn't that a run time solution ?
                    >
                    Not if you run it at compile time :-)
                    >
                    -- Richard
                    I sent a message recognizing my error around 30 seconds
                    after I sent the first answer.

                    Again:

                    Excuse, it was a mistake


                    --
                    jacob navia
                    jacob at jacob point remcomp point fr
                    logiciels/informatique

                    Comment

                    • Morris Dovey

                      #11
                      Re: Any Method to Determine Endianness at Compile Time ?

                      "perry.yuan " wrote:
                      >
                      How could I determine the endianness of my compile environment at
                      compile time, instead of run time? I need a macro ("some_expressi on"),
                      i.e.
                      >
                      #if some_expression
                      #define TARGET_IS_LITTL E_ENDIAN
                      #else
                      #define TARGET_IS_BIG_E NDIAN
                      >
                      No way or some way? TIA.
                      Hmm. If you're willing to run another program before the compile,
                      you could run something like this from your compiler script or
                      makefile (although it need only be run once on any given target
                      machine):

                      /* Method of determining endian-ness is Jacob Navia's */
                      /* Adjust path and filename to suit your situation */

                      #include <stdio.h>
                      #include <stdlib.h>
                      int main(void)
                      { FILE *fp;
                      union
                      { char c;
                      int i;
                      } u;
                      u.i = 0;
                      u.c = 1;
                      if (fp = fopen("/usr/include/endian.h","w"))
                      { fprintf(fp,"#de fine %S_ENDIAN\n",(u .i&1)?"LITTLE": "BIG");
                      fclose(fp);
                      return EXIT_SUCCESS;
                      }
                      return EXIT_FAILURE;
                      }

                      then in your application just

                      #include <endian.h>

                      #ifdef BIG_ENDIAN
                      /* Big endian code */
                      #else
                      /* Little endian code */
                      #endif

                      --
                      Morris Dovey
                      DeSoto Solar
                      DeSoto, Iowa USA

                      Comment

                      • Robbie Hatley

                        #12
                        Re: Any Method to Determine Endianness at Compile Time ?


                        "perry.yuan " wrote:
                        How could I determine the endianness of my compile environment at
                        compile time, instead of run time?
                        Read the manuals for your CPU, operating system, and compiler.
                        I need a macro
                        You could use a macro to do conditional compilation, yes. Your compiler
                        might even provide you with endian-ness macros.

                        But why not do the checking at run-time instead? Your code would be
                        more portable that way. Read Jacob Navia's reply to your post.
                        I'd recommend that approach.

                        Another method I've seen is, use a char pointer to point to the 0th
                        byte of an int which is set to 65. If the char pointed to is "A",
                        you're little endian. If it's the NUL character instead, you're
                        big-endian.

                        --
                        Cheers,
                        Robbie Hatley
                        lonewolf aatt well dott com
                        www dott well dott com slant user slant lonewolf slant



                        Comment

                        • Richard Tobin

                          #13
                          Re: Any Method to Determine Endianness at Compile Time ?

                          In article <Gs6dnfgoF6415H zanZ2dnUVZ_j6dn Z2d@giganews.co m>,
                          Robbie Hatley <see.my.signatu re@for.my.email .addresswrote:
                          >How could I determine the endianness of my compile environment at
                          >compile time, instead of run time?
                          >Read the manuals for your CPU, operating system, and compiler.
                          This is a stupid, unhelpful answer. The aim is obviously to produce a
                          system that works on systems you don't necessarily have.
                          >But why not do the checking at run-time instead?
                          That is often sufficient, but is sometimes unacceptably inefficient
                          (or verbose, if you replicate large chunks of code). If efficiency is
                          important, arrange to run a program at compile time to determine the
                          answer.
                          >Another method I've seen is, use a char pointer to point to the 0th
                          >byte of an int which is set to 65. If the char pointed to is "A",
                          >you're little endian.
                          Why confuse the issue by bringing in ASCII codes?

                          -- Richard
                          --
                          :wq

                          Comment

                          • CBFalconer

                            #14
                            Re: Any Method to Determine Endianness at Compile Time ?

                            Richard Tobin wrote:
                            Willem <willem@stack.n lwrote:
                            >
                            >>int main(void) {
                            >> union {
                            >> char c;
                            >> int i;
                            >> } u;
                            >> u.i = 0;
                            >> u.c = 1;
                            >>>
                            >> if (u.i == 1)
                            >> printf("little endian\n");
                            >> else
                            >> printf("big endian\n");
                            >>}
                            >
                            >Isn't that a run time solution ?
                            >
                            Not if you run it at compile time :-)
                            On the cross-compiler !!

                            --
                            [mail]: Chuck F (cbfalconer at maineline dot net)
                            [page]: <http://cbfalconer.home .att.net>
                            Try the download section.


                            --
                            Posted via a free Usenet account from http://www.teranews.com

                            Comment

                            • Richard

                              #15
                              Re: Any Method to Determine Endianness at Compile Time ?

                              "Robbie Hatley" <see.my.signatu re@for.my.email .addresswrites:
                              "perry.yuan " wrote:
                              >
                              >How could I determine the endianness of my compile environment at
                              >compile time, instead of run time?
                              >
                              Read the manuals for your CPU, operating system, and compiler.
                              >
                              >I need a macro
                              >
                              You could use a macro to do conditional compilation, yes. Your compiler
                              might even provide you with endian-ness macros.
                              >
                              But why not do the checking at run-time instead? Your code would be
                              more portable that way. Read Jacob Navia's reply to your post.
                              I'd recommend that approach.
                              >
                              Another method I've seen is, use a char pointer to point to the 0th
                              byte of an int which is set to 65. If the char pointed to is "A",
                              you're little endian. If it's the NUL character instead, you're
                              big-endian.
                              And the reason you can't use an int or long of value 1 is?

                              Comment

                              Working...