user4108694
user4108694

Reputation:

Is it possible to run program on different operating systems with only one compile?

I don't know if this question was answered before, but I couldn't find any relative one.

My question is simple.

Given a simple C program that outputs which operating system we are in, is it possible to detect it on the fly and not recompile it?

We can use macros like _WIN32, _WIN64, linux, etc, but we have to compile the code every time on each system. Can we do it with only one compilation?

Example of a simple program:

#include <stdio.h>

#ifdef _WIN32
#define OS "WINDOWS"
#endif

#ifdef linux
#define OS "LINUX"
#endif

int main(void) {
    printf("%s\n", OS);
    return 0;
}

For instance we compile the above code with gcc in linux environment. Expected outputs will be OS dependent.

Linux : LINUX

Windows : WINDOWS

Note:

  1. We don't want and don't use Gygwin.
  2. We don't use wine.

Upvotes: 0

Views: 1928

Answers (5)

Grzegorz Szpetkowski
Grzegorz Szpetkowski

Reputation: 37904

It is not possible in a direct manner, as there are different executable formats on both platforms. Windows uses PE format, which is not compatible with Linux's ELF.

See also: Has anyone been able to create a hybrid of PE COFF and ELF?

As already pointed out, you would need some compatibility layer such as Wine or Cygwin.

Upvotes: 1

P.P
P.P

Reputation: 121347

In the general case, no, it's not possible.

But on POSIX systems, you can use uname(2).

Another option is to use macros for the specific Operating Systems you are interested in.

The following provides the list of macros that can be used to identify specific OS: https://sourceforge.net/p/predef/wiki/OperatingSystems/

While this list is exhaustive, it may not cover every existing Operating Systems that's out there. But it covers pretty much covers all the majors ones.

Upvotes: 0

Ivan Rubinson
Ivan Rubinson

Reputation: 3329

C and C++ get compiled in to native binaries. This, by definition, means they won't run on multiple platforms. Different platforms have a different format for their executables, and have a different ABI.

You can, however, set up your build system to cross-compile to multiple platforms when you build (which means that your toolchain will generate all of the different executables).

If what you're trying to do is to figure out which version of an OS you're on at runtime, maybe your OS has an API for that.

Upvotes: 1

Edik Mkoyan
Edik Mkoyan

Reputation: 359

I guess no, because there should be data for the OSes loader, so it will know how to create data structures in RAM, and as different OSes manage the memory differently the answer should be negative.

The trick you show, probably is suitable for simple programs, that do not use libraries like dotnet. You can make thing complicated and use conditional compilation tricks, I am not sure that is a good idea tough.

Upvotes: 0

Tony Tannous
Tony Tannous

Reputation: 14856

C binary executables are platform dependent. So it is a no.

Upvotes: 3

Related Questions