Compiling for linux on windows

Run C program written in Linux on Windows

I have C program which I wrote in Linux that runs very well. Now I want to run it on Windows. How do I easily run it on Windows?

Does it include any code that is specific to Linux? Or does it use only C standard library and POSIX API?

4 Answers 4

Elaborating a bit on the answers from caf and jartieda.

Cygwin is an attempt to emulate a (nearly) complete POSIX execution environment in a native Windows process. It is complete enough that a surprising amount of Unix application code simply compiles and runs using the familiar ./configure && make && make install idiom. This trick was done by supplying a DLL that emulates POSIX system calls using the Windows API. Based on that, you get a complete GCC toolchain, bash, and all the usual command line utilities you are used to. One downside is that the compiled program is dependent on the Cygwin DLL, which makes it tricky to deliver the result to a system that does not already have Cygwin installed and whose user doesn’t want to use a Unix shell.

MinGW is a port of the GCC toolchain that generates native Windows applications that depend on the well known (and distributed with Windows itself) MSVCRT.DLL C runtime library. It makes no attempt to emulate a POSIX operating system, but applications that are mostly based on the standard C libraries, will often build essentially unchanged.

MSYS is a compile-time environment that provides enough Unix utilities (including bash) to often allow ./configure to run, and if the project supports the results, finish the build with MinGW’s GCC. The result is a native Windows executable that does not depend on any DLLs you don’t deliberately use aside from MSVCRT.DLL. Although the MSYS environment itself was a fork of an early version of the Cygwin project, it is primarily intended to be used to provide a unix-like place to compile native Windows applications: one would generally not build new applications based on its runtime environment.

Another approach to compiling for Windows is to use the MinGW cross compiler on linux. A number of MinGW’s core developers work that way, testing they product either under Wine, or under Windows running in a VM or a separate PC.

If the program has a GUI component, then you may have additional difficulties. Some GUI frameworks are available for both Linux and Windows. Qt, wxWidgets, and IUP all leap to mind, but there are others out there.

Edit: I’ve improved the paragraph above about MSYS to emphasize that it is intended to be a compile-time environment for building programs that run as native Windows applications, and not a full POSIX environment like Cygwin.

Note, also, that there is always the option of porting the project to one of the more traditional compilers for Windows development. Open Watcom, the Borland compilers, and Microsoft all have free or inexpensive versions, although often enough not under licenses that make the opens source community entirely happy.

Читайте также:  Linux get library path

This approach probably requires more effort because the differences between a Linux environment and the Windows environment become more visible when you also switch from one toolchain to another. This is especially true of the Microsoft compilers which are historically not as fully standards compliant as GCC.

Источник

Download, install, and set up the Linux workload

Linux projects are supported in Visual Studio 2017 and later. To see the documentation for these versions, set the Visual Studio Version selector control for this article to Visual Studio 2017 or Visual Studio 2019. It’s found at the top of the table of contents on this page.

You can use the Visual Studio IDE on Windows to create, edit, and debug C++ projects that execute on a remote Linux system, virtual machine, or the Windows Subsystem for Linux.

You can work on your existing code base that uses CMake without having to convert it to a Visual Studio project. If your code base is cross-platform, you can target both Windows and Linux from within Visual Studio. For example, you can edit, build, and debug your code on Windows using Visual Studio. Then, quickly retarget the project for Linux to build and debug in a Linux environment. Linux header files are automatically copied to your local machine. Visual Studio uses them to provide full IntelliSense support (Statement Completion, Go to Definition, and so on).

For any of these scenarios, the Linux development with C++ workload is required.

Visual Studio setup

  1. Type «Visual Studio Installer» in the Windows search box: Screenshot of the Windows search box which contains the text: visual studio installer.
  2. Look for the installer under the Apps results and double-click it. When the installer opens, choose Modify, and then click on the Workloads tab. Scroll down to Other toolsets and select the Linux development with C++ workload. Screenshot highlighting the Visual C++ for Linux Development workload item in Visual Studio Installer.
  3. If you’re targeting IoT or embedded platforms, go to the Installation details pane on the right. Under Linux development with C++, expand Optional Components, and choose the components you need. CMake support for Linux is selected by default.
  4. Click Modify to continue with the installation.

Options for creating a Linux environment

If you don’t already have a Linux machine, you can create a Linux Virtual Machine on Azure. For more information, see Quickstart: Create a Linux virtual machine in the Azure portal.

On Windows 10 and later, you can install and target your favorite Linux distro on the Windows Subsystem for Linux (WSL). For more information, see Windows Subsystem for Linux Installation Guide for Windows 10. If you’re unable to access the Windows Store, you can manually download the WSL distro packages. WSL is a convenient console environment, but it’s not recommended for graphical applications.

Linux projects in Visual Studio require the following dependencies to be installed on your remote Linux system or WSL:

  • A compiler — Visual Studio 2019 and later have full support for GCC and Clang.
  • gdb — Visual Studio automatically launches gdb on the Linux system, and uses the front end of the Visual Studio debugger to provide a full-fidelity debugging experience on Linux.
  • rsync and zip — the inclusion of rsync and zip allows Visual Studio to extract header files from your Linux system to the Windows filesystem for use by IntelliSense.
  • make
  • openssh-server (remote Linux systems only) — Visual Studio connects to remote Linux systems over a secure SSH connection.
  • CMake (CMake projects only) — You can install Microsoft’s statically linked CMake binaries for Linux.
  • ninja-build (CMake projects only) — Ninja is the default generator for Linux and WSL configurations in Visual Studio 2019 version 16.6 or later.
