Compiling command line Linux program on Windows [duplicate]
I need to write a relatively simple command line C++ program to be run a Linux environment. However, I would like to code as well as compile this on Windows. The reason I don’t want to port it to Linux is because it requires MySQL interactions, and that would require some messy porting (IMO). It does not have to run on Windows; I just want to compile it on Windows because I want to use my preferred IDE’s. I’ve looked up on Cygwin for this task, but I haven’t been able to find any documentation to do what I’m trying to do.
4 Answers 4
(I’m assuming «..don’t want to port it to Linux..» is a typo for «..from Linux» and that you want the code to run in Linux as you said in your first sentence. This means cygwin or mingw would only be used as cross compilers and aren’t going to be very useful.)
This program already builds and works (or mostly works) on Linux, right? No reason to change that. Use your preferred editor (probably the one in your IDE) to edit the files and then just run the build system (probably make) in a Linux system (possibly in a VM). Export the files using a samba share (especially easy from a VM) so you can edit and automatically save remotely.
Note that you seem fine ditching every other feature of your IDE (debugger and compiler, mainly) and just using the editor part anyway.
Ah, are you not starting from any existing project and want to write this from scratch? If so, porting doesn’t make any sense. You want to write cross-platform code. (Cross-platform or «portable code» being related to, but different than, the act of «porting code» from one platform to another.)
The code is then both «Windows code» and «Linux code» at the same time, and you can use any compiler on Windows that can accept the code. Usually this happens by you sticking to standard libraries and other portable libraries, or writing shims for non-portable libraries to give them a portable interface, with the compiler supporting the C++ Standard.
You can use your preferred IDE’s compiler and debugger in this case, and don’t need cygwin or mingw. (Unless they’re used by your preferred IDE. :P)
Create a CMake Linux project in Visual Studio
Linux support is available in Visual Studio 2017 and later. To see the documentation for these versions, set the Version drop-down located above the table of contents to Visual Studio 2017 or Visual Studio 2019.
We recommend you use CMake for projects that are cross-platform or will be made open-source. You can use CMake projects to build and debug the same source code on Windows, the Windows Subsystem for Linux (WSL), and remote systems.
Before you begin
First, make sure you have the Visual Studio Linux workload installed, including the CMake component. That’s the Linux development with C++ workload in the Visual Studio installer. See Install the C++ Linux workload in Visual Studio if you aren’t sure you have that installed.
Also, make sure the following are installed on the remote machine:
The CMake support in Visual Studio requires server mode support introduced in CMake 3.8. For a Microsoft-provided CMake variant, download the latest prebuilt binaries at https://github.com/Microsoft/CMake/releases.
The binaries are installed in ~/.vs/cmake . After deploying the binaries, your project automatically regenerates. If the CMake specified by the cmakeExecutable field in CMakeSettings.json is invalid (it doesn’t exist or is an unsupported version), and the prebuilt binaries are present, Visual Studio ignores cmakeExecutable and uses the prebuilt binaries.
Visual Studio 2017 can’t create a CMake project from scratch, but you can open a folder that contains an existing CMake project, as described in the next section.
You can use Visual Studio 2019 to build and debug on a remote Linux system or WSL, and CMake will be invoked on that system. Cmake version 3.14 or later should be installed on the target machine.
Make sure that the target machine has a recent version of CMake. Often, the version offered by a distribution’s default package manager isn’t recent enough to support all the features required by Visual Studio. Visual Studio 2019 detects whether a recent version of CMake is installed on the Linux system. If none is found, Visual Studio shows an info-bar at the top of the editor pane. It offers to install CMake for you from https://github.com/Microsoft/CMake/releases.
With Visual Studio 2019, you can create a CMake project from scratch, or open an existing CMake project. To create a new CMake project, follow the instructions below. Or skip ahead to Open a CMake project folder if you already have a CMake project.
Create a new Linux CMake project
To create a new Linux CMake project in Visual Studio 2019:
- Select File > New Project in Visual Studio, or press Ctrl + Shift + N.
- Set the Language to C++ and search for «CMake». Then choose Next. Enter a Name and Location, and choose Create.
Alternatively, you can open your own CMake project in Visual Studio 2019. The following section explains how.
Visual Studio creates a minimal CMakeLists.txt file with only the name of the executable and the minimum CMake version required. You can manually edit this file however you like; Visual Studio will never overwrite your changes.
To help you make sense of, edit, and author your CMake scripts in Visual Studio 2019, refer to the following resources:
Open a CMake project folder
When you open a folder that contains an existing CMake project, Visual Studio uses variables in the CMake cache to automatically configure IntelliSense and builds. Local configuration and debugging settings get stored in JSON files. You can optionally share these files with others who are using Visual Studio.
Visual Studio doesn’t modify the CMakeLists.txt files. This allows others working on the same project to continue to use their existing tools. Visual Studio does regenerate the cache when you save edits to CMakeLists.txt, or in some cases, to CMakeSettings.json. If you’re using an Existing Cache configuration, then Visual Studio doesn’t modify the cache.
For general information about CMake support in Visual Studio, see CMake projects in Visual Studio. Read that before continuing here.
To get started, choose File > Open > Folder from the main menu or else type devenv.exe in a developer command prompt window. The folder you open should have a CMakeLists.txt file in it, along with your source code.
The following example shows a simple CMakeLists.txt file and .cpp file:
// hello.cpp #include int main(int argc, char* argv[])
cmake_minimum_required(VERSION 3.8) project (hello-cmake) add_executable(hello-cmake hello.cpp)
Running Linux programs on Windows
To run a Linux program on Windows, you have these options:
- Run the program as-is on the Windows Subsystem for Linux (WSL). In WSL your program executes directly on the machine hardware, not in a virtual machine. WSL also enables direct filesystem calls between Windows and Linux systems, removing the need for SSL transport. WSL is designed as a command-line environment and is not recommended for graphics-intensive applications. For more information, see Windows Subsystem for Linux Documentation.
- Run the program as-is in a Linux virtual machine or Docker container, either on your local machine or on Azure. For more information, see Virtual Machines and Docker on Azure.
- Compile the program using gcc or clang in the MinGW or MinGW-w64 environments, which provide a translation layer from Linux to Windows system calls.
- Compile and run the program using gcc or clang in the Cygwin environment, which provides a more complete Linux environment on Windows compared to MinGW or MinGW-w64.
- Manually port your code from Linux and compile for Windows using Microsoft C++ (MSVC). This involves refactoring platform-independent code into separate libraries, and then re-writing the Linux-specific code to use Windows-specific code (for example, Win32 or DirectX APIs). For applications that require high performance graphics, this is probably the best option.
Feedback
Submit and view feedback for
Running Linux programs on Windows
To run a Linux program on Windows, you have these options:
- Run the program as-is on the Windows Subsystem for Linux (WSL). In WSL your program executes directly on the machine hardware, not in a virtual machine. WSL also enables direct filesystem calls between Windows and Linux systems, removing the need for SSL transport. WSL is designed as a command-line environment and is not recommended for graphics-intensive applications. For more information, see Windows Subsystem for Linux Documentation.
- Run the program as-is in a Linux virtual machine or Docker container, either on your local machine or on Azure. For more information, see Virtual Machines and Docker on Azure.
- Compile the program using gcc or clang in the MinGW or MinGW-w64 environments, which provide a translation layer from Linux to Windows system calls.
- Compile and run the program using gcc or clang in the Cygwin environment, which provides a more complete Linux environment on Windows compared to MinGW or MinGW-w64.
- Manually port your code from Linux and compile for Windows using Microsoft C++ (MSVC). This involves refactoring platform-independent code into separate libraries, and then re-writing the Linux-specific code to use Windows-specific code (for example, Win32 or DirectX APIs). For applications that require high performance graphics, this is probably the best option.
Feedback
Submit and view feedback for