Linux Visual Studio Code C++ File Not Found - c++

New to visual studio code. I've setup a basic opencv project and am trying to get it to compile. I've setup my include path in the c_cpp_properties.json and get auto-completion correctly. However, when I go to compile g++ says it doesn't know of the included directory. What is the best way to fix this issue? My assumption is I need to also pass it to the tasks.json, but i'm unsure of a good way to do that for include directories.
c_cpp_properties.json
{
"configurations": [
{
"name": "Linux",
"includePath": [
"${workspaceFolder}/**",
"/usr/local/include/opencv4"
],
"defines": ["_DEBUG"],
"compilerPath": "/usr/bin/g++",
"cStandard": "c11",
"cppStandard": "c++17",
"intelliSenseMode": "clang-x64"
}
],
"version": 4
}
tasks.json
{
"version": "2.0.0",
"tasks": [
{
"label": "build hello world",
"type": "shell",
"command": "g++",
"args": [
"-g", "main.cpp"
],
"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": "(gdb) Launch",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceFolder}/a.out",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": true,
"MIMode": "gdb",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
],
"preLaunchTask": "build hello world"
}
]
}
Basic code to show an image
#include <iostream>
#include <opencv2/opencv.hpp>
using namespace cv;
int main(int argc, const char** argv)
{
Mat image;
image = imread("./images/test.jpg");
if(!image.data)
{
std::cout << "No image data" << std::endl;
return -1;
}
namedWindow("Display Image",WINDOW_AUTOSIZE);
imshow("Display Image", image);
waitKey(0);
return 0;
}
Error
> Executing task: g++ -g main.cpp <
main.cpp:2:10: fatal error: opencv2/opencv.hpp: No such file or directory
#include <opencv2/opencv.hpp>
^~~~~~~~~~~~~~~~~~~~
compilation terminated.
The terminal process terminated with exit code: 1
Terminal will be reused by tasks, press any key to close it.

c_cpp_properties.json is the config for IntelliSense only. You have to configure required compiler arguments (include dirs) in tasks.json separately.
tasks.json
{
"version": "2.0.0",
"tasks": [
{
"label": "build hello world",
"type": "shell",
"command": "g++",
"args": [
"-I/usr/local/include/opencv4",
"-g",
"main.cpp"
],
"group": {
"kind": "build",
"isDefault": true
}
}
]
}

Related

Failing to setup VS Code with MingW-64; can't get simple HelloWorld.cpp to work

I have so far failed to get VS Code to compile a simple helloworld.cpp file with MingW-64 (v8.1.0). I'm doing this on windows 10 for the first time, and have had issues at pretty much every stage of trying to get MingW-64 to work with VS Code in one way or another, following this tutorial roughly: https://code.visualstudio.com/docs/cpp/config-mingw
I have setup my environment variables to include the path to mingw64, and can see the version numbers in terminal, no issue.
When I run my code, the terminal shows that my build finished successfully, debug returns "ERROR: Unable to start debugging. Unexpected GDB output from command "-exec-run". During startup program exited with code 0xc0000139." and the output window just shows that it ran the .exe and .cpp file and completed. I can't see that it has printed to terminal at all.
Extensions;
C/C++,
code runner,
ESLint,
npm
HelloWorld.cpp;
#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;
}
c_cpp_properties.json
{
"configurations": [
{
"name": "Win32",
"includePath": [
"${workspaceFolder}/**"
],
"defines": [
"_DEBUG",
"UNICODE",
"_UNICODE"
],
"windowsSdkVersion": "10.0.19041.0",
"compilerPath": "C:\\mingw64\\bin\\g++.exe",
"cStandard": "c17",
"cppStandard": "c++17",
"intelliSenseMode": "gcc-x64"
}
],
"version": 4
}
launch.json
{
"version": "0.2.0",
"configurations": [
{
"name": "C/C++: g++.exe build and debug active file",
"type": "cppdbg",
"request": "launch",
"program": "${fileDirname}\\a.exe",
"args": [],
"stopAtEntry": false,
"cwd": "C:\\mingw64\\bin",
"environment": [],
"externalConsole": true,
"MIMode": "gdb",
"miDebuggerPath": "C:\\mingw64\\bin\\gdb.exe",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
},
{
"description": "Set Disassembly Flavor to Intel",
"text": "-gdb-set disassembly-flavor intel",
"ignoreFailures": true
}
],
"preLaunchTask": "C/C++: g++.exe build active file"
}
]
}
tasks.json
{
"version": "2.0.0",
"tasks": [
{
"label": "build",
"type": "shell",
"command": "C:\\mingw64\\bin\\g++",
"args": [
"-g",
"-o",
"a.exe",
"HelloWorld.cpp"
],
"group": "build",
"presentation": {
"reveal": "silent"
},
"problemMatcher": "$msCompile"
}
]
}

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"
}

How to fix "undefined symbols for architecture arm64"

I'm running VS Code on MacOS, and I'm using clang to compile a simple "Hello World!" program in C++. However, when I try to run my program, VS Code gives me the following error message: Undefined symbols for architecture arm64: followed by dozens of references to the std library. At the bottom of the terminal, it says:
ld: symbol(s) not found for architecture arm64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
Build finished with error(s).
The terminal process terminated with exit code: -1.
(a) What does this mean? and (b) how can I fix it?
HelloWorld.cpp
#include <iostream>
using namespace std;
int main()
{
cout << "Hello World!" << endl;
return 0;
}
tasks.json
{
"tasks": [
{
"type": "cppbuild",
"label": "C/C++: clang build active file",
"command": "/usr/bin/clang",
"args": [
"-g",
"${file}",
"-o",
"${fileDirname}/${fileBasenameNoExtension}"
],
"options": {
"cwd": "${fileDirname}"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "Task generated by Debugger."
}
],
"version": "2.0.0"
}
launch.json
{
"version": "0.2.0",
"configurations": [
{
"name": "clang - Build and debug active file",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceFolder}/HelloWorld.cpp",
"args": [],
"stopAtEntry": false,
"cwd": "${fileDirname}",
"environment": [],
"externalConsole": false,
"MIMode": "lldb",
"preLaunchTask": "C/C++: clang build active file"
}
]
}
Thanks!
If you have multiple CPP files in the project then you need to add "${fileDirname}/*.cpp"
tasks.json
"version": "2.0.0",
"tasks": [
{
"type": "shell",
"label": "clang++ build active file",
"command": "/usr/bin/clang++",
"args": [
"-std=c++17",
"-stdlib=libc++",
"-g",
"${fileDirname}/*.cpp",
// "${file}",
"-o",
"${fileDirname}/${fileBasenameNoExtension}"
],
"options": {
"cwd": "${workspaceFolder}"
},
"problemMatcher": ["$gcc"],
"group": {
"kind": "build",
"isDefault": true
}
}
]
}
If you're having a vector; error from vscode also add the c_cpp_properties.json file in .vscode directory :
{
"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
}

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