Читайте также:  Connect usb device to linux

The following commands assume you’re using g++ instead of clang.

Linux projects in Visual Studio require the following dependencies to be installed on your remote Linux system or WSL:

  • gcc — Visual Studio 2017 has full support for GCC.
  • gdb — Visual Studio automatically launches gdb on the Linux system and uses the front end of the Visual Studio debugger to provide a full-fidelity debugging experience on Linux.
  • rsync and zip — the inclusion of rsync and zip allows Visual Studio to extract header files from your Linux system to the Windows filesystem to use for IntelliSense.
  • make
  • openssh-server — Visual Studio connects to remote Linux systems over a secure SSH connection.
  • CMake (CMake projects only) — You can install Microsoft’s statically linked CMake binaries for Linux.

Linux setup: Ubuntu on WSL

When you’re targeting WSL, there’s no need to add a remote connection or configure SSH to build and debug. zip and rsync are required for automatic syncing of Linux headers with Visual Studio for Intellisense support. ninja-build is only required for CMake projects. If the required applications aren’t already present, you can install them using this command:

sudo apt-get install g++ gdb make ninja-build rsync zip 

Ubuntu on remote Linux systems

The target Linux system must have openssh-server, g++, gdb, and make installed. ninja-build is required for CMake projects only. The ssh daemon must be running. zip and rsync are required for automatic syncing of remote headers with your local machine for Intellisense support. If these applications aren’t already present, you can install them as follows:

    At a shell prompt on your Linux computer, run:

sudo apt-get install openssh-server g++ gdb make ninja-build rsync zip 

Fedora on WSL

Fedora uses the dnf package installer. To download g++, gdb, make, rsync, ninja-build, and zip, run:

sudo dnf install gcc-g++ gdb rsync ninja-build make zip 

zip and rsync are required for automatic syncing of Linux headers with Visual Studio for Intellisense support. ninja-build is only required for CMake projects.

Fedora on remote Linux systems

The target machine running Fedora uses the dnf package installer. To download openssh-server, g++, gdb, make, ninja-build, rsync, and zip, and restart the ssh daemon, follow these instructions. ninja-build is only required for CMake projects.

    At a shell prompt on your Linux computer, run:

sudo dnf install openssh-server gcc-g++ gdb ninja-build make rsync zip 
sudo systemctl start sshd 

Next Steps

You’re now ready to create or open a Linux project and configure it to run on the target system. For more information, see:

Читайте также:  Red hat linux sap

Источник

C++ cross-compiler from Windows to Linux [closed]

Closed. This question is seeking recommendations for books, tools, software libraries, and more. It does not meet Stack Overflow guidelines. It is not currently accepting answers.

We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.

Funny: all that stuff going through the VS IDE is what confuses the heck out of me — there’s so much configuration required to get it to work. It’s so much easier on Linux where you don’t have it!

Usually «a cross-compiler from X to Y» would mean, «a compiler that itself runs on X, but produces binaries that run on Y». So you’re asking for a compiler that runs on Windows and produces Linux binaries. Is that what you want? The usual way to do it is to compile for linux on linux. The usual reason for cross-compiling is that Y is not a viable development environment (e.g. because it’s a mobile phone, set-top box, Roomba, etc), maybe you’d be better off finding a Linux IDE you can live with?

@ H.Josef come-one you can compile code for an avr uControler with a compiler running under windows and you can’t compile code for linux using a compiler that runs under windows . I don’t believe that .

@neo_x3m AFAIK you can compile standard C++ code using any compiler, but creating a library (and testing it) on a completely different operating system is cumbersome and even impossible, Once I tried to create and test a dynamic linux library (.so) using MingW and it did not work, it did not work even with Cygwin since Windows environment is very different from Linux.

Running the Linux binary would be very cumbersome, but if you have all the libs/OBJ in Linux format, but compiling is nothing magical. if a Linux compiler app would run on Windows, why shouldn’t it work? Why couldn’t you even compile a Linux compiler under Windows?

5 Answers 5

  • Install cygwin, with development packages
  • Download crosstool-ng
  • Extract the tarball ( tar xvjf crosstool-ng* )
  • Run ./configure
  • When configure complains about a missing tool, go back to cygwin setup and install the corresponding package (e.g. flex , libtool , ncurses-devel ) You’ll need at least the following:
    • Devel/gperf
    • Devel/bison
    • Devel/byacc
    • Devel/wget
    • Devel/patch
    • Devel/make (GNU version)
    • Devel/automake 1.10+
    • Libs/libncursesw10
    • Libs/libncursesw10-devel
    • the _libintl_gettext error as mentioned in the comments
    • the byteshift headers, and
    • the ELF headers (Thanks Duncan Calvert & osm0sis)

    Of course, this is NOT going to enable you to build linux applications from inside Visual Studio. (VS2010 and later let you build with other toolchains such as gcc, but you’d need an appropriate toolchain description in addition to the cross-compiler built by crosstool-ng). But you’ll have a working g++-linux-gnu , which you can either run directly or using a Makefile.

    NOTE: Building the cross-compiler takes a LONG time. osm0sis has provided a prebuilt gcc 4.8.1 here, along with his notes on building the cross-compiler (used to update this answer).

    Источник

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