Okay, so this is terribly frustrating.
I am simply trying to using clang++ in my VSCode editor to compile a very simple main.cpp program, located in the 'main' module of my project.
The project hierarchy is as follows:
${workspaceFolder}/
cpp/
main/
main.cpp
There are many other folders and files that I have purposefully omitted for the sake of simplicity.
Here is my terminal output from my tasks.json build task:
> Executing task: clang++ -Wall -std=c++17 -stdlib=libc++ -o main.out main.cpp -I/Users/ajm/Projects/restaurant/cpp/main/ --debug <
clang: error: no such file or directory: 'main.cpp'
clang: error: no input files
The terminal process terminated with exit code: 1
main.cpp is clearly in the folder that I am trying to include with -I (see the following screenshot):
What the heck am I missing here??? I am very tempted to switch my C++ IDE to either Visual Studio or Xcode, as besides this issue, VSCode's C++ autocomplete is buggy and terribly slow.
Note that I am new to using clang++. I have read through 5-10 other SO posts of others going through similar issues, none of which have helped. I also haven't found anything helpful on Clang quickstart guides on LLVM or Microsoft's website.
If anyone needs more info to answer the question, just let me know.
Someone, please help! Thanks in advance.
EDIT:
I figured I would also include my tasks.json, c_cpp_properties.json, and launch.json files.
tasks.json:
{
"version": "2.0.0",
"tasks": [
{
"label": "Build with Clang",
"type": "shell",
"command": "clang++",
"args": [
"-Wall",
"-std=c++17",
"-stdlib=libc++",
"-o",
"main.out",
"main.cpp",
"-I${workspaceFolder}/cpp/main/",
"--debug"
],
"group": {
"kind": "build",
"isDefault": true
}
}
]
}
c_cpp_properties.json:
{
"configurations": [
{
"name": "Mac",
"includePath": [
"${workspaceFolder}/**",
"/usr/local/boost_1_71_0"
],
"defines": [],
"macFrameworkPath": [
"/System/Library/Frameworks",
"/Library/Frameworks"
],
"compilerPath": "/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang++",
"cStandard": "c11",
"cppStandard": "c++17",
"intelliSenseMode": "${default}",
"browse": {
"path": []
}
}
],
"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": "(lldb) Launch",
"type": "cppdbg",
"request": "launch",
"program": "enter program name, for example ${workspaceFolder}/main.out",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": false,
"MIMode": "lldb",
"logging": {
"trace": true,
"traceResponse": true,
"engineLogging": true
}
}
]
}
Related
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.
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.
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.
I followed the tutorial on this webpage: https://code.visualstudio.com/docs/cpp/config-clang-mac to be able to compile c++ programs using VS Code. After doing all things requested, I am able to compile and to debug the c++ file.
However, it seems that there is an issue. In the box "PROBLEMS", I have the two following errors:
expected ';' at end of declaration [9, 23]
range-based for loop is a C++11 extension [-Wc++11-extensions] [11,
29]
My code is exactly the same as the one reported in the VS code website. I also checked all *.json files and c++17 is the default compiler.
The tasks.json is as follows:
{
// 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
}
}
]
}
The c_cpp_properties.json is as follows:
{
"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
}
When compiling, everything is fine:
> Executing task: /usr/bin/clang++ -std=c++17 -stdlib=libc++ -g /Users/stephane/Documents/c++/causality/run_mainfile.cpp -o /Users/stephane/Documents/c++/causality/run_mainfile <
Terminal will be reused by tasks, press any key to close it.
Here is the screenshot:
I am using macOS 10.15.6, VS Code 1.47.1, and C/C++ extension v0.29.
Any ideas?
#RangerBob The solution is simply what #brc-dd said but you don't have to create a new project/directory. Go to Preferences > Search > "c++ standard". Choose C++17, and VS Code will create a new file "settings.json" with that standard causing the 3 warnings to disappear.
I am facing issues in configuring VS code on mac for running c++ programs. I have already created the 3 files for setup- 1.c_cpp_properties 2.launch.json . 3.tasks.json. Please let me know the issue in the 3 json files i have added . I am getting error in my terminal-
Executing task: clang++ -std=c++17 -stdlib=libc++ vectors.cpp -o vectors.out --debug <
clang: error: no such file or directory: 'vectors.cpp'
clang: error: no input files
The terminal process command '/bin/bash -c 'clang++ -std=c++17 -stdlib=libc++ vectors.cpp -o vectors.out --debug'' failed to launch (exit code: 1)
/*Contents of 1st file:*/
{
"configurations": [
{
"name": "Mac",
"includePath": [
"${workspaceFolder}/**"
],
"defines": [],
"macFrameworkPath": [
"/Library/Developer/CommandLineTools/SDKs/MacOSX10.14.sdk/System/Library/Frameworks"
],
"compilerPath": "/usr/bin/clang",
"cStandard": "c11",
"cppStandard": "c++17",
"intelliSenseMode": "clang-x64"
}
],
"version": 4
}
/*contents of 2nd 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": "/Users/tanvi.singh/Documents/quest/learn_ds/vectors.out",
"args": [],
"stopAtEntry": true,
"cwd": "/Users/tanvi.singh/Documents/quest/learn_ds/",
"environment": [],
"externalConsole": true,
"MIMode": "lldb",
"logging": {
"trace": true,
"traceResponse": true,
"engineLogging": true
}
}
]
}
/* contents of 3rd file:*/
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"tasks": [
{
"label": "Build with Clang",
"type": "shell",
"command": "clang++",
"args": [
"-std=c++17",
"-stdlib=libc++",
"vectors.cpp",
"-o",
"vectors.out",
"--debug"
],
"group": {
"kind": "build",
"isDefault": true
}
}
]
}
All you need to do is create a file.cpp and save it somewhere.
You can use
#include <iostream>
using namespace std;
int main()
{
cout << "hello world" << endl;
return 1;
}
Then you will want to open a terminal with 'command' + 'space' this will pull up the stoplight search. Search terminal and open. Type
g++ into terminal and hit enter, it may prompt you to install some things just agree to it and it will install the compiler.
Once that is installed you can type into the terminal g++ filename.cpp
In order for this to work you will need to cd into the correct directory, or you can type the 'g++ ' part and then just drag in the filename.cpp file and it will automatically add in the files path. Enter.
You should see that an a.out file was created. To run the code, type ./a.out into the terminal or if you are not in the correct directory then ./ then drag the a.out file into terminal to paste the path. Enter. You should see the terminal print 'hello world'
I do assume you wanted to run the code with a terminal as there is not much other options for vs code on mac unless you want to use makefiles or cmake. If you would like to learn more about the terminal or makefiles (which are amazing), I can help with that too.