Unable to compile C++ code on Mac OS Sonoma

135 Views Asked by At

I've been trying to compile my simple program

#include <iostream>

using namespace std;

int main() {
    cout << "Hello world" ;
}

But I get a bunch of errors when i compile

clang++ -std=c++17 e.cpp -o test:

/Library/Developer/CommandLineTools/SDKs/MacOSX13.3.sdk/usr/include/c++/v1/exception:144:24: note: definition of 'std::exception_ptr' is not complete until the closing '}'
class _LIBCPP_TYPE_VIS exception_ptr
                       ^
/Library/Developer/CommandLineTools/SDKs/MacOSX13.3.sdk/usr/include/c++/v1/exception:149:5: error: '__abi_tag__' attribute only applies to structs, variables, functions, and namespaces
    _LIBCPP_INLINE_VISIBILITY exception_ptr(nullptr_t) _NOEXCEPT : __ptr_() {}
    ^
/Library/Developer/CommandLineTools/SDKs/MacOSX13.3.sdk/usr/include/c++/v1/__config:689:37: note: expanded from macro '_LIBCPP_INLINE_VISIBILITY'
#  define _LIBCPP_INLINE_VISIBILITY _LIBCPP_HIDE_FROM_ABI
                                    ^
/Library/Developer/CommandLineTools/SDKs/MacOSX13.3.sdk/usr/include/c++/v1/__config:673:26: note: expanded from macro '_LIBCPP_HIDE_FROM_ABI'
          __attribute__((__abi_tag__(_LIBCPP_TOSTRING(_LIBCPP_VERSIONED_IDENTIFIER))))
                         ^
In file included from e.cpp:1:
In file included from /Library/Developer/CommandLineTools/SDKs/MacOSX13.3.sdk/usr/include/c++/v1/new:94:
/Library/Developer/CommandLineTools/SDKs/MacOSX13.3.sdk/usr/include/c++/v1/exception:149:55: error: expected ';' at end of declaration list
    _LIBCPP_INLINE_VISIBILITY exception_ptr(nullptr_t) _NOEXCEPT : __ptr_() {}
                                                      ^
/Library/Developer/CommandLineTools/SDKs/MacOSX13.3.sdk/usr/include/c++/v1/cmath:544:5: error: use of undeclared identifier '__promote'
    __promote<_A1, _A2, _A3>
    ^
/Library/Developer/CommandLineTools/SDKs/MacOSX13.3.sdk/usr/include/c++/v1/cmath:548:22: error: no template named '__promote'
    typedef typename __promote<_A1, _A2, _A3>::type __result_type;
                     ^
/Library/Developer/CommandLineTools/SDKs/MacOSX13.3.sdk/usr/include/c++/v1/cmath:552:12: error: call to 'hypot' is ambiguous
    return hypot((__result_type)__lcpp_x, (__result_type)__lcpp_y, (__result_type)__lcpp_z);
           ^~~~~
/Library/Developer/CommandLineTools/SDKs/MacOSX13.3.sdk/usr/include/c++/v1/cmath:533:46: note: candidate function
inline _LIBCPP_INLINE_VISIBILITY float       hypot(       float __x,       float __y,       float __z ) { return sqrt(__x*__x + __y*__y + __z*__z); }
                                             ^
/Library/Developer/CommandLineTools/SDKs/MacOSX13.3.sdk/usr/include/c++/v1/cmath:534:46: note: candidate function
inline _LIBCPP_INLINE_VISIBILITY double      hypot(      double __x,      double __y,      double __z ) { return sqrt(__x*__x + __y*__y + __z*__z); }
                                             ^
/Library/Developer/CommandLineTools/SDKs/MacOSX13.3.sdk/usr/include/c++/v1/cmath:535:46: note: candidate function
inline _LIBCPP_INLINE_VISIBILITY long double hypot( long double __x, long double __y, long double __z ) { return sqrt(__x*__x + __y*__y + __z*__z); }
                                             ^
In file included from /Library/Developer/CommandLineTools/SDKs/MacOSX13.3.sdk/usr/include/c++/v1/__memory/unique_ptr.h:14:
/Library/Developer/CommandLineTools/SDKs/MacOSX13.3.sdk/usr/include/c++/v1/__functional/hash.h:646:34: error: use of undeclared identifier 'nullptr_t'; did you mean 'nullptr'?
struct _LIBCPP_TEMPLATE_VIS hash<nullptr_t>
                                 ^
/Library/Developer/CommandLineTools/SDKs/MacOSX13.3.sdk/usr/include/c++/v1/__functional/hash.h:646:34: error: template argument for template type parameter must be a type
struct _LIBCPP_TEMPLATE_VIS hash<nullptr_t>
                                 ^~~~~~~~~
/Library/Developer/CommandLineTools/SDKs/MacOSX13.3.sdk/usr/include/c++/v1/__functional/hash.h:638:17: note: template parameter is declared here
template <class _Tp>
                ^
/Library/Developer/CommandLineTools/SDKs/MacOSX13.3.sdk/usr/include/c++/v1/__functional/hash.h:647:29: error: use of undeclared identifier 'nullptr_t'; did you mean 'nullptr'?
  : public __unary_function<nullptr_t, size_t>
                            ^
