Setting up VS code for c++ on mac - c++

I am trying to follow this guide exactly
https://code.visualstudio.com/docs/cpp/config-clang-mac
Everything works fine until I reach the step "Debug helloworld.cpp", I am able to use the "tasks.json" file to build the .cpp file and run it in the command line. However as soon as I add the "launch.json" file outlined in "Debug helloworld.cpp" everything stops working and I get the following errors as soon as I try to debug.
expected ';' at end of declaration
range-based for loop is a C++11 extension [-Wc++11-extensions]
I have looked at many other stack overflow posts claiming that clang++ is defaulting to c++03 whereas I need to be using c++11, however the guide above uses
-std=c++17 as an argument.
The guide itself even provides the following related help at the bottom.
If you see build errors mentioning "C++11 extensions", you may not have updated your task.json build task to use the clang++ argument --std=c++17. By default, clang++ uses the C++98 standard, which doesn't support the initialization used in helloworld.cpp. Make sure to replace the entire contents of your task.json file with the code block provided in the Build helloworld.cpp section.
Which I definitely did do as you can see in the tasks.json file I linked below, so I don't see what the problem could be.
Any help at all would be appreciated.
tasks.json file...
{
"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": "build"
},
{
"type": "cppbuild",
"label": "C/C++: clang++ build active file",
"command": "/usr/bin/clang++",
"args": [
"-g",
"${file}",
"-o",
"${fileDirname}/${fileBasenameNoExtension}"
],
"options": {
"cwd": "${workspaceFolder}"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "Task generated by Debugger."
}
]
}
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": "clang++ - Build and debug active file",
"type": "cppdbg",
"request": "launch",
"program": "${fileDirname}/${fileBasenameNoExtension}",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": false,
"MIMode": "lldb",
"preLaunchTask": "C/C++: clang++ build active file"
}
]
}
c_cpp_properties.json file...
{
"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
}

Your launch.json calls the "C/C++: clang++ build active file" task. You want it to call your "clang++ build active file" task.
You don't need the -stdlib=libc++ on a Mac.

That is because your VSCode Debug Extension, does little matter with configuration files.
Do not use Microsoft C/C++(Identifier: ms-vscode.cpptools, for C/C++
IntelliSense, debugging, and code browsing)!
Just use CodeLLDB to debug C++ on mac. Just use CodeLLDBv1.4.5. Do not use the latest version of CodeLLDB(CodeLLDBv1.6.x has caused much more buggers since Nov 5, 2020, and never fix. that bugger version 1.6.10 has taken me seven days to config debug C++
with VSCode on mac, made me in trouble, made me depressed even masturbate 4 times in a week.).
Setup C++11 debug with VSCode on mac: Details of successful configuration refer to here.

Related

VSCode doesn't accept c++11 in problems panel even though compiler is set to c++20 and able to run

I've set up my vs code for c++ debugging and it's running fine. However the auto detection doesn't recognize c++11 standard and marks error and warning messages. My compiler is set to c++20 and I'm able to run code without problem.
example
I've searched the internet and only find 1 similar post which said the issue went away after VSCode and extension update, so I guess this must mean I had some set up wrong. I've tried to turned off IntelliSense engine and it didn't work, and all I can find about problemmatcher said "$gcc" is recommended for macOS, so I guess that should work too.
Here's my task.json
{
"tasks": [
{
"type": "cppbuild",
"label": "C/C++: clang++ build active file",
"command": "/usr/bin/clang++",
"args": [
"-fdiagnostics-color=always",
"-g",
"${file}",
"-o",
"${fileDirname}/${fileBasenameNoExtension}",
"-std=c++20",
"-stdlib=libc++"
],
"options": {
"cwd": "${fileDirname}"
},
"problemMatcher": [
"$gcc",
],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "Task generated by Debugger."
}
],
"version": "2.0.0"
}
lauch.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": "(lldb) Launch",
"type": "cppdbg",
"request": "launch",
"program": "${fileDirname}/${fileBasenameNoExtension}",
"args": [],
"stopAtEntry": false,
"cwd": "${fileDirname}",
"environment": [],
"externalConsole": false,
"MIMode": "lldb",
"preLaunchTask": "C/C++: clang++ build active file"
}
]
}
c_cpp_properties.json
{
"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": "c17",
"cppStandard": "c++20",
"compilerArgs": [],
"intelliSenseMode": "macos-clang-x64"
}
],
"version": 4
}
Update
After I installed gcc and LLVM, somehow it messed up my configurations and all C++ compilers cannot be found in tasks. So I uninstalled all compilers, reinstalled xcode command line tools and move usr/bin to top of PATH. After the clang++ compiler reappers in build task configuration, the error squiggles are working with C++11 code. My best guess is that some settings got reset during the process and it's using the correct arguments for intellisense.

