0

I'm using Visual Studio Code and Clang on macOS (non-M1 machine, an old 2012 laptop) to write C++ code, and I've been able to sort out most of the debug/run settings so things compile and run smoothly, except for one thing:

My experience is that the version of Clang that Apple has decided to include in their dev tools defaults to some older C++ standard, so some things (list initialization of variables, for example) won't compile without an explicit "-std=c++17" or similar line in the tasks.json file.

This is fine, except every time I create a new workspace I have to manually add that line into the tasks.json file since it's not there by default. Is there really no way to update the default settings so VS Code puts it in there automatically?

I've updated all the settings I can find to default to C++17 but that doesn't seem to change how the tasks.json file is actually created when I run a new .cpp file for the first time.

starball
  • 20,030
  • 7
  • 43
  • 238
Merlin2600
  • 68
  • 7
  • The official VSCode / clang documentation explains the settings pretty well and the changes you must make to the 3 json files: [https://code.visualstudio.com/docs/cpp/config-clang-mac](https://code.visualstudio.com/docs/cpp/config-clang-mac) – drescherjm Apr 30 '23 at 15:15
  • ***include in their dev tools defaults to some older C++ standard*** The default is c++98 I believe. – drescherjm Apr 30 '23 at 15:18
  • Yes, I've read and done this, but I have to re-do it for every new workspace manually? That was my question. – Merlin2600 Apr 30 '23 at 15:20
  • 1
    ***but I have to re-do it for every new workspace manually?*** You could just copy and paste the `.vscode` folder into a new workspace. – drescherjm Apr 30 '23 at 16:36
  • Okay, that's what I was trying to get at. There seems to be no way (within VS Code itself anyway) to eliminate that manual copy/paste step. I guess it's really a case of how VS Code expects you to use workspaces. I was thinking in terms of a "default global profile", say for all C++ projects, that would create the same .vscode files automatically any time you built a C++ workspace, but VS Code doesn't seem to be built that way. – Merlin2600 Apr 30 '23 at 16:42

1 Answers1

1

Yes, you'll have to keep specifying it manually.

That's not such a bad thing. Some people have various projects that purposely use different C++ language standards.

VS Code does not support a "global tasks" mechanism at the time of this writing (source: https://github.com/Microsoft/vscode/issues/5779#issuecomment-214620412)

Short of specifying the C++ language standard manually in each project and the non-existent "global tasks" feature, the only way you're going to get what you want is either to use a buildsystem that supports pulling a C++ language standard argument from an environment variable, or building your compiler from source after editing the sources (Ex. for you, https://opensource.apple.com/source/clang/) to change the default C++ language standard flag. For more info on those two options, see How can I find the default version of the c++ language standard used by my compiler and change it?.

Note that at least for IntelliSense purposes with the vscode-cpptools extension (which has separate configuration of the C/C++ language standard to use from whatever compiler flag you specify in your build task- if any- in your tasks.json), you can use the "C_Cpp.default.cppStandard" setting, which you can specify on a per-workspace basis by using the workspace's settings.json or c_cpp_properties.json file, or on a per-user basis, by using the user settings.json.

starball
  • 20,030
  • 7
  • 43
  • 238