Build linux binaries on windows

Creating Linux binaries

Creating Linux binaries that can be downloaded and run on any distro (like .dmg packages for OSX or .exe installers for Windows) has traditionally been difficult. This is even more tricky if you want to use modern compilers and features, which is especially desired in game development. There is still no simple turn-key solution for this problem but with a bit of setup it can be relatively straightforward.

Installing system and GCC

First you need to do a fresh operating system install. You can use spare hardware, VirtualBox, cloud or whatever you want. Note that the distro you install must be at least as old as the oldest release you wish to support. Debian stable is usually a good choice, though immediately after its release you might want to use Debian oldstable or the previous Ubuntu LTS. The oldest supported version of CentOS is also a good choice.

Once you have installed the system, you need to install build-dependencies for GCC. In Debian-based distros this can be done with the following commands:

Then create a src subdirectory in your home directory. Copy-paste the following into install_gcc.sh and execute it.

Then finally add the following lines to your .bashrc .

Log out and back in and now your build environment is ready to use.

Adding other tools

Old distros might have too old versions of some tools. For Meson this could include Python 3 and Ninja. If this is the case you need to download, build and install new versions into

/devroot in the usual way.

Adding dependencies

You want to embed and statically link every dependency you can (especially C++ dependencies). Meson’s Wrap package manager might be of use here. This is equivalent to what you would do on Windows, OSX, Android etc. Sometimes static linking is not possible. In these cases you need to copy the .so files inside your package. Let’s use SDL2 as an example. First we download and install it as usual giving it our custom install prefix (that is, ./configure —prefix=$/devroot ). This makes Meson’s dependency detector pick it up automatically.

Building and installing

Building happens in much the same way as normally. There are just two things to note. First, you must tell GCC to link the C++ standard library statically. If you don’t then your app is guaranteed to break as different distros have binary-incompatible C++ libraries. The second thing is that you need to point your install prefix to some empty staging area. Here’s the Meson command to do that:

The aim is to put the executable in /tmp/myapp/bin and shared libraries to /tmp/myapp/lib . The next thing you need is the embedder. It takes your dependencies (in this case only libSDL2-2.0.so.0 ) and copies them in the lib directory. Depending on your use case you can either copy the files by hand or write a script that parses the output of ldd binary_file . Be sure not to copy system libraries ( libc , libpthread , libm etc). For an example, see the sample project.

Make the script run during install with this:

Источник

How to run Linux binaries natively in Windows 10

Windows Subsystem for Linux, explained

When Microsoft announced that they were bringing the Bash shell to the Windows 10 Anniversary Update via the Windows Subsystem for Linux (WSL) along with the open-sourcing of PowerShell, it meant that most of the tools that developers and sysadmins rely on were now non-proprietary and cross-platform. WSL addresses a unique paradigm — most of the web runs on Linux servers while Windows rules the enterprise space. Web developers have always struggled to move between platforms and often had to adapt to seemingly polarizing workflows. WSL aims to address this exact problem. By offering the ability to run native Ubuntu ELF64 binaries in a Bash shell invoked via the Command Prompt, Microsoft and Canonical (the company behind Ubuntu) are making it easy for developers to use familiar Linux-first tools such as apt, ssh, grep, awk, curl, mysql, ruby, perl etc. while still retaining their Windows deployment. It’s not just the availability of tools that makes WSL enticing but also the availability of immense literature, expertise, and coding that goes with these tools.

How different it is from Cygwin?

We all know Cygwin, which provides a POSIX API functionality within Windows by running packages that have been specifically recompiled for it. Till the announcement of WSL, this was a popular way of using Linux tools on Windows. Unlike Cygwin, WSL does not require any recompilation and can run a Linux distro and binaries as is.

Is it better than running a Linux VM?

Of course. WSL is neither a Linux virtual machine (VM) nor is it an emulator. Heck, it’s not even the actual Linux kernel. It is a Windows layer that translates Linux syscalls into Windows syscalls. In the words of Canonical’s Dustin Kirkland, Ubuntu on Windows is «bit-for-bit, checksum-for-checksum Ubuntu ELF binaries running directly in Windows». Think of it as the inverse of Wine in some sorts.

