Windows executable got executed on my linux - c++

I wrote a simple code and compiled using g++ in linux in .exe format, suprisingly it got executed in my linux terminal. Can u say the reason for this ? Can linux terminal execute machine code in any format ? Can I run the same in windows ?
Code :
#include<iostream>
using namespace std;
int main(){
cout<<"Hello World !"<<endl;
return 0;
}
Compile code:
g++ main.cpp -o program.exe
OS: Linux Mint 20 Cinnamon.
I execute by typing ./program.exe

Q: I wrote a simple code and compiled using g++ in linux in .exe format, suprisingly it got executed in my linux terminal
A: There's nothing "surprising". I assume you compiled it under Linux? So why shouldn't you be able to run it under Unix?
Q: Will an .exe I build on Windows run on Linux (if I copied the binary)? A: Short answer: No.
Longer answer: you can install Wine to run Windows applications on Linux.
Q: Will an .exe I build on Ubuntu run on Windows? A: No.
Q: Does an executable I build on Ubuntu need to have the suffix .exe? A: No. File suffixes are irrelevant.
Q: Does an executable I build on Windows need to have the suffix .exe? A: Yes.
To answer your additional questions:
There are many reasons why an .exe built for one platform cannot load or run on a different platform.
Sam Varshavchik put it well:
It's for the same reason a radiator for your Toyota won't fit into a
Dodge
More to the point, an "executable image" is much more than just "the machine code".
An .exe is an example of "executable images". They come in many different formats: https://en.wikipedia.org/wiki/Category:Executable_file_formats. Most of these formats are platform-specific.
Any image must be loaded by the operating system in order to become a running process. This, too, is platform-specific.
The running process will need resources like file I/O , memory and shared libraries, which are also platform-specific.
I hope that helps...

Since you compiled the code using g++ on Linux, you should be able to run it on Linux but it won't work on Windows because the binary executable is compiled for Linux.
On Windows, .exe is the extension of the executable file but the binary code must be compiled for Windows so that it can run. On Linux, you can use any extension, .mp4 or anything and it will still run when typing ./program.mp4 in terminal.

Related

How to run .exe files output by Turbo C++?

I wrote a program in Turbo C++, and compiled it there. Everything was successful, and the program ran as expected. Now, I want to run the program as an exe file. I found the exe file in a separate folder named 'Source'. But whenever I try to run it, I get the error :"This app can't run on your PC. To find a version for your PC, check with the software publisher." None of the exe files in that folder are working, although all the other softwares (like Photoshop) are opening fine.
Could someone help me please?
The OS on your PC probably is 64 bit. So the older 16 bit application would not run on it. I suggest you to use some virtual machine with installed OS DOS (or free DOSBOX for example), or try to compile your program on more modern compiler.
write
system("PAUSE");
before return 0;

running exe in Linux

I have built a simple console application in c++ vs2010 that just displays one line of text. Is it possible to run this executable from the Release folder in Linux?
Say I put the Release folder on a thumb drive, open it on a linux machine?
I was under the impression that console application written in c++ could run in both windows in linux? how mislead am I?
a) You need to run the executable with wine and emulate a windows environment
- OR -
b) You need to compile your code for your Linux distribution.

running an executable c++ program in solaris

I compiled my class Transfer correctly in Solaris. There is the executable file transfer. If i were in Linux, I would to ./transfer and the program would execute perfectly. However, when I run ./transfer in Solaris I get:
bash: ./transfer: invalid argument
Does someone know how to run an executable in Solaris?
Sounds like compilation was done targeting a different architecture than the host machine.
See related thread - x86 binary on a SPARC machine.
Please have a look at the follwing pages that I append here:
https://unix.stackexchange.com/questions/36376/what-does-invalid-argument-mean-in-solaris
I am trying to excute java from solaris, getting invalid argument error
and also consider correct folder of files and file permissions, sometimes these issues can make problems.

How to make a linux command-line program work in windows?

