Cannot compile a simple C++ program on Mac OS

I'm trying to compile a simple hello world C++ program on my MacBook Pro. I have a M3 Pro (Nov 2023) running Sequoia 15.6. The program is:

#include <iostream>

int main() {
    std::cout << "Hello World!";
    return 0;
}

The error I get is:

In file included from test.cpp:1:
In file included from /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include/c++/v1/iostream:42:
In file included from /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include/c++/v1/ios:220:
In file included from /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include/c++/v1/__locale:15:
In file included from /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include/c++/v1/__memory/shared_ptr.h:13:
In file included from /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include/c++/v1/__compare/compare_three_way.h:13:
In file included from /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include/c++/v1/__compare/three_way_comparable.h:12:
In file included from /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include/c++/v1/__compare/common_comparison_category.h:15:
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include/c++/v1/cstddef:42:5: error: <cstddef> tried including <stddef.h> but didn't find libc++'s <stddef.h> header.           This usually means that your header search paths are not configured properly.           The header search paths should contain the C++ Standard Library headers before           any C Standard Library, and you are probably using compiler flags that make that           not be the case.
   42 | #   error <cstddef> tried including <stddef.h> but didn't find libc++'s <stddef.h> header. \
      |     ^

However, I can see stddef.h in /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include/c++/v1/ and in /usr/local/include

Answered by DTS Engineer in 854894022

Are you trying to compile this as an Xcode project? Or trying to compile from the command line?

If it’s the latter, I have a standard set of steps that I use to check that compiling from the command line works. See Investigating Third-Party IDE Integration Problems. Does that work for you?

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

Okay, I found some of the answers. Somehow, clang++ is looking into /usr/local/include before the MacOS SDKs. I am trying to figure out if brew somehow configured the system this way. None of the usual environmental variables are set, so I'm not sure how clang++ is being redirected to look into /usr/local/include.

Are you trying to compile this as an Xcode project? Or trying to compile from the command line?

If it’s the latter, I have a standard set of steps that I use to check that compiling from the command line works. See Investigating Third-Party IDE Integration Problems. Does that work for you?

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

Cannot compile a simple C&#43;&#43; program on Mac OS
 
 
Q