Читайте также:  Сбрасывается браузер по умолчанию windows 10

How does it benefit end-users?

While WSL has been made with developers in mind, end users can benefit from it just as well. Since WSL can be enabled even on Windows 10 Home, just about anyone can install and use it. WSL is perfect if you are interested in learning about Linux commands but are still wary of installing a dedicated distro on a partition or using a VM. Also, Linux has some neat free programs for those in education and science. With each iteration of Windows, we will see more and more compatibility of standard Linux programs with WSL.

Does it have any limitations?

It does. For one, you cannot run WSL on a 32-bit Windows machine. Even the Linux packages that can be installed from within the Bash shell are all 64-bit. WSL is intended to be mainly a command line toolset, therefore, Microsoft does not officially support graphics or multimedia output. Some other limitations include —

  • WSL also cannot access GPU hardware. That means no support for CUDA or OpenCL frameworks yet. However, there are now ways to get GUI apps and multimedia running by TCP/IP’ing the GPU output from Linux land to Windows land albeit with varying results.
  • I/O operations are still on the slower side compared to a Linux VM.
  • Since this is not a kernel per se, you cannot run apps that rely on the kernel such as VirtualBox, for example. Also, not all Linux syscalls are supported for translation at this time.
  • You still cannot run a server from Bash on Windows.
  • There are currently only a few distros available from the Microsoft Store but enthusiasts have found ways of using just about any Linux flavor with WSL (Arch Linux, anyone?).

Now that we’ve acquainted ourselves with what WSL is all about, let’s get down to business.

Step 1: Enable Developer Mode and WSL

Before you get to tinker with cowsay messages in your terminal, there are some basics that need to be sorted out. Firstly, enable Developer Mode by going to Settings > Update and Security > For Developers. If this is the first time Developer Mode is being turned on, you might have to wait for sometime for the feature to get activated. Then, in the Cortana search box, search for Turn Windows Features on or off (you might require Admin priveleges for this), scroll down and check the box against Windows Subsystem for Linux. You might be prompted to restart the computer for the changes to take effect.

Источник

Build linux binaries on windows

Statically linking and cross-compiling Rust apps on Windows

This example demonstrates how you can use just the tools that are readily accessible through rustup to statically link and cross-compile Rust apps on Windows.

Rust is compiled ahead-of-time to machine code that runs directly on an end-user’s machine. That means you have to know upfront what platforms you’re going to target and have the right build tools and libraries available for each of them.

Even when you do have compiled binaries, you can run into problems distributing them if you find yourself depending on the availability of C runtime libraries on the end-user’s machine.

When your build environment is Windows and you also need to target Linux, it turns out we can solve both our cross-compilation and distribution problems at once by statically linking MSVCRT for Windows and by cross-compiling our Linux builds to target musl instead of glibc.

Let’s start with a fresh Hello World Rust app:

Statically linking MSVCRT

If we build our library for the MSVC target (which is the default for Windows) now, it’ll dynamically link to MSVCRT:

In order to tell the Rust compiler to statically link MSVCRT, we need to add some configuration to a ./cargo/config file:

The crt-static target feature is a code generation option that’s only available for targets that are suitable for both static and dynamic linkage. MSVC is one of those targets. When crt-static is specified, the C runtime libraries will be linked statically instead of dynamically.

Building our app again results in a different binary:

It’s bigger than before because we have the relevant pieces of MSVCRT included.

Cross-compiling to Linux

For Windows, we statically link MSVCRT because it’s more convenient for end-users. The crt-static feature solves our distribution problem. For Linux, we’re going to statically link the musl libc because it’s more convenient for us at build time (and is more portable). musl is a complete, self-contained Linux libc with no system dependencies. That means we don’t have to provide Linux system libraries to dynamically link to in our Windows build environment. musl solves our cross-compilation problem.

We can install musl as a target for Rust using rustup :

Attempting to build right now probably won’t work though:

We’ve got the runtime we need, but not the build tools to link up our final Linux binary. Well, actually we do have the build tools we need. We’re just not using them yet. Rust embeds LLVM’s linker, lld , which we can use instead of the unavailable cc to link our Linux binary on Windows.

