regarding using env variable

I

invincible

Hi

I have program

void main()
{
#ifdef env_var
printf(" I am in Linux");
#endif
}

I am using solaris and Linux platform to compile this code. When
i run it on solaris I sould not get any message. But when i run it on
linux i should get "I am in Linux".

This one I can achieve using -dLinux while compiling it on linux

But I dont want to use any variables , is there any inbuilt environment var
which i can use for my required task.

Thanks

Mohan
 
W

Walter Roberson

I have program
void main()
{
#ifdef env_var
printf(" I am in Linux");
#endif
}
This one I can achieve using -dLinux while compiling it on linux
But I dont want to use any variables , is there any inbuilt environment var
which i can use for my required task.

No. The C standard does not require the consultation of any environment
variables at compile time.

That having been said: if you tell your compiler to show all the phases
as it compiles, you will often find that each different environment
automatically -D's different preprocessor tokens. For example, on
the machine I am on now, in the default compile mode I used,

-Dmips -DMIPSEB -D_MIPSEB -D_PIC -D__DSO__ -D__EXTENSIONS__ \
-D__INLINE_INTRINSICS -D__MATH_HAS_NO_SIDE_EFFECTS -DLANGUAGE_C -Dunix \
-Dsgi -Dhost_mips -D_SGI_SOURCE -D_LONGLONG -D_SVR4_SOURCE \
-D_LANGUAGE_C -D_MODERN_C -D__sgi -D__host_mips -D_SYSTYPE_SVR4 \
-D__unix -D_COMPILER_VERSION=730 -D__mips=3 -D_MIPS_ISA=3 -D_ABI64=3 \
-D_MIPS_SIM=_ABI64 -D_MIPS_FPSET=32 -D_MIPS_SZINT=32 -D_MIPS_SZLONG=64 \
-D_MIPS_SZPTR=64 -D_SIZE_INT=32 -D_SIZE_LONG=64 -D_SIZE_PTR=64

The exact set of tokens will be dependant on the system you are on,
on the compiler you are using, on the compiler version, and upon
compilation flags.

Note, by the way, that a preprocessor token definition is NOT an
environment variable.
 
B

Bill C

invincible said:
Hi

I have program

void main()
{
#ifdef env_var
printf(" I am in Linux");
#endif
}

I am using solaris and Linux platform to compile this code. When
i run it on solaris I sould not get any message. But when i run it on
linux i should get "I am in Linux".

This one I can achieve using -dLinux while compiling it on linux

But I dont want to use any variables , is there any inbuilt environment var
which i can use for my required task.

Thanks

Mohan

I am not sure about a flag available to the compiler, but it can be
done during runtime.

#include <string.h>
#include <cstdlib.h.
char *ostype;
ostype = getenv("OSTYPE");
if (strcmp(ostype,"linux")==0) printf("I am in Linux");




Cheers,
Bill C.
 
C

CBFalconer

invincible said:
I have program

void main()
{
#ifdef env_var
printf(" I am in Linux");
#endif
}

I am using solaris and Linux platform to compile this code. When
i run it on solaris I sould not get any message. But when i run it on
linux i should get "I am in Linux".

This one I can achieve using -dLinux while compiling it on linux

But I dont want to use any variables , is there any inbuilt environment var
which i can use for my required task.

Who knows? This is not a C question. Ask in groups covering your
particular systems.

At any rate, the above program has un (or implementation) defined
behavior. void main is only allowed to adherents of BullSchildt
books.
 
R

Richard Bos

I am not sure about a flag available to the compiler, but it can be
done during runtime.

#include <string.h>
#include <cstdlib.h.
char *ostype;
ostype = getenv("OSTYPE");
if (strcmp(ostype,"linux")==0) printf("I am in Linux");

That may work for Linux, but it's hardly a general solution. In fact, it
causes undefined behaviour on my machine.
The usual solution is to do what invincible says he doesn't want to do:
have the makefile (which will probably need to be somewhat different for
most targets anyway) define a constant.

Richard
 
B

Bill C

Richard said:
That may work for Linux, but it's hardly a general solution. In fact, it
causes undefined behaviour on my machine.
The usual solution is to do what invincible says he doesn't want to do:
have the makefile (which will probably need to be somewhat different for
most targets anyway) define a constant.

You are correct, my solution isn't general. In fact, I wasn't trying
to be general. The OP asked for a solution on 2 specific platforms. I
showed him one way to confirm that he is running on one of those 2. I
probably should have checked it out on Solaris to make sure it wouldn't
crash, but I figured he could do that much himself.

Your point is well taken, and I should have noted that this is a
potential, specific, solution to his specific problem.

Cheers,
Bill C.
 

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

No members online now.

Forum statistics

Threads
474,161
Messages
2,570,892
Members
47,427
Latest member
HildredDic

Latest Threads

Top