Everyone
Everyone

Reputation: 1825

Are statically compiled pure standard C++ programs portable provided running on the same architecture?

When a program is recursively statically compiled (assuming all linked libraries allow static compilation and are kind-of platform independent in their code, I know this is possible because I managed to statically compile nano from Ubuntu to run on x86 Android and it worked because it didn't link to system libraries), is it really safe to assume it is self-contained? That is, does it need anything else from the platform (i.e. Linux, Windows, ..., etc)? I suspect the answer is yes. So accordingly...

For simplicity, if I take this program (no dependencies):

int main(){
      return 0;
}

I can easily compile this statically (since obviously it has no dependencies) and should produce native code that runs on the underlying architecture (say x86 architecture).

I know there are wrappers that would pass environment variables and other things that will differ depending on the platform (e.g. Windows exe or Linux executable).

Putting the wrappers aside for a minute, the question is: if I compiled the previous program statically using GCC on Linux, would I theoretically be able to run it on Windows?

Back to wrappers, if I know how to convert the wrappers from Windows to Linux and vice-versa, would I need to recompile the program to move it from one to another?

Upvotes: 0

Views: 366

Answers (4)

Alexey B.
Alexey B.

Reputation: 1156

Theoretically, yes. The other way around (run a Windows executable in Linux) has been possible for quite a while using Wine. Now, with Windows Subsystem for Linux, I think you can also run Linux executables in Windows 10.

Upvotes: 0

SergeyA
SergeyA

Reputation: 62583

No, even completely statically built binaries for C++ would not cross-platform. Even the simplest program

int main() {
}

Will have CRT startup code linked into it (statically). And that CRT will have not insignificant amount of OS-dependent API calls (an exit syscall to name the one which immediately comes to mind, but of course many more).

Sorry, but you will have to either cross-compile or compile natively on the target platform

Upvotes: 1

John Bollinger
John Bollinger

Reputation: 180351

is it really safe to assume it is self-contained? That is, does it need anything else from the platform (i.e. Linux, Windows, ..., etc)? I suspect the answer is yes.

You would be right. A program built for use in an operating system relies on the services of the host operating system, via OS interfaces that are specific to that system. Among the more fundamental of those are the facilities for loading and starting programs, which handle only specific program formats. The formats supported by Windows and those supported by Linux do not overlap.

if I compiled the previous program statically using GCC on Linux, would I theoretically be able to run it on Windows?

No, not directly. But you could run it in a Windows Subsystem for Linux container, which provides a Linux environment to binaries running inside.

if I know how to convert the wrappers from Windows to Linux and vice-versa, would I need to recompile the program to move it from one to another?

To run the program natively, you need to build it separately for each target OS. You might even need to build it separately for different versions of the OS. Full static compilation reduces the surface area for the latter need, but it does not address cross-platform issues.

Upvotes: 1

Eric Postpischil
Eric Postpischil

Reputation: 222908

The normal user programs you are familiar with are embedded in an executable file format. Loading the program from such a file into memory requires operating system services that depend on the specific format used.

Once loaded, programs use requests to the operating system to perform fundamental operations, such as reading input, writing output, and terminating the program. These cannot be built into the program because normal user programs do not have permission to access the hardware features needed to implement them.

It is of course possible to write software that does not depend on an operating system. An example is operating systems themselves: They provide all their own functions. However, they must be executed on the bare hardware (or a virtual environment mimicking it) and must be built to be loaded by the boot loader for the hardware.

Upvotes: 3

Related Questions