2

I'm trying to write code that can be compiled with any modern version of g++ and am having difficulties. I wrote the code using Visual Studios and it worked fine but I tried compiling it on another computer and it had problems.

One problem was it didn't recognize the macro EXIT_SUCCESS. I see here it's defined in cstdlib but how come in VS I didn't need to import that library?

Also in a .h file I had const private int PI = 3.1416 and on the other computer it didn't like that.

So how do you know what's going to be portable? I thought C (and therefore inherently C++) was invented to be compiled anywhere. I asked this question but think it go misinterpreted, are there certain settings that must be done to the IDE to ensure portability?

EDIT: I might understand it better knowing why did the code compile in Visual Studio without include <cstdlib> and how do you turn off such feature?

Community
  • 1
  • 1
Celeritas
  • 14,489
  • 36
  • 113
  • 194
  • 1
    1) learn the difference between C and C++. 2) learn the difference between integers and floating point numbers. 3) learn C and C++. 4) write correct, standard-conforming code. Then you're up to a pretty good start in terms of portability. – Kerrek SB Feb 03 '14 at 07:30
  • What KerrekSB said. Plus try compiling with the latest G++ and CLANG compilers, with flags specifying a particular standard, e.g. `-std=c++11` or `-std=c++98`, to avoid compiler extensions. And find out which headers macros, functions and types live in, and include them where required. Do not rely on indirect inclusions. – juanchopanza Feb 03 '14 at 07:32
  • 1
    @KerrekSB -1 answer is valueless...learn C and C++ – Celeritas Feb 03 '14 at 07:40
  • 2
    @Celeritas The [c++ standards](http://en.cppreference.com/w/) define pretty well what's portable and what not! – πάντα ῥεῖ Feb 03 '14 at 07:44
  • I mean seriously, how do you know cstdlib must be included for the macro? I red about EXIT_SUCCESS, used it, and it worked. No one ever told me I needed to include the the library and I'm wondering how did you know? – Celeritas Feb 03 '14 at 07:45
  • 1
    @Celeritas You search [in a good reference](http://en.cppreference.com/mwiki/index.php?title=Special%3ASearch&search=EXIT_SUCCESS) and it tells you. After a while you get to know these things, but there are so many names in the standard library (and new ones to come in the next standard) that is it worth having a reference handy to check these things. – juanchopanza Feb 03 '14 at 07:48
  • 1
    @Celeritas: I don't know why you said what @KerrekSB said is valueless, did you know that the `C11 standard §7.22/3 says` that `EXIT_SUCCESS` is under `stdlib.h` and you say learning C is useless. Learning a language doesn't mean its syntax alone, it's much more than that. – legends2k Feb 03 '14 at 08:02
  • @Celeritas: Kerrek's comment is because your question is tagged C++ and mentions C++ in the title, then you suddenly say "I thought C was invented to be compiled anywhere"... where did that change of subject come from? Anyway, if you compile with several compilers and investigate the warnings and errors you see, reading the Standard or asking when you're not sure... that's the best you can do. – Tony Delroy Feb 03 '14 at 08:05
  • 1
    You cannot program by guessing. If you want to use something like `EXIT_SUCCESS`, you need to know how, and you can only know that by learning the language (mostly by reading manuals). I don't think there's a shortcut, or that there's any more specific advice that will be universally useful regarding the question. – Kerrek SB Feb 03 '14 at 08:12

1 Answers1

3

In practice the only way to tell whether the code is portable is to compile it under all target compiler/platform. You can try to adhere to C++ standard but reality is harsh and there is no 100% complient compilers. Even simple program can give you surprises on other compilers.

AlexT
  • 1,413
  • 11
  • 11
  • 1
    +1, but it's not just about being 100% compliant... portability also involves studiously avoiding reliance on undefined-, unspecified- and implementation-defined behaviours that happen to "work" (well enough) on one system but may not on another.... – Tony Delroy Feb 03 '14 at 08:09
  • For this specific situation (using MS compiler as primary one) [MinGW](http://www.mingw.org/) is a good tool for checking portability – anatolyg Feb 03 '14 at 08:23