Conditional compilation

Conditional compilation

Post by e.. » Thu, 08 Jun 2000 04:00:00



#ifdef unix
#include <whatever_unix.h>
#else
#include <whatever_dos.h>

-Kevin

 
 
 

Conditional compilation

Post by e.. » Thu, 08 Jun 2000 04:00:00


And don't forget

#endif

Damn i need sleep...

-Kevin

 
 
 

Conditional compilation

Post by srinivas_vanjar » Fri, 09 Jun 2000 04:00:00


Hello,

 I have a set of C functions written on Linux OS. I want to port the
same functions to Windows environment.
 The code in the functions is not OS dependent, but if I use VC++ for
compiling the code, the header files are
 different. (For example VC++ needs memory.h for malloc()).

 I want to create a code such that the code can be compiled on both WIN
and linux.

 I want to try something like this

 #ifdef  THE OS IS WINDOWS
 #include <xxx.h>
 #endif

 #ifdef THE OS IS LINUX
 #include <yyy.h>
 #endif

 Can somebody let me know what to put in place of THE OS IS WINDOwS and
THE OS IS LINUX. Please let me know if the idea works. How is this
normally done ?

I am using cc/gcc for linux and VC++ for windows.

 Any help in this regard will be truly appreciated.
--
regards,
srinivas_vanjari,


Sent via Deja.com http://www.deja.com/
Before you buy.

 
 
 

Conditional compilation

Post by Stephan Erbs Korshol » Wed, 14 Jun 2000 04:00:00


Hello,

You wrote,

  "I want to create a code such that the code can be compiled on both
   WIN and linux.

   I want to try something like this

   #ifdef  THE OS IS WINDOWS
   #include <xxx.h>
   #endif

   #ifdef THE OS IS LINUX
   #include <yyy.h>
   #endif"

As others have mentioned you can do this by defining a variable in
your makefiles based on some system command that will give you the OS
type.

I would like to recommend another approach to cross platform
compilation though. My experience suggests that you should not use
preprocessor directives, like in the example above, to separate
platform specific code from paltform independent code. It renders the
file unreadable and very ugly in the end. Instead you should have one
file containing code that is platform independent and then a set of
files, one for each platform, that contains the platform specific
parts. Then let the makefile choose whichever subset of your files it
needs for a certain platform. This gives a much cleaner interface, and
a better design. Additionally you are forced to separate out the
algorithm itself or in other words the core parts of your logic into
the platform independent file, and all the trivial stuff into the
platform dependent files. So, you actually get to understand the
problem better by using this approach.

Regards,

/Stephan

 
 
 

1. Conditional compilation in C?

Hi, wonder if anyone can advice. I've got a C program with two (yes two!)
main() functions. During compilation I want only want part to compile
depending on what has been passed to gcc.

I've heard that I should be able to do something like

        #ifdef SECTION_1
        int main(int argc, char *argv)
        {
                /* First version of main() */
                .......
        }
        #endif

        #ifdef SECTION_2
        int main(int argc, char *argv)
        {
                /* Second version of main() */
                .......
        }
        #endif

and during compile I should be able to do something like
        gcc -DSECTION_2 progname.c -o progname
to compile SECTION_2 only but I've not been very successful. Any ideas?
Thanks.

2. auto logout - untamo

3. Sys V/BSD "ps" newbie g++ preprocessor conditional compilation?

4. Connecting from linux console to aix; please help

5. conditional compilation

6. US-TN-Memphis Seeking Competent UNIX System Admin with TCP/IP and Security "C" Experience Fortune126

7. make & conditional compilation

8. HOW-TO : undelete linux files (ext2fs/debugfs)

9. make: conditional compilation/linking?

10. Assembler and Conditional Compilation (C Preprocessor)

11. conditional compilation

12. YACC parser & conditional compilation directives

13. automake conditional compilation technicality