Читайте также:  Загрузка iso образа диска windows

Adding rust-lld as the linker for our musl target in our ./cargo/config file will switch from cc to Rust’s lld :

We should now be able to cross-compile a Linux binary from our Windows host:

  • You can’t directly or transitively depend on any libraries that need to compile C code. That includes the failure crate with its backtrace dependency. You can build some reasonably complex projects though, including this UDP server that depends on tokio .
  • musl binaries linked using LLD don’t seem to be able to recover from panics (there’s been problems with LLVM’s libunwind port that’s used in Rust’s musl target in the past).

Other approaches for cross-compilation

This example uses a combination of musl and LLD to cross-compile a Linux binary from Windows without needing any tools that aren’t readily available through rustup . Other approaches include:

  • Use LCOW to build your Linux binaries natively.
  • Use a separate Linux build agent.

They’re probably more robust, but depend on the availability of those features, or additional build complexity to coordinate the bundling of artifacts produced in separate environments. Azure Pipelines makes this coordination fairly straightforward though. I’ve got an example of building a native library (in this case LLVM itself) in Azure Pipelines on several platforms and packaging their artifacts together at the end here.

About

An example of cross compiling Rust apps to Linux on Windows, with only rustup

Источник

Build linux binaries on windows

Table of Contents

No Linux binaries are provided because of the differences between distributions. The usual approach is to build the cross-compiler on the host system and then re-use the prebuilt windows packages (since they are neutral to Linux systems anyway).

There are roughly three steps:

  • build yypkg
  • build the cross-compiler
  • acquire or build the windows binaries

The packages are split into three series: native, cross_toolchain, windows:

  • native adds new native tools on the build system that Slackware doesn’t have or has at a version that doesn’t match; for instance, it installs OCaml because building the Ocaml cross-compiler requires an Ocaml native compiler of the exact same version
  • cross_toolchain builds the cross-toolchain
  • windows cross-compiles the binaries that will run on windows (this includes the GCC native compiler)

Building takes a fairly large amount of space, roughly 500MB for native_toolchain , 2GB for cross_toolchain_* and 4.5GB for windows_* . Around 90% of this space is made up of temporary files in tmp directories.

1.В Dependencies

The per-distributions package lists below are here to help you get all the dependencies easily. The Debian/Ubuntu list is exhaustive and is the reference. For other distributions, if you are able to complete the lists with the right package name, please send it either on the users mailing list or on the bug tracker.

Источник

How to Compile Linux Programs Under Windows with Cygwin

Windows and Linux are two very different systems, and as such, it often isn’t easy to port programs written for one to the other, especially when dealing with GUI programs. Although there are many different cross-platform libraries and SDKs, native programs written without portability in mind are quite hard to port.

When it comes to compiling and running programs written for Linux on Windows, there is a solution known as Cygwin. The Cygwin project is a collection of the most common tools and compilers (including the bash shell and the GNU compiler chain) for Windows. It also includes a library that provides a compatibility layer so that programs which call Linux specific APIs can be compiled. Cygwin isn’t an emulator or virtual machine, and it doesn’t allow Linux binaries to run on Windows without first being re-compiled.

Visit the Cygwin installation page and download the 32-bit or 64-bit setup executable (depending on which variant of Windows you are using). Execute the setup program. Click Next and Next again (to “Install from Internet”). The default directory is “C:\cygwin”. It can be changed if needed, but unless you have a specific reason to change it, the default is best. Click Next, Next and Next again.

The Cygwin project has mirror sites all over the world; pick one which you think will best serve your location and click Next. You now need to pick which packages to install. To compile simple Linux programs in Windows, you will need the GNU Compiler Chain (GCC) which provides a C and C++ compiler.

Type “gcc” in the search box and then click in the small plus sign next to “Devel” in the list of packages. Find “gcc-core” and “gcc-g++” and click “Skip” for each one. The word “Skip” will change into a version number and the “n/a” sign in the “Bin?” column will turn into a checked box. Type “make” in the search box and find “make” under “Devel.” Click “Skip” to mark it for install. Search for “wget” and also mark it for install from “Web.” To build the example below, we will also need “libiconv;” search for it and mark it for install.

