VSCode C++ configuration
To set up Visual Studio Code (VSCode) to use C++17 by default when compiling your C++ programs, you can configure your build tasks and settings. Here's how to do it:
Step 1: Install the Necessary Extensions
First, ensure you have the C++ extension for VSCode installed:
Open VSCode.
Go to the Extensions view by clicking on the Extensions icon in the Activity Bar on the side of the window or pressing
Ctrl+Shift+X.Search for "C++" and install the "C/C++" extension provided by Microsoft.
Step 2: Set Up the tasks.json File
tasks.json FileNext, you'll set up a task in VSCode to compile your C++ code using C++17.
Open your project folder in VSCode.
Go to the Command Palette (
Ctrl+Shift+P) and type "Tasks: Configure Default Build Task". Select it.Choose "Create tasks.json file from template" and select "Others".
This will create a
tasks.jsonfile in a.vscodefolder in your project.Modify the
tasks.jsonfile to look something like this:jsonCopy code{ "version": "2.0.0", "tasks": [ { "label": "build", "type": "shell", "command": "/usr/bin/g++", "args": [ "-std=c++17", "-o", "${fileDirname}/${fileBasenameNoExtension}", "${file}" ], "group": { "kind": "build", "isDefault": true }, "problemMatcher": ["$gcc"], "detail": "Generated task by VS Code." } ] }/usr/bin/g++: This is the path to the g++ compiler. If it's installed elsewhere, update this path accordingly.-std=c++17: This flag tells the compiler to use C++17.${file}: This refers to the currently open file in the editor.${fileDirname}and${fileBasenameNoExtension}: These refer to the output file path and name.
Step 3: Configure the c_cpp_properties.json File
c_cpp_properties.json FileTo ensure IntelliSense and other VSCode features use the correct C++ standard, you should also configure the c_cpp_properties.json file.
Go to the Command Palette (
Ctrl+Shift+P) and type "C/C++: Edit Configurations (UI)".Set the
C++ Standardtoc++17in the UI, or directly edit thec_cpp_properties.jsonfile:
Step 4: Build and Run Your Code
Now, whenever you want to build your code:
Open the file you want to compile.
Press
Ctrl+Shift+Bto run the build task. This will compile your code using the C++17 standard.If your code compiles successfully, you can run the output binary from the terminal.
By setting up these configurations, VSCode will now use C++17 for compiling and IntelliSense.
To modify your existing tasks.json configuration to use the C++17 standard, you need to add the -std=c++17 flag to the args array. Here's how you can update your tasks.json file:
Explanation:
-std=c++17: This flag tells the compiler to use the C++17 standard.The rest of your configuration remains the same, ensuring that the active file is compiled with the correct standard.
After making this change, your tasks in VSCode will compile C++ files using the C++17 standard by default when you run the build task (Ctrl+Shift+B).
Last updated