Intellisense does not show function descriptions in Visual Studio Code for C++ using the clang++ compiler on macOS

So I'm just starting to learn C++ and I decided to use Visual Studio Code as my development environment and use the clang++ compiler on macOS.
I followed the official Using Clang in Visual Studio Code guide and ended up with the following configuration files:
tasks.json (compiler build settings)
{
"version": "2.0.0",
"tasks": [
{
"type": "shell",
"label": "[mthree] 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 (debugger settings)
{
"version": "0.2.0",
"configurations": [
{
"name": "[mthree] clang++ - Build and debug active file",
"type": "cppdbg",
"request": "launch",
"program": "${fileDirname}/${fileBasenameNoExtension}",
"args": [],
"stopAtEntry": false,
"cwd": "${fileDirname}",
"environment": [],
"externalConsole": false,
"MIMode": "lldb",
"preLaunchTask": "[mthree] clang++ build active file"
}
]
}
c_cpp_properties.json (compiler path and IntelliSense settings)
{
"configurations": [
{
"name": "Mac",
"includePath": [
"${workspaceFolder}/**"
],
"defines": [],
"macFrameworkPath": [
"/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/System/Library/Frameworks"
],
"compilerPath": "/usr/bin/clang++",
"cStandard": "c17",
"cppStandard": "c++17",
"intelliSenseMode": "macos-clang-x64"
}
],
"version": 4
}
Now my problem has to do with Intellisense -- while the code completion/suggestion works fine, I just don't see any of the function descriptions.
Here is a simple example:
No description for the append() function
If I go to the definition of the string append function, it takes me to /Library/Developer/CommandLineTools/usr/include/c++/v1/string. And yes, this file happens indeed to not have any descriptive documentation in it. Here's what it says at the top:
// -*- C++ -*-
//===--------------------------- string -----------------------------------===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
Therefore, does anyone know what I should do in order for Intellisense to show the complete documentation (i.e tell me what the functions do in 'plain English')?
Thanks!
I had the same issue. I found this post. I ended up installing gcc in Homebrew, but didn't set "compilerPath": "/opt/homebrew/Cellar/gcc/11.2.0/bin/g++-11" and got the function description showing up properly.

How can I include .dylib files in ".json" configuration files? - VS Code on Mac iOS - Clang++ compiler - Language: c++

I unsuccessfully was looking 2 days on google to find a clear paragraph to describe how to include dynamic libraries (files with .dylib extension on Mac iOS) in order to be compiled by clang++ when someone is setting up the task.json and/or c_cpp_properties.json files - (prior to press F5 in order to launch the task and execute the source code)
Particularly I would like to include next two .dylib files:
/usr/local/Cellar/glfw/3.3.3/lib/libglfw.3.3.dylib;
/usr/local/Cellar/glew/2.2.0_1/lib/libGLEW.2.2.0.dylib;
Have to mention that I successfully succeeded to make the clang++ to compile in the OpenGL main.cpp file both glew.h and glfw3.h headers as per the following snippet:
// GLEW
#define GLEW_STATIC
#include <GL/glew.h>
// GLFW
#include <GLFW/glfw3.h>
...
This task was accomplished writing the next c_cpp_properties.json file:
{
"configurations": [
{
"name": "Mac",
"includePath": [
"${workspaceFolder}/**",
"${workspaceFolder}/include"
],
"defines": [],
"macFrameworkPath": [
"/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/System/Library/Frameworks"
],
"compilerPath": "/usr/bin/clang++",
"cStandard": "gnu17",
"cppStandard": "gnu++14",
"intelliSenseMode": "macos-gcc-x64"
}
],
"version": 4
}
where the magic is done by "macFrameworkPath" instruction.
But yet is not enough.
I still have this error when the clang++ compiles:
clang: error: linker command failed with exit code 1 (use -v to see invocation).
And this - as I understood after googling the solution - happens because is necessary to include those two dynamic libraries which I mentioned earlier.
But as I said in the beginning I didn't find how to do it.
Maybe someone can come up with a clear and appropriate solution.
My best guess is to add a new argument in task.json script, which by the way looks like that:
{
// 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}",
"-I/Users/Armonicus/MyProjects/C++VSCodeProjects/projects/helloworld01/include",
"-o",
"${fileDirname}/src/${fileBasenameNoExtension}",
"-Wno-deprecated",
"-Wno-pragma-once-outside-header"
],
"options": {
"cwd": "${workspaceFolder}"
},
"problemMatcher": ["$gcc"],
"group": {
"kind": "build",
"isDefault": true
}
}
]
}
Best regards to all readers.
It seems you are indeed need to pass the libs into arguments of clang++.
Try to do this with -l flag, pointing to target library.
Add
"-l /usr/local/Cellar/glfw/3.3.3/lib/libglfw.3.3.dylib"
and
"-l /usr/local/Cellar/glew/2.2.0_1/lib/libGLEW.2.2.0.dylib" to the args list of strings.
The solution to this particular problem is to add in task.json the next command arguments:
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}",
"-I/Users/Armonicus/MyProjects/C++VSCodeProjects/projects/helloworld01/include",
"/usr/local/Cellar/glfw/3.3.3/lib/libglfw.3.3.dylib",
"/usr/local/Cellar/glew/2.2.0_1/lib/libGLEW.2.2.0.dylib",
"-o",
"${fileDirname}/src/${fileBasenameNoExtension}",
"-Wno-deprecated",
"-Wno-pragma-once-outside-header"
],
"options": {
"cwd": "${workspaceFolder}"
},
"problemMatcher": ["$gcc"],
"group": {
"kind": "build",
"isDefault": true
}
}
]
}
As everyone can see, simply writing the full path of the file as additional argument will be accepted by compiler

