-2

Till now, i have been successfully compiling a same code in both windows and Linux Based system using code-blocks! And got no errors till now but i am just c++ beginner and my program are simple so i couldn't figure it !so i thought maybe I could get it cleared from you, guys!

Does all c++ code(exactly the same) will compile and work the same on Windows system as well as Linux Based system? and Why?

If your answer is:YES,they do

  • Why are same c++ programs in different versions (Windows version and Linux version)?

If your answer is:No,they dont

  • Why? and What code work in windows and not in Linux and vice versa?

  • Is there any c++ code to find whether it is running in windows based system or Linux based system?

  • Does all c++ code(exactly the same) will compile and work the same on MAC system as well as Linux Based system?

Thanks for reading

mike
  • 88
  • 1
  • 9
  • sorry putting few extra questions in my post! i thought they are too small and silly to put a separate post for them ! – mike Sep 25 '16 at 11:24
  • 1
    You don't have to **bold** all the questions... Our eyesights are still good you know – WhiZTiM Sep 25 '16 at 11:24
  • ok done editing it ! i have changed it! – mike Sep 25 '16 at 11:26
  • What are all those Ctrl-M characters? – stark Sep 25 '16 at 11:49
  • Reminds me of a geocities site. Only thing missing is colorful text. Bold, italic different font sizes and every possible formatting mixed in a soup. – sashoalm Sep 25 '16 at 12:14
  • is the question clear! Only that matters! – mike Sep 25 '16 at 12:19
  • so the term you are searching for is "c++ code portability" what portabiliy is? how and when and where? eg: os: win or linux or osx or bsd or... which target x86? or x64? gui? console? altcom? mfc?? you can google code portability. – Raindrop7 Sep 25 '16 at 12:49

4 Answers4

3

There are many requirements for C++ code that must be met, if you want it to compile on two different OS:

  • Does your code use OS-specific libraries? Such code won't compile on different platforms.
  • Does your compiler provide appropriate extensions on OS A to support OS B libraries?

The best way would be to avoid both of these situations. Another question is:

  • Does compiler on OS A use the same C++ standard version as compiler on OS B?

If you use features from C++14 with C++14-supporting compiler on OS A, but compiler on OS B does not support it, it simply won't compile.

Why are same c++ programs in different versions (Windows version and Linux version)?

Because C++ standard doesn't provide information about OS without some additional libraries or compiler extensions. There might be also some differences in string parsing that you use in your code, for example when opening files or writing to files.

Why? and What code work in windows and not in Linux and vice versa?

Why is that, see above. Only code that uses exacly the same C++ standard without any OS specific aspects will compile of different OS. You can hax this with conditionally compiled code,

see this.

Is there any c++ code to find whether it is running in windows based system or Linux based system?

Compilers should provide prefedined macros to check the target OS (see link above)

Does all c++ code(exactly the same) will compile and work the same on MAC system as well as Linux Based system?

No, definitely not all, reason given above.

Community
  • 1
  • 1
xinaiz
  • 7,744
  • 6
  • 34
  • 78
  • Finally, a prefect and clear answer that answer all! thats what i want! thanks alot! – mike Sep 25 '16 at 12:16
  • `` is a header file, not a library. And there isn't anything in it, that would inhibit **compilation** on an arbitrary platform. Depending on what is referenced, it may not even inhibit running the final executable on a non-Windows platform. *"Compilers should provide prefedined macros, so you can check what OS you are running."* - No, they don't. They usually provide macros you can use to check the **target platform**. In all, this answer isn't even approximately correct. You may wish to improve it, now that it's the accepted answer. – IInspectable Sep 25 '16 at 13:07
  • @IInspectable Thanks for pointing out. Edited. – xinaiz Sep 25 '16 at 13:12
1

There are already some answers, however there are more subtle differences than the system libraries. The first one that comes to my mind is line endings. Windows uses \r\n while linux uses just \n. If you use e.g. std::endl you will get the right line ending. However, once you want to read a file in linux that was written in windows, or vice versa, this might be an issue. Similarly windows uses \ as seperator for folders while linux uses /.

I am not claiming that this would be major obstacles when porting code from linux to windows or vice versa. The point is: the code might compile and maybe even run without errors but it might not work as expected when it was tested on linux but running on windows (or vice versa). The good thing is that C++ gives you all you need to write code that in principle should be able to run both on windows and linux.

463035818_is_not_an_ai
  • 109,796
  • 11
  • 89
  • 185
  • understood! thx ! BTW, Is there any c++ code to find whether it is running in windows based system or Linux based system? – mike Sep 25 '16 at 12:05
  • @Aravind.KEN: Why should there be? If the target platform matters, you already know at compile time. This information is usually available through predefined preprocessor macros. The point about line-ending differences really isn't related to cross-platform code either. It's something you have to deal with whenever **data** enters or leaves your application, be that a file stream or network socket. You have to know the protocol in order to digest it. – IInspectable Sep 25 '16 at 13:14
0

Not necessarily. They are two different operating systems and hence both of them have system specific libraries which they work with. If your code is a simple code with libraries that are both present in linux and Windows, well and great, the code will run on both systems.
On the other hand, for instance, if your code contains libraries that play with the operating system itself, the system calls the libraries make are different. Windows uses the windows API which will not work when run on Linux systems.

Zeokav
  • 1,653
  • 4
  • 14
  • 29
  • well, got ur point! and Is there any c++ code to find whether it is running in windows based system or Linux based system? – mike Sep 25 '16 at 11:29
0

Answer: it depends!

If your code contains references to system files (ex: windows dlls), then it will not compile on a different system (ex: linux system). If your code does not reference system files, then it will compile on any system.

Elias N
  • 1,430
  • 11
  • 19