VScode C/C++ extension setup and configuration (macOS) - c++

I am new to C++ and am still trying to set up the C/C++ extension in vscode.
I followed this tutorial provided by vscode official:
https://code.visualstudio.com/docs/cpp/config-clang-mac#_cc-configuration.
With the "lauch.json", "tasks.json", and "c_cpp_properties.json" files completed, I clicked the "play" icon, but my debugger still always show error with ";" and would open the launch.json file.
I think I am encountering the exact same problem with this post but its solution fails to solve mine: Mac VSCode Debugger always show error about ';' and ':'.
This is my code:
#include <iostream>
#include <vector>
#include <string>
using namespace std;
int main()
{
vector<string> msg {"Hello", "C++++", "World", "from", "VS Code", "and the C++ extension!"};
for (const string& word : msg)
{
cout << word << " ";
}
vector<string> aaa {"H", "E", "L", "L", "O"};
for (const string& word : aaa)
{
cout << word << " ";
}
cout << endl;
}
This is my launch.json file:
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "(lldb) Launch",
"type": "cppdbg",
"request": "launch",
"program": "enter program name, for example ${workspaceFolder}/a.out",
"args": [],
"stopAtEntry": false,
"cwd": "${fileDirname}",
"environment": [],
"externalConsole": false,
"MIMode": "lldb"
}
]
}
This is my tasks.json file:
{
"tasks": [
{
"type": "cppbuild",
"label": "C/C++: clang++ build active file",
"command": "/usr/bin/clang++",
"args": [
"-fdiagnostics-color=always",
"-g",
"${file}",
"-o",
"${fileDirname}/${fileBasenameNoExtension}"
],
"options": {
"cwd": "${fileDirname}"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "Task generated by Debugger."
}
],
"version": "2.0.0"
}
This is my c_cpp_properties.json file:
{
"configurations": [
{
"name": "Mac",
"includePath": [
"${workspaceFolder}/**"
],
"defines": [
],
"macFrameworkPath": [
"/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/System/Library/Frameworks"
],
"compilerPath": "/usr/bin/clang",
"cStandard": "c11",
"cppStandard": "c++17",
"intelliSenseMode": "clang-x64"
}
],
"version": 4
}
Thanks!

The problem is that the program field is not right in launch.json, if your CPP name is your-program.cpp, you could set the program field to your-program.
Because the compiling output file name is "${fileDirname}/${fileBasenameNoExtension}" as you set in tasks.json:
{
"version": "0.2.0",
"configurations": [
{
"name": "(lldb) Launch",
"type": "cppdbg",
"request": "launch",
"program": "your-program", // set it to right name.
"args": [],
"stopAtEntry": false,
"cwd": "${fileDirname}",
"environment": [],
"externalConsole": false,
"MIMode": "lldb"
}
]
}

Related

I am having a problem using intellisense for my custom headers file in for c++ in vscode

I tried compiling my shopping program with IntelliSense in vs code but is not working. Both the program and the header file are in the same folder called Shopping_program but that folder is in another folder called c++ which is where my .vcode folder is.
shopping_program.cpp
#include "shopping.h"
int main(){
do{
clear();
choice();
calculateTotal();
receipt();
} while(again());
cout << "Thanks for shopping";
}
launch.json
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "g++.exe - Build and debug active file",
"type": "cppdbg",
"request": "launch",
"program": "${fileDirname}\\${fileBasenameNoExtension}.exe",
"args": [],
"stopAtEntry": false,
"cwd": "${fileDirname}",
"environment": [],
"externalConsole": true,
"MIMode": "gdb",
"miDebuggerPath": "C:\\msys64\\mingw64\\bin\\gdb.exe",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
],
"preLaunchTask": "C/C++: g++.exe build active file"
}
]
}
c_cpp_properties.json
{
"configurations": [
{
"name": "Win32",
"includePath": [
"${workspaceFolder}/**",
"${workspaceFolder}/Shopping_program/"
],
"defines": [
"_DEBUG",
"UNICODE",
"_UNICODE"
],
"compilerPath": "C:/msys64/mingw64/bin/g++.exe",
"cStandard": "gnu17",
"cppStandard": "gnu++17",
"intelliSenseMode": "windows-gcc-x64"
}
],
"version": 4
}
tasks.json
{
"tasks": [
{
"type": "cppbuild",
"label": "C/C++: g++.exe build active file",
"command": "C:/msys64/mingw64/bin/g++.exe",
"args": ["-g", "${file}", "-o", "${fileDirname}\\${fileBasenameNoExtension}.exe",],
"options": {
"cwd": "${fileDirname}"
},
"problemMatcher": ["$gcc"],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "compiler: C:/msys64/mingw64/bin/g++.exe"
}
],
"version": "2.0.0"
shopping.h
#ifndef SHOPPING
#define SHOPPING
#include <iostream>
#include <string>
#include <vector>
#include <array>
#include <algorithm>
#include <fstream>
#include <iomanip>
using std::cin;
using std::cout;
const std::array <std::string, 3> products = {"Bread","Egg","Jam"}; //the product in the shop
const std::array <float, 3> price = {10.0,5.50,7.20}; //the prices for the items
void clear();
void receipt();
void calculateTotal();
void choice();
bool again();
#endif

C++ Uniform initialization doesn't work with "expected ';' at end of declaration"

I'm the beginner of C++. I set development environment in vscode in Mac.
But I trapped in variable initialization.
Copy initialization and direct initialization are ok, but uniform initialization doesn't work.
#include <iostream>
int main() {
int a(0);
int b = 0;
int c{0};
int d = {0};
int e;
std::cout << "Hello World";
return 0;
}
In this code, int c{0}; doesn't work. It failed to build.
It printed error: expected ';' at end of declaration
What can I try to solve this problem??
I don't know if it helps, but I'll upload ".vscode".
// c_cpp_properties.json
{
"configurations": [
{
"name": "Mac",
"includePath": ["${workspaceFolder}/**"],
"defines": [],
"macFrameworkPath": [
"/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/System/Library/Frameworks"
],
"compilerPath": "/usr/bin/clang",
"cStandard": "c11",
"cppStandard": "c++17",
"intelliSenseMode": "clang-x64"
}
],
"version": 4
}
// launch.json
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "clang++ - Build and debug active file",
"type": "lldb",
"request": "launch",
"program": "${fileDirname}/${fileBasenameNoExtension}",
"args": [],
"stopAtEntry": true,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": false,
"MIMode": "lldb",
"preLaunchTask": "clang++ build active file"
}
]
}
// tasks.json
{
"version": "2.0.0",
"tasks": [
{
"type": "shell",
"label": "clang++ build active file",
"command": "/usr/bin/clang++",
"args": [
"-std=c++17",
"-g",
"${file}",
"-o",
"${fileDirname}/${fileBasenameNoExtension}",
"&&",
"${fileDirname}/${fileBasenameNoExtension}"
],
"options": {
"cwd": "${workspaceFolder}"
},
"problemMatcher": [
"$gcc"
],
"group": "build"
},
{
"type": "cppbuild",
"label": "C/C++: clang++ 활성 파일 빌드",
"command": "/usr/bin/clang++",
"args": [
"-fdiagnostics-color=always",
"-g",
"${file}",
"-o",
"${fileDirname}/${fileBasenameNoExtension}"
],
"options": {
"cwd": "${fileDirname}"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "디버거에서 생성된 작업입니다."
}
]
}

C++ not executing on M1 Mac VS Code

Code able to compile, but not executing, i.e. output not showing in debug window, breakpoints are not hit. Same configuration was working on non M1 Mac
Task.json:
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"tasks": [
{
"type": "shell",
"label": "clang++ build active file",
"command": "/usr/bin/clang++",
"args": [
"-std=c++17",
"-stdlib=libc++",
"-g",
"${file}",
"-o",
"${fileDirname}/${fileBasenameNoExtension}"
],
"options": {
"cwd": "${workspaceFolder}"
},
"problemMatcher": ["$gcc"],
"group": {
"kind": "build",
"isDefault": true
}
}
]
}
launch.json:
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "clang++ - Build and debug active file",
"type": "cppdbg",
"request": "launch",
"program": "${fileDirname}/${fileBasenameNoExtension}",
"args": [],
"stopAtEntry": true,
"cwd": "${fileDirname}",
"environment": [],
"externalConsole": false,
"MIMode": "lldb",
"preLaunchTask": "C/C++: clang++ build active file"
}
]
}
cp_properties.json
{
"configurations": [
{
"name": "Mac",
"includePath": ["${workspaceFolder}/**"],
"defines": [],
"macFrameworkPath": [
"/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/System/Library/Frameworks"
],
"compilerPath": "/usr/bin/clang",
"cStandard": "c11",
"cppStandard": "c++17",
"intelliSenseMode": "clang-x64"
}
],
"version": 4
}
Terminal Output:
Starting build...
/usr/bin/clang++ -fdiagnostics-color=always -g
/Users/gyan/workplace/vscode/test/test.cpp -o /Users/gyan/workplace/vscode/test/test
Build finished successfully.
Terminal will be reused by tasks, press any key to close it.
test.cpp:
#include <iostream>
#include <sstream>
using namespace std;
int main(){
string s = "my name is Gyan p\n";
istringstream iss(s);
string word;
cout<<"s";
while(iss >> word){
cout<<word<<" ";
}
return 0;
}
Not getting any idea of the issue
I fixed it using following config changes in launch.json:
"-arch", "x86_64",
"targetArchitecture": "x86_64",
"osx": {
"preLaunchTask": "C/C++: g++ build active file",
"MIMode": "lldb"
}