I'm a beginner with programming, and I've been doing work in C/C++ in Ubuntu. When I tell something to cin/cout/cerr or printf/scanf or take arguments from the command line, this all happens from the linux terminal in Ubuntu.
Now if I want to run these same programs (very simple programs, beginner-level) and run them in Windows, how do I run them from the Windows command line? A previous course I've taken had us download cygwin to simulate the linux command line in windows, but what if I want to just run the program from the ordinary windows command line? Is that possible, and does it require modification of the software?
You can cross-compile the program for Windows from linux.
On Ubuntu, process is basically this:
sudo apt-get install wine mingw32 mingw32-binutils mingw32-runtime
...
i586-mingw32msvc-g++ -o myProgram.exe myProgram.cpp
Easy, right? Google for "ubuntu cross-compile windows," there's a ton of information out there.
It's exactly the same. You run cmd and write the command (almost) exactly as you would in Linux.
For example, if you build your program to program, you would run it in Linux like this:
./program --option1 -o2 file1 file2
And in Windows, first you have to make the output have a .exe suffix and then in cmd you would write:
program.exe --option1 -o2 file1 file2
Basically saying, cmd is Windows' terminal. It's nowhere near as good as the Linux terminal, but that would be all you get without installing additional software.
cin/cout/cerr and printf/scanf/fprintf(stderr, ...) use the standard C preopened files stdin, stdout and stderr which are defined both in Linux and Windows. Once you run the application from Windows' terminal (cmd), you see the input/output exactly as you would in the Linux terminal. I/O redirection is also very similar.
cin and cout, and printf and scanf, work much the same in Windows as they do in Linux. (I'm pretty sure cerr does too, but that one i'm not 100% sure about. At the very least, though, it's there and works.) The biggest difference is that Windows typically won't expand wildcards (stuff like *.txt) before running your program; you have to do that yourself in most cases.
Basically, as long as the app doesn't use anything specific to Linux or GCC, you could just recompile it on the target machine using whatever compiler you like to test.
If you don't want to recompile...well...good luck with that. Even Cygwin won't run native Linux binaries. You'd need a virtual machine with Linux on it.
Well, if you program is portable and not using any features specific to Linux, you would have to compile it from source on Windows to make it work on Windows.
You would need the GCC tool-chain for windows to do that, which you can get from the TDM-GCC homepage. Its MinGW internally and the installer allows you to choose the features you want to install as well as the target directory for installation. It also adds itself to Windows path so that the compiler commands are available from the shell prompt.
I have to do the cross compilation regularly and it works without any issues for me. There is one change which you must make if your project is using Makefiles. For the target binary, such as <target>.out in linux, you would have to edit your Makefile and rename it to <target>.exe so that it runs on the command line. If you are not using Makefiles and just doing gcc <file.c>, the a.exe is produced by default (similar to a.out in Linux).
Say you have this program code you want to run on UNIX and Windows:
#include <stdio.h>
int main()
{
printf("Hi\n");
return 0;
}
When you type a command in a UNIX shell it will be something like this.
/usr/home/bobby# gcc main.c
/usr/home/bobby# ./a.out
Hi
/usr/home/bobby#
On Windows you'd have to first choose your development environment/compiler. Without going to something like Cygwin, you could install the Windows SDK or Visual studio (although if the latter you might just want to develop in the GUI).
Start -> Run -> cmd /k ""C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\vcvarsall.bat"" x86
C:\Windows\system32>cd c:\bobby
C:\bobby>cl main.c
C:\bobby>main.exe
Hi
C:\bobby>
When a C program is compiled into an executable this is done in a system dependent way. On Ubuntu the ELF format is used and on Windows we have PE.
When you start a process the ELF or PE is read giving instructions/map on how to allocate memory and where to put various pieces of the process in a virtual memory table. Further it links up to dynamically loaded libraries, already in physical memory, that it share with other processes which is using the same libraries. Or if the dynamic libraries is not present load them. (Linux .so, windows .dll). If it has static libraries these are allocated and linked in (Linux .a, Windows .lib). - Very simplified.
Memory restrictions etc are inherited from previous process.
Environment variables are put into the running environment for the process. This being paths, arguments, etc. Then main() is added to the stack and called.
Now everything happening before main is called and how linkage etc are resolved, and so many other things, depends on the system. This is why one simply can't run an executable compiled on Linux on Windows.
Using cygwin one is simply creating a virtual environment where those linkages etc are the same and would work. One create an ELF environment.
To get it linked for native Windows command line one would have to compile for Windows. On that matter I see there is lots of answers already.
The ELF and PE, as on different systems, also have different ways of handling environment variables etc. What these are etc. So i.e. file expansion is handled differently. However both running processes has the default streams like stderr, stdout and stdin. But below your code in C they are not the same.
It is like driving a diesel vs a petrol car. Much is the same but under the hood quite a few things is different.
Be aware that i.e. signals are handled different on Windows.

running unix like command "./a.out <data.txt" in windows environment on c++ file

How do I run a command like interface on windows and use the g++ and ./a.out
I am a beginning programmer used to using putty/ssh to write (nano), compile (g++ command), and run (./a.out) c++ programs.
Our class has now switched to netbeans, but our latest assignment requires us to use the ./a.out <datafile.txt-like command.
Or is the input redirection style ./a.out <data.txt unique to unix and cannot be done in windows?
edit: the < input redirection marks made my post mostly unreadable. Sorry about that
2nd edit: There is actually a terminal built into netbeans that VERY conveniently starts in your project directory. open it in netbeans by selecting Window -> output -> Terminal
Cygwin is a collection of tools which provide a Linux look and feel environment for Windows.
start,
run,
cmd,
cd directory,
g++ a.cpp,
a.exe
Windows includes . in $PATH, so the ./ at the beginning is superfluous (and wrong, since it would be .\). The rest is the same.