Cosmopolitan, a standard C library with a cross-platform executable


The first version has just been released of the project Cosmopolitan which stands out for developing a standard C library and a universal executable file format that can be used to distribute programs for different operating systems.

The container for the formation of universal executable files is based on the combination of specific segments and headings for different operating systems (PE, ELF, MALE, OPENBSD) in a single file.

To guarantee the launch a single executable file on Windows and Unix systems, a trick whose essence is the encoding of Windows PE files is used in the form of a shell script, taking advantage of the fact that the Thompson Shell does not use the "#!" script marker.

Cosmopolitan makes C a language that can be built once and can be run anywhere, similar to Java, except that it does not require the prior installation of interpreters or virtual machines. Cosmo offers the same portability benefits as high-level languages ​​like Go and Rust, but it doesn't invent a new language and you won't need to configure a CI system to create separate binaries for each operating system.

What Cosmopolitan focuses on is fixing C by decoupling it from platforms, so it can be nice to use for writing small Unix programs that are easily distributed to a much wider audience.

As a result, an executable file is created in which several different formats are combined used on Unix, Windows and macOS. and an outline of the proposed format.

The file begins with the tag "MZqFpD", which is interpreted as a header in Windows PE format.

For example, on Unix systems, the shell code run by exec command, passing executable code through an unnamed pipe.

Though it is mentioned that there is a limitation of the proposed method is the ability to run on a Unix-like operating system using only shells that support Thompson Shell compatibility mode, otherwise it will not start directly with the system call.

The call qemu-x86_64 is intended to provide additional portability and allows you to run compiled x86_64 code on non-x86 platforms such as Raspberry Pi boards and Apple devices with ARM processors.

The project can also be used to create standalone applications that work without an operating system (bare metal). In such applications, a loader is attached to the executable file and the program acts as a bootable operating system.

In the C libc standard library, developed by the project, about 1400 functions have been proposed for creating console applications.

In terms of performance, Cosmopolitan works as fast as glibc and is notably ahead of Musl and Newlib, whereas Cosmopolitan is an order of magnitude smaller in code size than glibc and roughly matches Musl and Newlib.

To optimize frequently called functions such as memcpy and strlen, the technique in which a macro binding is used to call the function is additionally used, in which the compiler is informed about the CPU registers involved in the execution of the code, which which saves resources by saving CPU state by storing only mutable registers.

To create programs that include multiple files (linking all resources in a single file), the formation of a universal executable file in the form of a ZIP file is supported specially crafted (content can be viewed with the "unzip -v" command, but the file remains available for direct launch).

The file obtained by static linking with the proposed libc and runtime is suitable for running on any Linux distribution, macOS, Windows, FreeBSD and OpenBSD. The project code is distributed under the ISC license (simplified version of MIT / BSD).

Finally if you are interested in knowing more about it about this library, you can check the details, as well as the documentation In the following link.

The content of the article adheres to our principles of editorial ethics. To report an error click here!.

Be the first to comment

Leave a Comment

Your email address will not be published.



  1. Responsible for the data: AB Internet Networks 2008 SL
  2. Purpose of the data: Control SPAM, comment management.
  3. Legitimation: Your consent
  4. Communication of the data: The data will not be communicated to third parties except by legal obligation.
  5. Data storage: Database hosted by Occentus Networks (EU)
  6. Rights: At any time you can limit, recover and delete your information.