It's no any response after starting debugging

According to the instruction: https://code.visualstudio.com/docs/cpp/config-mingw, .vscode files are set. Then, I add the MinGW64 path to the system path. But, when I start debugging, VS Code hangs and no finish, I also scan the Console by the steps: Help->Toggle Developer Tools, and the Console displays the following error, and I don't know how to solve.
enter image description here
The followings are my source code and both JSON files:
// source code file
#include <iostream>
#include <vector>
#include <string>
using namespace std;
int main()
{
vector<string> msg {"Hello", "C++", "World", "from", "VS Code", "and the C++ extension!"};
for (const string& word : msg)
{
cout << word << " ";
}
cout << endl;
}
// launch.json
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "g++.exe build and debug active file",
"type": "cppdbg",
"request": "launch",
"program": "${fileDirname}\\${fileBasenameNoExtension}.exe",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": true,
"MIMode": "gdb",
"miDebuggerPath": "C:\\Program Files (x86)\\mingw-w64\\i686-7.2.0-win32-sjlj-rt_v5-rev1\\mingw32\\bin\\gdb.exe",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
],
"preLaunchTask": "g++.exe build active file"
}
]
}
// tasks.json
{
"version": "2.0.0",
"tasks": [
{
"type": "shell",
"label": "g++.exe build active file",
"command": "C:\\Program Files (x86)\\mingw-w64\\i686-7.2.0-win32-sjlj-rt_v5-rev1\\mingw32\\bin\\g++.exe",
"args": [
"-g",
"${file}",
"-o",
"${fileDirname}\\${fileBasenameNoExtension}.exe"
],
"options": {
"cwd": "C:\\Program Files (x86)\\mingw-w64\\i686-7.2.0-win32-sjlj-rt_v5-rev1\\mingw32\\bin"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
}
}
]
}