Читайте также:  Dark theme firefox linux

Click Next. The installer will then see what other packages need to be installed to resolve any dependencies. Click Next to accept the recommendations.

Once all the packages have been downloaded and installed, follow the last steps until the installer exits. Start the “Cygwin Terminal” to enter into the Linux-like development environment. In the terminal you don’t use Windows commands like “dir” but rather shell commands like “ls”.

To demonstrate how to compile a Linux program under Windows, we will use the HTML-XML package from the W3. For a look at what it can do, see How to Manipulate HTML and XML Files from the Command Line.

Download the source files using “wget”:

Now unpack the archive file:

The source files are now in the “html-xml-utils-6.7” directory. Enter that directory:

Before the files can be built, you need to run the “configure” shell script to generate the Makefile (the build instructions) which are suitable for this build environment. This is a common step on Linux (and Cygwin) when building packages from source.

Once “configure” has finished, you can start the build using “make”:

The build will fail part way through. I was in two minds about what to do next. Either I could switch to another project and build that from its source or battle on with the HTML-XML-utils. I opted for the latter as it shows that not everything will be a walk-in-the-park when trying to compile Linux programs under Cygwin. The solution to this particular problem is simple. The error message shows that the linker is unable to find the “iconv” library. A quick look at the link command shows that the library isn’t specified. The quick and dirty solution is to run the command manually and tell the linker to use libconv. The “proper” way to fix this would be to start delving into the Makefile etc. to find out why it isn’t working.

Run the following command, noting the inclusion of “-liconv” at the end:

Once the “hxindex.exe” file is built, you can continue with the rest of the build by typing “make” again. The way “make” works is it checks what has and has not been built, and then it continues the build process at the appropriate point. Since we have manually built “hxindex.exe”, “make” just carries on with the next binary in its list.

When “make” completes, you will have all the .exe files in the html-xml-utils-6.7 directory.

If you get stuck using Cygwin you should look at the FAQ, and at the documentation. Failing that, the project has a set of mailing lists. If you have any problems with the steps described above, then please use the comments section below.

Gary has been a technical writer, author and blogger since 2003. He is an expert in open source systems (including Linux), system administration, system security and networking protocols. He also knows several programming languages, as he was previously a software engineer for 10 years. He has a Bachelor of Science in business information systems from a UK University.

4 comments

Did you try to run the executable file on some variant of Linux ? I think you just built a program which depends on Cygwin’ DLL (cygwin1.dll or cygwin.dll) or a Cygwin package in other words.

Programs compiled under Cygwin don’t run under Linux. Cygwin is designed to allow Linux programs to be compiled under Windows to run under Windows.

When I try to run ./configure I get all the way up to here:

checking for library containing iconv_open… no
configure: error: no iconv library found, will be unable to compile

and then for some reason it can’t find my libiconv installation.
Its definitely there. I have an iconv.exe in my bin directory and iconv.h in my usr/include directory.
In the iconv.h file I found these lines:

#ifndef LIBICONV_PLUG
#define iconv_open libiconv_open
#endif
extern iconv_t iconv_open (const char* tocode, const char* fromcode);

Could the #ifndef LIBICONV_PLUG part be stopping it from defining iconv_open? I don’t think that’s the case since I can still run iconv_open() from the terminal and it finds something.

thanks for the guide, have been using cygwin for only about two weeks and just so happens i was looking for html-xml-utils :p

Comments are closed.

RedMagic 6S Pro Review: Gaming Is Serious Business.

How to Boot to Recovery Mode (Safe Mode) in Ubuntu

Ubuntu Software Center Not Working? Here Are the Fixes

20 Awesome Screensavers for Windows 10

How to Share Files Between Android and Windows on Your Network

How to Install and Run the Nginx Server on Windows

What You Should Do If Windows Fails to Start

How to Stress Test a Graphics Card on Linux

10 of the Best Terminal Emulators for Windows

15 Useful Windows Registry Hacks to Optimize Your Experience

Windows 10 Start Menu Search Not Working? Here Are 12 Fixes

Affiliate Disclosure: Make Tech Easier may earn commission on products purchased through our links, which supports the work we do for our readers.

Источник

Оцените статью