I would like to know what's the difference between the program developed in C++ under windows and linux. Can anybody tell me why the program developed under Windows in C++ can't be used under linux?
views:
167answers:
8Native programs are not compatible because Windows has a completely different set of API's than Linux, for one. As others have mentioned, each platform uses a different executable format as well. Also both platforms have their own set of libraries that programs will be linked against and/or share. For example, a Windows program will typically be developed in Visual Studio using windows-specific libraries such as MFC
, Win32
API, etc. These libraries are not available in linux, so the program will not even compile unless care is taken to make sure cross-platform libraries (such as QT) are used.
If you are careful, however, you can use cross-platform libraries in your code and you can get the same program to compile under both platforms. For such a program you would need to carefully put any platform-specific details (file system locations, etc) in their own files. Then, you would need to setup the proper #define
statements and/or makefile directives to ensure the proper files are included in the build for each platform.
Of course, if you use a "cross-platform" language such as Java or Python, and do not use any platform-specific code in your implementation, then your program can run under both environments.
Note Although the executable formats are different, some programs developed on Windows can be executed under Linux using an emulator called WINE.
C++ is itself portable. But some C++ libraries are not.
If a c++ program uses some libraries, which are not portable, then this program is not portable.
e.g. a C++ program uses MFC to draw the GUI stuff, because MFC is supported only in windows, so this C++ program cannot be compiled or run in linux directly.
Each operating system defines an API. If you code to call the Win32 API, it won't be there on Linux. If you code to the POSIX API, it won't jump right out at you in Windows.
To learn more about this, download a significant open source program (e.g. perl or python) and see how it's 'configure' script makes arrangements to compile in either place.
- Windows and Linux use different container formats to hold the executable code. (PE vs ELF)
- Windows and Linux have completely different APIs (except for trivial programs that only use the CRT and STL)
- Windows and Linux have a completely different directory structure
You could write a program that can use either set of APIs (for example, using QT), and that can handle either directory structure, but you still won't be able to run the same file on both operating systems because of the differing container formats.
This can be solved by using WINE.
In a nutshell,
- Windows runs PE format executables
- Linux runs ELF format executables
Furthermore, even if there were a tool to convert between PE and ELF, the program instructions necessary to interface with the operating system are completely different between Windows and Linux. Only the most restricted computation-only code (that only does calculations and doesn't interact with the operating system at all) could be ported between systems without special action. However, this is rarely done.
I believe some versions of Linux allow you to directly load device drivers designed for Windows without recompiling. However, this is an extremely special purpose application and the technique is not generally used.
There are 2 major reasons Theoretically , same program (source code) for some languages like C , can run on both Windows and Linux but the Compilation only differs , this means you have to compile the Same source code file for each Platform
But Actually , each Operating System has a different set of APIs . and different techniques to get job done faster ... which usually attract developers to use them .. and don't stick to standards , so they loose portability
this was for Native programs ... anyway .. there is Java and Python languages .. they are truly cross-platform but you have to sacrifice speed for sake of portability
This is a large topic.
first windows and linux aren't binary comparable. This mean that even the simplest of programs will not be recognized from one machine to the other. This is why interpreted languages like php perl python and java are becoming so popular, but even these don't all support the same set of features on each platform
Library dependence / OS support : Any significantly complicated program will need to access the system is some way, and many of the features available on one system are not available on the other. There are a million examples just look on so for linux equivalent of blank or windows equivalent of blank. Moving beyond os support applications are built mostly on top of libraries of functions and some of those are just not available on both systems.
Not to be overly pedantic, but developing a program is different than building it and executing it. In many cases, a program written on one operating system can be built and compiled to execute on another. Other programs, as others have pointed out, rely on certain functionality provided only by a particular OS or libraries resident only on that OS. As a result, it must be built and run on that OS.