Can't build c++ in Visual Studio Code

It seems like this, except that I followed it and I still unable to make it work in VS Code. This is the directory that I use:
C:\Users\EKI\Desktop\projects_cpp
As the instructions goes, I put the .json files in \project_cpp.vscode and the .cpp file in \projects_cpp\helloworld.
My .cpp file:
#include <iostream>
using namespace std;
int main()
{
cout << "Hello World";
}
The .json files are in the following:
launch.json
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "(gdb) Launch",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceFolder}/helloworld.exe",
"args": [],
"stopAtEntry": true,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": true,
"MIMode": "gdb",
"miDebuggerPath": "C:\\mingw-w64\\x86_64-8.1.0-win32-seh-rt_v6-rev0\\mingw64\\bin\\gdb.exe",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
]
}
]
}
settings.json
{
"files.associations": {
"ostream": "cpp"
}
}
tasks.json
{
"version": "2.0.0",
"tasks": [
{
"label": "build hello world",
"type": "shell",
"command": "g++",
"args": [
"-g",
"${file}",
"-o",
"$${workspaceFolder}\\${fileBasenameNoExtension}",
],
"group": {
"kind": "build",
"isDefault": true
}
}
]
}
c_cpp_properties.json
{
"configurations": [
{
"name": "Win32",
"includePath": [
"${workspaceFolder}/**"
],
"defines": [
"_DEBUG",
"UNICODE",
"_UNICODE"
],
"compilerPath": "C:\\mingw-w64\\x86_64-8.1.0-posix-seh-rt_v6-rev0\\mingw64\\bin\\g++.exe",
"cStandard": "c11",
"cppStandard": "c++17",
"intelliSenseMode": "gcc-x64"
}
],
"version": 4
}
I tried turning off controlled folder access but still it doesn't work.
Here is the error message
Executing task: g++ -g c:\Users\EKI\Desktop\projects_cpp\helloworld\helloworld.cpp -o
$C:\Users\EKI\Desktop\projects_cpp\helloworld <
g++.exe: error: c:UsersEKIDesktopprojects_cpphelloworldhelloworld.cpp:
No such file or directory g++.exe: fatal error: no input files
compilation terminated. The terminal process terminated with exit
code: 1