/Library/Developer/CommandLineTools/SDKs/MacOSX13.3.sdk/usr/include/c++/v1/__functional/hash.h:647:29: error: template argument for template type parameter must be a type
  : public __unary_function<nullptr_t, size_t>
                            ^~~~~~~~~
/Library/Developer/CommandLineTools/SDKs/MacOSX13.3.sdk/usr/include/c++/v1/__functional/unary_function.h:41:17: note: template parameter is declared here
template <class _Arg, class _Result>
                ^
In file included from /Library/Developer/CommandLineTools/SDKs/MacOSX13.3.sdk/usr/include/c++/v1/__memory/shared_ptr.h:28:
/Library/Developer/CommandLineTools/SDKs/MacOSX13.3.sdk/usr/include/c++/v1/__memory/unique_ptr.h:173:3: error: non-static data member cannot be constexpr; did you intend to make it const?
  _LIBCPP_CONSTEXPR unique_ptr(nullptr_t) _NOEXCEPT : __ptr_(__value_init_tag(), __value_init_tag()) {}
  ^
/Library/Developer/CommandLineTools/SDKs/MacOSX13.3.sdk/usr/include/c++/v1/__config:728:31: note: expanded from macro '_LIBCPP_CONSTEXPR'
#    define _LIBCPP_CONSTEXPR constexpr
                              
fatal error: too many errors emitted, stopping now [-ferror-limit=]
20 errors generated.

I have tried uninstalling and installing Xcode, as well as the CommandLineTools.

I've also tried the steps in this post but nothing has worked.

I have also added this to my .zshrc:

export CPATH=`xcrun --show-sdk-path`/usr/include
export PATH="/opt/homebrew/bin:$PATH"
export PATH="/opt/homebrew/sbin:$PATH"

What should I do to make my .cpp files compile?

ls -lh of \Library\Developer\CommandLineTools\SDKs shows:

total 0
lrwxr-xr-x  1 root  wheel    14B 12 Mar 12:40 MacOSX.sdk -> MacOSX14.4.sdk
drwxr-xr-x  7 root  wheel   224B 12 May  2023 MacOSX13.3.sdk
lrwxr-xr-x  1 root  wheel    14B 12 Mar 12:39 MacOSX13.sdk -> MacOSX13.3.sdk
drwxr-xr-x  7 root  wheel   224B 19 Feb 19:28 MacOSX14.4.sdk
lrwxr-xr-x  1 root  wheel    14B 12 Mar 12:38 MacOSX14.sdk -> MacOSX14.4.sdk

Environment variables:

SSH_AUTH_SOCK=/private/tmp/com.apple.launchd.xODP4nMDfe/Listeners
LC_TERMINAL_VERSION=3.4.23
COLORFGBG=7;0
ITERM_PROFILE=Default
XPC_FLAGS=0x0
LANG=en_CA.UTF-8
PWD=/Users/myname
SHELL=/bin/zsh
__CFBundleIdentifier=com.googlecode.iterm2
TERM_PROGRAM_VERSION=3.4.23
TERM_PROGRAM=iTerm.app
PATH=/opt/homebrew/sbin:/opt/homebrew/bin:/Users/myname/.pyenv/shims:/Library/Frameworks/Python.framework/Versions/3.11/bin:/Users/myname/.rbenv/shims:/opt/homebrew/bin:/Library/Frameworks/Python.framework/Versions/3.11/bin:/Library/Frameworks/Python.framework/Versions/3.10/bin:/usr/local/bin:/System/Cryptexes/App/usr/bin:/usr/bin:/bin:/usr/sbin:/sbin:/var/run/com.apple.security.cryptexd/codex.system/bootstrap/usr/local/bin:/var/run/com.apple.security.cryptexd/codex.system/bootstrap/usr/bin:/var/run/com.apple.security.cryptexd/codex.system/bootstrap/usr/appleinternal/bin:/Library/Apple/usr/bin:/usr/local/mongodb/bin
LC_TERMINAL=iTerm2
COLORTERM=truecolor
COMMAND_MODE=unix2003
TERM=xterm-256color
HOME=/Users/myname
TMPDIR=/var/folders/bf/ynvdz9294yl3b5v2hn5kg11m0000gp/T/
USER=myname
XPC_SERVICE_NAME=0
LOGNAME=myname
ITERM_SESSION_ID=w0t1p0:6B7014C1-5EDB-4218-9A47-80371E817BDB
__CF_USER_TEXT_ENCODING=0x0:0:82
SHLVL=1
OLDPWD=/Users/myname
GEM_HOME=/Users/myname/.gem
RBENV_SHELL=zsh
PYENV_ROOT=/Users/myname/.pyenv
PYENV_SHELL=zsh
CPATH=/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include
CPLUS_INCLUDE_PATH=/usr/local/opt/llvm/include/c++/v1:/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include
LIBRARY_PATH=:/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/lib
_=/usr/bin/printenv
0

There are 0 best solutions below