Xcode 9.2 (9C40b) header files problem.

I installed Xcode 9.2 on a new MacBook Pro. Unfortunately it failed to compile my programs. The problem can be reproduced with a Hello World! C++ program.


// helloworld.cpp

#include <iostream>

int main()

{

std::cout<<"Hello World!\n";

return 0;

}


Running "$ g++ helloworld.cpp", the error starts from iostream as follows:


In file included from helloworld.cpp:1:

In file included from /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/iostream:40:

In file included from /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/istream:163:

In file included from /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/ostream:140:

/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/locale:2212:23: error: member access into incomplete

type 'tm'

__get_day(__tm->tm_mday, __b, __e, __err, __ct);

^

/usr/include/wchar.h:131:19: note: forward declaration of 'tm'

const struct tm * __restrict) __DARWIN_ALIAS(wcsftime);


I tried reinstalling Xcode, but the same error showed up. Has anyone seen this problem? Thanks!


And here's the g++ version.


$ g++ -v

Configured with: --prefix=/Applications/Xcode.app/Contents/Developer/usr --with-gxx-include-dir=/usr/include/c++/4.2.1

Apple LLVM version 9.0.0 (clang-900.0.39.2)

Target: x86_64-apple-darwin17.3.0

Thread model: posix

InstalledDir: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin

You said:


#include <iostream>>


Is that your actual source code, or is it a typo? There should be only one ">". If you had two of them, then the error message is plausible, because the extra ">" gets compiled as part of the previous declaration (which is inside <iostream>, or inside one of the things iostream itself includes, more likely). You end up with an error message that seems to say there's something wrong with the header, but there really isn't.

Thanks Quincey. That was a typo in the post, which I've corrected in the post now. The cpp file doesn't have the extra '>'. (Although by adding it I got the same error.) It looks like the header files are inconsistent, eg, the time.h. How are the two head file directories related? Is there a way to cleanly re-install both?


/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/

/usr/include/c++/4.2.1/

OK. I resolved this by setting the system root explicitly to the MacOSX10.13.sdk.


g++ -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.13.sdk helloworld.cpp


Notice the sdk is a link to MacOSX.sdk. This should probably be a default env variable used by /usr/bin/clang++ or g++, but in my case it isn't.

Here's what I get for "g++ -v":


Configured with: --prefix=/Applications/Xcode.app/Contents/Developer/usr --with-gxx-include-dir=/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.13.sdk/usr/include/c++/4.2.1

Apple LLVM version 9.0.0 (clang-900.0.39.2)

Target: x86_64-apple-darwin17.3.0

Thread model: posix

InstalledDir: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin


and when I try to compile your test I don't get any errors. Xcode does manipulate things so that compilations depending on /usr redirect to the set of /usr files inside the Xcode package, but you can override this by installing files manually in /usr (or, perhaps, setting environment variables to something else), and that is perhaps what happened in your case.


What happens if you try using "c++ helloworld.cpp" instead? As you've demonstrated, you are in fact using clang, not the gnu compiler. If you really need the gnu compiler, you will have to install it yourself, since it hasn't been distributed with Xcode for quite a few years.

Thanks Quincey. Yes, the "--with-gxx-include-dir" made the difference, mine by default was pointing to /usr/include/c++/4.2.1/. Setting isysroot to MacOSX.sdk worked.

Xcode 9.2 (9C40b) header files problem.
 
 
Q