How could I determine the endianness of my compile environment at
compile time, instead of run time? I need a macro ("some_expression"),
i.e.
#if some_expression
#define TARGET_IS_LITTLE_ENDIAN
#else
#define TARGET_IS_BIG_ENDIAN
No way or some way? TIA. 19 6824
In article <27**********************************@c19g2000prf. googlegroups.com>,
perry.yuan <pe********@gmail.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
"perry.yuan" <pe********@gmail.comwrites:
How could I determine the endianness of my compile environment at
compile time, instead of run time? I need a macro ("some_expression")
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
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_expression"),
i.e.
#if some_expression
#define TARGET_IS_LITTLE_ENDIAN
#else
#define TARGET_IS_BIG_ENDIAN
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.
-- Er*********@sun.com
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_expression"),
i.e.
#if some_expression
#define TARGET_IS_LITTLE_ENDIAN
#else
#define TARGET_IS_BIG_ENDIAN
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 http://www.cs.virginia.edu/~lcc-win32
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_expression"), i.e.
#if some_expression #define TARGET_IS_LITTLE_ENDIAN #else #define TARGET_IS_BIG_ENDIAN
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 "preprocessor" part.
--
jacob navia
jacob at jacob point remcomp point fr
logiciels/informatique http://www.cs.virginia.edu/~lcc-win32
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_expression"),
^^^^^^^ ^^^
) 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
jacob navia <ja***@nospam.comwrites:
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_expression"),
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 http://benpfaff.org
In article <sl********************@snail.stack.nl>,
Willem <wi****@stack.nlwrote:
>) 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
Richard Tobin wrote:
In article <sl********************@snail.stack.nl>,
Willem <wi****@stack.nlwrote:
>) 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 http://www.cs.virginia.edu/~lcc-win32
"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_expression"),
i.e.
#if some_expression
#define TARGET_IS_LITTLE_ENDIAN
#else
#define TARGET_IS_BIG_ENDIAN
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,"#define %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 http://www.iedu.com/DeSoto/
"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
In article <Gs******************************@giganews.com>,
Robbie Hatley <se**************@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
Richard Tobin wrote:
Willem <wi****@stack.nlwrote:
>>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
"Robbie Hatley" <se**************@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?
user923005 wrote:
We have to compiler for dozens of platforms here. We just figure out
the endianness before hand and compile with an appropriate macro
definition.
That would be my approach, as well - but I don't generally find
myself compiling here for other platforms. My normal mode of
operation has been to code, compile, and (at least) unit test on
this (nearly ancient) machine - then recompile and test on the
target machine. Whenever a cross-compiler has been needed, it's
always been available at the client's site.
Heh - I just realized that this is the same machine I first used
to read this group. I guess that makes it a "keeper". :-)
--
Morris Dovey
DeSoto Solar
DeSoto, Iowa USA http://www.iedu.com/DeSoto/
There is no way in Standard C. But read the manual(s) for all
compilers that you are using. Most compilers will provide the
information that you need in some form. Write a header file that
checks for all compilers that you can identify and defines the macro
that you want, and compiles a #error statement if the compiler is not
one that you can identify.
"sr******@gmail.com" <sr******@gmail.comwrites:
[...]
>How could I determine the endianness of my compile environment at compile time, instead of run time? I need a macro ("some_expression"), i.e.
>#if some_expression #define TARGET_IS_LITTLE_ENDIAN #else #define TARGET_IS_BIG_ENDIAN
[...]
The above (starting with "How could I determine") was written by
"perry.yuan" <pe********@gmail.com>. The attribution was lost because
Gordon Burditt, as usual, deliberately deleted it.
Probably declaring the option in MAKEFILE as a switch will take care
to compile for BIG-ENDIAN or LITTLE-ENDIAN. CISC are LE type whereas
RISC are BE type, one has to choice the option and then build which
takes care at compile time.
I don't believe there any correlation between big-endian
vs. little-endian and RISC vs. CISC. As far as I know, all four
combinations exist.
The best approach, if it's practical, is to write your code so it
doesn't matter whether the platform is big-endian or little-endian.
(This may not always be practical.)
(I do not grant permission to quote this, or anything else I write
here, without attribution.)
--
Keith Thompson (The_Other_Keith) <ks***@mib.org>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
(I do not grant permission to quote this, or anything else I write
here, without attribution.)
Noted.
--
Richard Heathfield :-) <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Richard Heathfield <rj*@see.sig.invalidwrites:
>(I do not grant permission to quote this, or anything else I write here, without attribution.)
Noted.
Retroactive exemptions may be granted if the response is sufficiently
humorous.
--
Keith Thompson (The_Other_Keith) <ks***@mib.org>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister" This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Lenard Lindstrom |
last post by:
I was wondering if anyone has suggested having Python determine
a method's kind from its first parameter. 'self' is a de facto
reserved word; 'cls' is a good indicator of a class method
( __new__...
|
by: Kapil Khosla |
last post by:
Dear all,
I am trying to underlying implementation of virtual functions in C++.
The way I understand polymorphism is
class Base
{
public:
virtual int func();
};
|
by: kelvSYC |
last post by:
Are there any endianness concerns in C++, or does the compiler take
care of those details? I ask because I'm not sure if code such as the
following have consistent behavior on all platforms.
...
|
by: SSM |
last post by:
Hi,
Does C standard comment about "Endianness" to be used
to store a structure/union variables?
Thanks & Regards,
Mehta
|
by: Jeff User |
last post by:
Here is what I have and what I would like to do:
I am building a web client application.
I add a web reference to some web service server to my project so that
now I can call the web service.
A...
|
by: friend.05 |
last post by:
Code to check endianness of machine
|
by: jacob navia |
last post by:
Hi
Suppose you have somewhere
#define BOOL int
and somewhere else
typedef BOOL int;
|
by: gg9h0st |
last post by:
i really wander what makes static method special?
in fact i can access a non static method statically using '::'
class aclass {
function anonstatic() {
echo 'non static';
}
static...
|
by: Indian.croesus |
last post by:
Hi,
If I am right Endianness is CPU related. I do not know if the
question is right in itself but if it is then how does C handle issues
arising out of Endianness.
I understand that if we pass...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 2 August 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM)
The start time is equivalent to 19:00 (7PM) in Central...
|
by: linyimin |
last post by:
Spring Startup Analyzer generates an interactive Spring application startup report that lets you understand what contributes to the application startup time and helps to optimize it. Support for...
|
by: erikbower65 |
last post by:
Here's a concise step-by-step guide for manually installing IntelliJ IDEA:
1. Download: Visit the official JetBrains website and download the IntelliJ IDEA Community or Ultimate edition based on...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Sept 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM)
The start time is equivalent to 19:00 (7PM) in Central...
|
by: Taofi |
last post by:
I try to insert a new record but the error message says the number of query names and destination fields are not the same
This are my field names
ID, Budgeted, Actual, Status and Differences
...
|
by: DJRhino1175 |
last post by:
When I run this code I get an error, its Run-time error# 424 Object required...This is my first attempt at doing something like this. I test the entire code and it worked until I added this -
If...
|
by: DJRhino |
last post by:
Private Sub CboDrawingID_BeforeUpdate(Cancel As Integer)
If = 310029923 Or 310030138 Or 310030152 Or 310030346 Or 310030348 Or _
310030356 Or 310030359 Or 310030362 Or...
|
by: lllomh |
last post by:
Define the method first
this.state = {
buttonBackgroundColor: 'green',
isBlinking: false, // A new status is added to identify whether the button is blinking or not
}
autoStart=()=>{
|
by: lllomh |
last post by:
How does React native implement an English player?
| |