Build and Run current active file in visual studio code for C++

I am new to cpp programming, and I am using Visual Studio Code as my IDE/editor. I created a custom build task which builds the current active file (file open in editor that I am working on). Below is my taks.json.
{
"version": "2.0.0",
"tasks": [
{
"type": "cppbuild",
"label": "C/C++: g++-10 build active file",
"command": "/usr/local/bin/g++-10",
"args": [
//"-g", // include to add debugging support
"-O2",
"${file}",
"-o",
//"${fileDirname}/${fileBasenameNoExtension}"
"build/${fileBasenameNoExtension}"
],
"options": {
"cwd": "${workspaceFolder}"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "compiler: /usr/local/bin/g++-10"
},
{
"type": "cppbuild",
"label": "C/C++: g++-10 run active file",
"command": "${workspaceFolder}/build/${fileBasenameNoExtension}",
"args": [],
"options": {
"cwd": "${workspaceFolder}"
},
"problemMatcher": [
"$gcc"
],
"group": "none",
"detail": "compiler: /usr/local/bin/g++-10"
}
]
}
and the 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++-10 - Build and debug active file",
"type": "cppdbg",
"request": "launch",
"program": "build/${fileBasenameNoExtension}",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": false,
"MIMode": "lldb",
// "preLaunchTask": "C/C++: g++-10 build active file"
}
]
}
I am able to run the build task to generate cpp object file. But how do I run the file just built from within IDE. I essentially just want to emulate build and run for quickly checking programs. I tried creating a new task (the second item in tasks.json's "tasks"), but when run it does not works. I tried finding online but to no avail.
Currently I build the file using this tasks and then switch to terminal and run the file. This workflow is ok, but had this been achieved, I would have been able to do build and run as supported in common IDEs. Is there a way to achieve this?
Thanks!
Attaching screen snips for F5 debugging issue.
To read from standard input using a debugging session, standard input must be redirected from a text file.
To redirect standard input, you need a command like this
program_name < input.txt
So you have to add these commands to you launch
"args": [
"<",
"input.txt"
],
In launch.json change
"externalConsole": false,
to
"externalConsole": true,

VS Code C++ compile multiple file

I am learning C++ and I want to use VS Code as my code editor. I created a simple project that has a file with the main method and 2 other files to define a class (a .h and a .cpp). I created the default build task in VS Code to compile my code (g++ build active file), only to get a compile error: undefined reference for the class constructor. I saw that it was related to the linker not finding the implementation because it wasn't included in the build. So I modified my build task to build all .cpp :
"version": "2.0.0",
"tasks": [
{
"type": "shell",
"label": "g++ build active file",
"command": "/usr/bin/g++",
"args": [
"-g",
"${fileDirname}/*.cpp",
"-o",
"${fileDirname}/${fileBasenameNoExtension}"
],
"options": {
"cwd": "/usr/bin"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
}
}
]
}
But now when I build the project, I get this error: /path/to/project/*.cpp": No such file or directory. Meaning that *.cpp wasn't interpreted as a wildcard. I am using the default C++ extension for VS Code if is relevant. Am I missing some configuration in my task? How can I make this work? For a large project the method in which I manually add all cpp files as arguments is obviously not appropriate so I would like to make this method to work. Thanks in advace!
This is a tough question to deal with because everyone's computers have different programs, files etc... For reference, I am on windows 10.
This worked for me and it's worth trying...
in tasks.json, either add a new task or modify the existing one to look as follows:
{
"label": "g++.exe build active file",
"args": [
"-g",
"${fileDirname}\\*.cpp",
"-o",
"${fileDirname}\\${fileBasenameNoExtension}.exe",
],
"command": "g++.exe",
"options": {
"cwd": "${workspaceFolder}",
},
"group": "build",
"type": "shell"
}
Make a new file under .vscode called c_cpp_properties.json: The following may be different if you are using another compiler or OS
{
"configurations": [
{
"name": "Win32",
"includePath": [
"C:\\msys64\\mingw64\\include\\c++",
"C:\\msys64\\mingw64\\lib\\gcc\\x86_64-w64-mingw32\\11.2.0\\include", "${workspaceFolder}/**"
],
"defines": [
"_DEBUG",
"UNICODE",
"_UNICODE"
],
"compilerPath": "C:\\msys64\\mingw64\\bin\\gcc.exe",
"cStandard": "c11",
"cppStandard": "c++20",
"intelliSenseMode": "gcc-x64"
}
],
"version": 4
}
Now in launch.json, you need to change the "preLaunchTask" to match the task's name we just created in tasks.json. This will tell vscode to first compile/build the program... then to run it:
Mine now looks like this:
{
"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": false,
"MIMode": "gdb",
"miDebuggerPath": "C:\\msys64\\mingw64\\bin\\gdb.exe",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
],
"preLaunchTask": "g++.exe build active file"
}
]
}
Note that the name we chose is not exactly describing what the new task actually does, feel free to change it, but if you do... make sure to update both the "label" in tasks.json and "preLaunchTask" in launch.json to be identical.