Click here to Skip to main content
15,901,283 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I need to do conditional compilation in a c++ program based on the different version of RedhatLinux for example for RHEL 5.5 I need to define some macros and for RHEl 6.1 these macros will have different values.
Like,

C++
#ifdef LINUX
    #define MY_STRING "abc"
#else
    #define MY_STRING "def"
#endif


but "#ifdef LINUX" is for all the versions of RHEL.

So can any body plz help me how to get this done.

My requirement is as such for RHEL 6.1 and RHEL 5.5 I need to declare some preprocessor directives like
So plz help in determining different versions of RHEL.

Thanks,
Dev.
Posted
Updated 22-May-12 10:47am
v2

The easiest way would be to add a dynamic definition at buid time, either in your gcc call or in your Makefile.
 
Share this answer
 
It's a real bummer trying to work out versions on Linux. So don't even try. Really, it ain't worth it. I know, what a crap answer, what are you supposed to do? The trouble is Linux systems, even from the same distro CD can be completely different from each other. Different versions of libraries, even different libraries full stop.

So what I'd do is work out what features you need from the platform and check to see if they exist. A feature is something you want the code to do that's different on various platforms - an installed library, function in an installed library or a system call are all things that can be features.

Either:

- Use AC_CHECK_FUNC so you can test that a particular function is there when you configure your code for a particular platform

- write little programs to test the behaviour of the functions you want and use AC_RUN_IFELSE to set preprocessor defines or even the macros themselves.

Now you might be sitting there going "Huh, what's he wittering on about?" If you are I'm talking about autotools, a good way of generating configure scripts for programs that run on UNIXy style systems. They enable you to write a couple of relatively simple text files that'll generate the make file for you on just about any system. Cool or what?

They can be a bugger to use but if you're serious about programming on Linux then have a look at the Goat Book[^] which describes how to use the autotools. It's worth spending a while to learn how to use them. This Wikipedia article[^] gives a good overview of how the tools fit together.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900