(C/C++) How to Generate Executable File That Can Run on Both Windows and Linux

(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.

Sample Image

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.

Sample Image

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



Leave a reply



Submit