(C/C++) How to generate executable file that can run on both Windows and Linux?
There is no way to have a single native executable compiled from C++ source that works on different platforms. I don't believe you that the same executable file is run on Windows and Linux, as you state in your second paragraph.
How to compile executable for Windows with GCC with Linux Subsystem?
Linux Subsystem works as a Linux-computer. You can only run Linux executables inside it and default gcc
creates Linux executables.
To create Windows executables, you need to install mingw cross-compiler:
sudo apt-get install mingw-w64
Then you can create 32-bit Windows executable with:
i686-w64-mingw32-gcc -o main32.exe main.c
And 64-bit Windows executable with:
x86_64-w64-mingw32-gcc -o main64.exe main.c
Note that these Windows executables will not work inside Linux Subsystem, only outside of it.
Generate windows executable from Linux source code?
You can use MSYS2
to use bison
on Windows. Here is the package link. Version 3.6.4-1
is available there.
A quick search took me to this video. You can refer to that if you don't have experience setting up MSYS2
on Windows
.
I noticed that you are using Chocolatey
. So you can install MSYS2
using that also. Here is the package link.
If you have MSYS2
in PATH
(C:\tools\msys64;C:\tools\msys64\mingw64\bin;C:\tools\msys64\usr\bin;
if you've installed using Chocolatey with default settings), then you don't need its shell
to run bison
. You can use any of your preferable terminals.
Edit :
You can use VS-2019
to build the executable itself. Here is the link to the git repository. Current stable release include bison
3.5.0
. But since you need version greater than 3.5.4
, you can go with the under development builds like this one.
Compile file in linux and run in windows
You can use a mingw cross compiler to build windows binaries in linux. In Ubuntu (and I guess other Debian variants as well) the package is called mingw32
. You then have a cross compiler under the name i586-mingw32msvc-g++
(or similar). For building simple command line programs without library dependencies this is an OK solution.
If you need more then this I'd recommend you use MXE (M cross environment). MXE installs its own cross compiler and can build many libraries for you so you don't need to care about how to build the library dependencies.
For example the OpenSCAD project (a 3D CAD program that is using Qt for its GUI) is using MXE for building the Windows releases. See this page on the OpenSCAD wiki for a description of the build process.
How to compile C code in Linux to run on Windows?
You would need a cross-compiler to create a Windows executable in Linux.
Mingw-w64 is an advancement of the original mingw.org project, created to support the GCC compiler on Windows systems.
Installing the cross-compilation
sudo apt-get install mingw-w64
32bit
i686-w64-mingw32-gcc -o test.exe test.c
64bit
x86_64-w64-mingw32-gcc -o test.exe test.c
How to create an executable C file for Windows
cygwin gcc produce an executable linked to the cygwin1.dll. So it is not usable without that.
gcc hello.c -o hello-cygwin.exe
$ ldd hello-cygwin.exe
ntdll.dll => /cygdrive/c/Windows/SYSTEM32/ntdll.dll (0x77bd0000)
kernel32.dll => /cygdrive/c/Windows/system32/kernel32.dll (0x77ab0000)
KERNELBASE.dll => /cygdrive/c/Windows/system32/KERNELBASE.dll (0x7fefdc60000)
SYSFER.DLL => /cygdrive/c/Windows/System32/SYSFER.DLL (0x75650000)
cygwin1.dll => /usr/bin/cygwin1.dll (0x180040000)
If you need a standalone program, a solution is to use the mingw compiler
(it is available on cygwin as cross compiler to windows)
$ x86_64-w64-mingw32-gcc.exe hello.c -o hello-mingw64.exe
$ ldd hello-mingw64.exe
ntdll.dll => /cygdrive/c/Windows/SYSTEM32/ntdll.dll (0x77bd0000)
kernel32.dll => /cygdrive/c/Windows/system32/kernel32.dll (0x77ab0000)
KERNELBASE.dll => /cygdrive/c/Windows/system32/KERNELBASE.dll (0x7fefdc60000)
SYSFER.DLL => /cygdrive/c/Windows/System32/SYSFER.DLL (0x75650000)
msvcrt.dll => /cygdrive/c/Windows/system32/msvcrt.dll (0x7fefdf40000)
You can move the resulting program on another windows machine that don't have cygwin installed.
Related Topics
How to Know If One Shared Library Depends on Another Shared Library or Not
Lightweight Memory Leak Debugging on Linux
How to Detect the Number of Physical Processors/Cores on Windows, MAC and Linux
Correct Use of Std::Cout.Precision() - Not Printing Trailing Zeros
Efficient Way to Determine Number of Digits in an Integer
Force Gcc to Notify About Undefined References in Shared Libraries
Why Does Auto X{3} Deduce an Initializer_List
Should We Pass a Shared_Ptr by Reference or by Value
Difference Between Std::System_Clock and Std::Steady_Clock
Linux C++ Error: Undefined Reference to 'Dlopen'
System("Command") Produces an Error; But It Works When Invoked Directly from Bash Prompt
Illegal Token on Right Side of ::
Regex Replace with Callback in C++11
How to Compile Qt 5 Under Windows or Linux, 32 or 64 Bit, Static or Dynamic on Visual Studio or G++
Convert Float to String with Precision & Number of Decimal Digits Specified