Sign in to follow this  

How can I find if I'm my program is being compiled on linux?

This topic is 4034 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Guest Anonymous Poster
Quote:
Original post by Deliverance
I wrote a program and I want it to be compatible with linux and windows also. I want to know how can I find if the program is running on windows or linux?
#ifdef -> what's the directive?

:)


do you want to know whether a program is being COMPILED under Linux or being RUN?


google: PRE PROCESSOR MACROS

will point you to a site that contains platform specific defines that are automatically set by the compiler.


Share this post


Link to post
Share on other sites
Quote:
Original post by Deliverance
I wrote a program and I want it to be compatible with linux and windows also. I want to know how can I find if the program is running on windows or linux?
#ifdef -> what's the directive?

:)


This is like asking "how do I find out whether I breathe water or air?". The program has to be *compiled* separately for Windows or Linux; by the time it's running, it's too late to "detect" anything meaningfully - the Linux build of your program will "know" it's on Linux, by virtue of the fact that it's running at all.

That said, there are many #defines that may be relevant (although you still have to either transfer the code to a Linux machine and compile it there, or find a "cross-compiler"), and you should probably try to [google] it first. It will depend on what compilers you are using, as well (each compiler generally provides its own #defined symbol, which "identifies" the compiler to the preprocessor).

Share this post


Link to post
Share on other sites
GCC on GNU/Linux systems will define __linux__. Obviously, that won't be defined by GCC on Windows, *BSD, VMS, etc. Other C(++) compilers may or may not define __linux__ on GNU/Linux systems. Gcc will always define __GNUC__. Additionally, g++ will always define __GNUG__.

This a nice page about the GNU C preprocessor. You can get a list of all the symbols defined by GCC with:

$ gcc -dM -E - < /dev/null
# OR (on systems that have pipes and echo,
# but don't have /dev/null (aka Windows))
$ echo | gcc -dM -E -


If I may, I'd like to add that relying on such tricks isn't a good idea. Unless your software is indeed Linux-specific — meaning that it requires the Linux kernel — you're going to artificially limit your software to GCC on GNU/Linux, even though it might very well compile on, say, FreeBSD with ICC, or on SunOS with the Comeau C++ compiler. Testing for the presence of functionalities at build time — like the configure scripts created by the autoconf tool — is much less limiting.


Hope this helps.

Share this post


Link to post
Share on other sites
#if defined(_WIN32) || defined(_WIN64) || defined(__WIN32__) || defined(WIN32)
// Windows Specific Code goes here
#else
// POSIX(Linux,FreeBSD,Darwin?) code goes here
#endif

In general, if you stick to POSIX code and cross compatible libraries, you shouldn't even need that, reasons to split the code that way that come to mind are Multithreading (though there is a windows version of pthreads) and using Windows Registry functions.

Share this post


Link to post
Share on other sites

This topic is 4034 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this