0

I am a beginner was trying to do some C++ programming on Xcode. It works fine, but when I try to compile the same c++ file on my windows pc using VS, there were some errors. After I look at my code closely, there are really some stupid mistakes that I have made which caused the errors, but Xcode seemed to have ignored them...

My question is that is there any setting that I need to change to prevent Xcode from being so smart?

For example, the following code can actually compile in xcode:

#include <iostream>
using namespace std;

int main() {

    if (true or false){
        cout << "How is this possible? \n";
    }
    return 0;
}

There are also other cases where the code is actually wrong, but it can compile just fine is Xcode which is the annoying part and I want to disable that.

bames53
  • 86,085
  • 15
  • 179
  • 244
zycuber
  • 83
  • 1
  • 7
  • It can compile in Visual Studio too if you include the correct header. http://stackoverflow.com/questions/2376448/the-written-versions-of-the-logical-operators – Retired Ninja Sep 26 '14 at 05:23
  • You can also enable them without the header by disabling extensions: http://stackoverflow.com/questions/555505/c-alternative-tokens As to your broader question, it can be difficult to write code that works on all compilers. The best way to know is to actually try and compile it. You can try to stick to the C++ standard, but different compilers have different levels of support for it. – Retired Ninja Sep 26 '14 at 05:37

4 Answers4

1

As far as I can see there is nothing wrong with your code.

The ISO C++ standard does not specify which standard headers are included by other standard headers. So, it is entirely possible that the version of iostream used by Xcode directly or indirectly includes ciso646. Whereas Visual Studio's version of iostream does not include ciso646. There are many similar cases with other headers. You just need to read the error messages and realize that your error (when you move your file to a different platform) is due to a missing header file.

user515430
  • 298
  • 1
  • 3
  • 7
  • The C++ standard supports 'alternative tokens' like `or` without the header ``. C++ specifies that this header "has no effect". VS should support alternative tokens without this header but doesn't. – bames53 Sep 26 '14 at 06:06
  • It does with the /Za switch to disable Microsoft language extensions. – Retired Ninja Sep 26 '14 at 06:10
  • @RetiredNinja disabling extensions in VS is a bad idea. /Za is buggy and they don't test their library implementation under /Za. Some of it doesn't work. Here's a message from one of their engineers to that effect: http://lists.cs.uiuc.edu/pipermail/cfe-dev/2012-June/021886.html – bames53 Sep 26 '14 at 06:21
  • @bames53 I didn't say whether it was good or bad, just that it is possible. I would argue using `and` and `or` in your code is a bad idea, but people will do what they will. – Retired Ninja Sep 26 '14 at 06:47
1

It would be nice if writing portable code meant writing code in accordance with the C++ standard specification, but unfortunately that's not the case. Although there are various compiler options on various implementations which can help bring different implementations closer together, in general you will just have to bring the code into the target environment and actually test it there.

So ultimately writing portable code means you'll have to learn some subset of C++ that is accepted by all the implementations you want to target.


or is an 'alternative token' in C++, and VS is incorrect to reject it. There's no option in Xcode to disable support for alternative tokens. However VS has non-standard support for or as a macro using the header <ciso646>, and Xcode does have a header <ciso646> which does nothing (as the standard specifies). So you can write code which uses or and which works in both Xcode and VS by including this header.

#include <iostream>
#include <ciso646> // does nothing in Xcode, allows `or` in VS
using namespace std;

int main() {

    if (true or false){
        cout << "How is this possible? \n";
    }
    return 0;
}

Unfortunately VS can't support all of the alternative tokens through macros and so Xcode will still support some that VS doesn't.


There are also other cases where the code is actually wrong, but it can compile just fine is Xcode which is the annoying part and I want to disable that.

If you give specific examples then I can provide additional advice on how to write portable code.

bames53
  • 86,085
  • 15
  • 179
  • 244
0

VS does not support or: you need to use || instead.

You can include some special files but it doesn't inject or sufficiently well into the language for it to work in all instances.

If you want to suppress use of or (and your compiler supports no better way) #define it to something that emits a compiler error, for example

#define or OR

This at least means that the nature of the compilation errors will be identical on Xcode and VC.

Bathsheba
  • 231,907
  • 34
  • 361
  • 483
  • I know, so I am asking how can I disable the support of 'or' on Xcode – zycuber Sep 26 '14 at 05:23
  • VS does support 'or'. Just include . – user515430 Sep 26 '14 at 05:34
  • There are (or were) numerous cases where this didn't work correctly. – Bathsheba Sep 26 '14 at 05:34
  • Okay, actually I do not think I need to modify anything on the VS side. It is just the Xcode side which is causing the problem for not reporting the errors. – zycuber Sep 26 '14 at 05:36
  • The problem is you think the above code is incorrect because Visual Studio would not compile it, when the code is actually correct. The fact that you need to jump through a few hoops to make it work in Visual Studio is Visual Studio's problem, not the code. – Retired Ninja Sep 26 '14 at 05:57
  • @RetiredNinja: You're absolutely correct. For an easy life though, just drop the `or`s. – Bathsheba Sep 26 '14 at 06:49
0

Rather than changing your Xcode settings, I suggest cross-checking your code using another development environment.

If you're looking for something cheap and full-proof. Download a VirtualBox Windows VM, and run download Dev C++ (bloodhshed)

Kevin Michael
  • 118
  • 1
  • 7
  • 1
    Bloodshed Dev-C++ is ancient and very wrong on many things. The only reason to use it is if the class graders are using it for grading. – bames53 Sep 26 '14 at 05:56