can't compile c++ hello world with clang on mac sequoia 15.0 and VS Code
Asked Answered
M

1

6

I have updated XCode(v. 16.0) and made sure the xcode command line tools (xcode-select version 2409) are updated.

I have installed the C/C++ VS Code extension v.1.21.6

My compiler path is set to /usr/bin/clang++

with the command clang++ --version I see the output:

Apple clang version 16.0.0 (clang-1600.0.26.3)
Target: arm64-apple-darwin24.0.0
Thread model: posix
InstalledDir: /Library/Developer/CommandLineTools/usr/bin

If I follow the hello world tutorial at https://code.visualstudio.com/docs/cpp/config-clang-mac, I see a red squiggle for the first include with the error:

#include errors detected. Please update your includePath.

I have searched extensively across my filesystem and fond many directories that have iostream, each specific to a version of macos.

I have tried adding an additional includePath which does contain iostream:

/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/c++/v1

but when i try to build I still get the error:

fatal error: 'iostream' file not found

The documentation makes it sound like if I have the correct complier path then it should work:

The extension uses the compilerPath setting to infer the path to the C++ standard library header files.

When I start up a XCode C++ project and build and run the same hello-world code it works fine. I'm new to C++ and not sure how to proceed troubleshooting and correctly pointing the VS Code C/C++ extension to the standard library.

c_cpp_properties file for reference:

{
    "configurations": [
        {
            "name": "Mac",
            "includePath": [
                "${workspaceFolder}/**",
                "/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/c++/v1"
            ],
            "defines": [],
            "macFrameworkPath": [
                "/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/System/Library/Frameworks"
            ],
            "compilerPath": "/usr/bin/clang++",
            "cStandard": "c11",
            "cppStandard": "c++17",
            "intelliSenseMode": "macos-clang-arm64"
        }
    ],
    "version": 4
}

tasks.json file for reference:

{
  "tasks": [
    {
      "type": "cppbuild",
      "label": "C/C++: clang++ build active file",
      "command": "/usr/bin/clang++",
      "args": [
        "-std=c++17",
        "-pedantic-errors",
        "-Wall",
        "-Weffc++",
        "-Wextra",
        "-Wconversion",
        "-Wsign-conversion",
        "-ggdb",
        "-Werror",
        "-fcolor-diagnostics",
        "-fansi-escape-codes",
        "-g",
        "${file}",
        "-o",
        "${fileDirname}/${fileBasenameNoExtension}"
      ],
      "options": {
        "cwd": "${fileDirname}"
      },
      "problemMatcher": [
        "$gcc"
      ],
      "group": {
        "kind": "build",
        "isDefault": true
      },
      "detail": "Task generated by Debugger."
    }
  ],
  "version": "2.0.0"
}

Running clang++ directly on the command line produces this output:

clang++ -std=c++17 -g /Users/todd/temp/vscode-c++/hello-world.cpp -o /Users/todd/temp/vscode-c++/hello-world
/Users/todd/temp/vscode-c++/hello-world.cpp:1:10: fatal error: 'iostream' file not found
    1 | #include <iostream>
      |          ^~~~~~~~~~
1 error generated.

Update: I can now get the hello world code to compile via the command line now with the isystem argument.

clang++ -std=c++17 -isystem /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/c++/v1 hello-world.cpp -o h
ello-world
Mouser answered 18/9, 2024 at 18:23 Comment(18)
#include errors detected. Please update your includePath. This error is independent of compiling. You fix this in c_cpp_properties.json while you fix the build in tasks.json the documentation you link should explain these 2 files.Dramshop
i had edited my c_cpp_properties.json file. sorry i didn't include that for reference initially. again I'm new to c++.Mouser
What about your tasks.json which does the build?Dramshop
i added the tasks.json. thank you for the help.Mouser
@ToddMatthews 1) c_cpp_properties.json has nothing at all to do with compilation 2) You need to get the command line working first, if that doesn't work then VScode isn't going to 3) Once you get the command line working then any options you use on the command line must go into tasks.json 4) Having said all that I don't know what the solution is. If the compiler cannot find standard header files then that's a problem with how the compiler and/or header files have been installed.Alexandro
thank you for the clarifications. that helps a lot. so this sounds like a xcode / xcode command line tools issue with the latest version since that is how I have clang.Mouser
@ToddMatthews This may help, it tells you how to check where clang++ is looking for header files. It even makes a suggestion for a MacOS fix (no idea if it is applicable to you).Alexandro
that helped me get the command line working! thanks again for the help! I updated my post with the working command line argument. vscode intellisense still thinks the header files are missing but at least I can run the code from the command line for now.Mouser
Definitely something going on with the release. gcc hello.c when that includes stdio.h; gcc hello.cpp fails (same error as above) when that includes iostream. Not a VSCode issue; broken C++ compiler on 15.0 as far as I can tell (still debugging ...)Genova
After uninstall and reinstall of xcode (version 2409 reported before & after) I can compile hello-world using clang++ and g++Genova
Thank you John! I uninstalled and reinstalled xcode command line tools version 2409 and that fixed it for me as well.Mouser
@JohnKerl Thank you! BTW after reinstalling Xcode Command Line Tools (using sudo rm -rf /Library/Developer/CommandLineTools followed by xcode-select --install), I noticed that Apple Clang was downgraded from version 16.0.0 to 15.0.0. I'm not sure why macOS 15 includes version 16.0.0 if it's not yet publicly available via xcode-select --install.Cashandcarry
This is a strange issue; this works for me clang -o hello hello.cpp -lstdc++.Advocate
@ToddMatthews if you've solved your problem you should post an answer detailing how you fixed itDunaway
Hi Alan - I updated the top of the post with the solution. Let me know if that works!Mouser
No, questions are for questions, answers are for answers, don't add answers to questionsDunaway
Sounds good, Alan. I was not aware, obviously, or I would not have put my answer there. You deleted my content and told me no don't do that, which is a great first step, but you failed to help me do it correctly in the future. Stop failing and learn to be constructive. If anybody with less aggressive demeanor can help me that would be great!Mouser
Not sure how I was being aggressive? The answer box is at the bottom of the page How to Answer. Also if you want to direct a comment at a user use @ followed by their name. See also tour. Also note that your content isn't gone, you can still see it in the edit historyDunaway
M
8

Thanks to John Kerl and hsiaofei, they found out that the fix to this issue is to uninstall and reinstall the xcode command line tools.

Uninstall

sudo rm -rf /Library/Developer/CommandLineTools

Reinstall

xcode-select --install
Mouser answered 23/9, 2024 at 21:41 Comment(1)
This solved it on my end. Huge thanks for finding and sharing.Tucky

© 2022 - 2025 — McMap. All rights reserved.