How can I make a target 'makefile' in visual studio code? - c++

I'm trying to figure out how to compile my c++ code within the vs code environment.
I'm able to compile using g++ but I haven't been able to figure it out in vs code yet.
I used the answer from BeeOnRope from this question to set up my build command and the associated hotkey.
How do I set up Visual Studio Code to compile C++ code?
The error that comes out is this
make: *** No rule to make target `Makefile'. Stop.
The terminal process terminated with exit code: 2
Edit: After working on my tasks.json it looks like this, however I'm still getting the same error shown above.
{
"version": "2.0.0",
"command": "make",
"tasks":[
{
"label": "Makefile",
"group": "build",
// Show the output window only if unrecognized errors occur.
"presentation": {"reveal": "always"},
// Pass 'all' as the build target
"args": ["all"],
// Use the standard less compilation problem matcher.
"problemMatcher": {
"owner": "cpp",
"fileLocation": ["relative", "${workspaceRoot}"],
"pattern": {
"regexp": "^(.*):(\\d+):(\\d+):\\s+(warning|error):\\s+(.*)$",
"file": 1,
"line": 2,
"column": 3,
"severity": 4,
"message": 5
}
}
}
]
}

In your tasks.json, add/edit the "command" and "args" fields to have the build command line you would run manually. That could be g++, make, or whatever. See here:
https://code.visualstudio.com/docs/editor/tasks
Update:
Looking at the tasks.json file that you posted, your command needs to go inside a task. Something like this:
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"tasks": [
{
"label": "My Build",
"group": "build",
"type": "shell",
"command": "g++",
"args": [
"-o", "LexParse", "Lexxy.cpp", "Parzival.cpp"
]
}
]
}
PS, One way to format your code here is to indent it all:
int main
{
[]( auto world ) { return "Hello"s + world; } ( ", World!" );
}
Another way is to wrap it in three backticks (no need to indent, though I do here so I can have backticks within backticks):
```
int main
{
[]( auto world ) { return "Hello"s + world; } ( ", World!" );
}
```

Related

Unable to initialize vector with initializer list in visual studio code

#include <vector>
#include <iostream>
using namespace std;
int main()
{
vector<int> v = {1,2,3,4};
for (int x : v)
{
cout << x << ' ';
}
return 0;
}
When I run the above code in vscode I receive the following ERROR:
non-aggregate type 'vector' cannot be initialized with an initializer list gcc [7, 17]
NOTICE - the error includes gcc even though that is not the compiler I am using.
The code compiles fine in the terminal and in Xcode so I know it has something to do with vscode. How do I fix this issue?
NOTE - I am using I C/C++ IntelliSense with the following configurations: Compiler Path (/usr/bin/clang++) IntelliSense mode (macros-clang-arm64) Include path (${workspaceFolder}/**) C standard (c17) C++ standard (C++17).
I copied your code and named it test.cpp. I faced the same issue and I solved it by adding some configurations. Find tasks.json and add something in args.
"args": [
"-g",
"-Wall",
"-std=c++11",
"test.cpp"
]
It works on my MAC! I used command+shift+B to compile and it generated a.out after compiling. Then you can run it by F5. I also post my launch.json here, where /Users/work/Foo is my workspaceFolder. Pay attention to the line of program, I have changed this line. Good luck!
{
"version": "0.2.0",
"configurations": [
{
"name": "C/C++ Runner: Debug Session",
"type": "cppdbg",
"request": "launch",
"args": [],
"stopAtEntry": false,
"cwd": "/Users/work/Foo",
"environment": [],
"program": "/Users/work/Foo/a.out",
"MIMode": "lldb",
"externalConsole": true
}
]
}
You might post your code and how you're compiling it. The following worked for me:
#include <vector>
#include <iostream>
int main() {
std::vector<int> v = {1,2,3,4};
for (std::vector<int>::const_iterator i = v.begin(); i != v.end(); ++i) {
std::cout << *i << ' ';
}
std::cout << std::endl;
return 0;
}
Compiled and run like so:
$ g++ -std=c++11 test.cpp
$ ./a.out
1 2 3 4
$
Have you gone through this official tutorial thoroughly?
Configure VS Code for clang on macOS
Most probably you have have not configured your tasks.json correctly. Your build system is not using the right c++ standard.
Can you post your tasks.json so that we can understand your configuration exactly.
Here is the tasks.json file config from documentation:
{
// 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
}
}
]
}
Note: I don't have reputation to comment on question, so asking you here.

Why am I not getting a warning by my compiler, although I've added these compiler options in tasks.json?

I am using VSCode and have followed this tutorial: https://code.visualstudio.com/docs/cpp/config-mingw
In my tasks.json file, I have added the two command line arguments "-Wall" ,"-fsanitize=undefined" and -fsanitize=address.
The file looks like this now:
{
"version": "2.0.0",
"tasks": [
{
"type": "shell",
"label": "C/C++: g++.exe build active file",
"command": "C:\\Program Files\\mingw-w64\\x86_64-8.1.0-posix-seh-rt_v6-rev0\\mingw64\\bin\\g++.exe",
"args": [
"-g",
"-Wall",
"-fsanitize=undefined",
"-fsanitize=address",
"${file}",
"-o",
"${fileDirname}\\${fileBasenameNoExtension}.exe"
],
"options": {
"cwd": "${workspaceFolder}"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
}
}
]
}
If I run the simple piece of code below, I do not get any warning, although I'm trying to access the element at index 3 of a vector with size 1. What do I have to do in order to get a warning/error?
#include <iostream>
#include <vector>
int main()
{
std::vector<int> a(1);
std::cout << a[3];
}
There isn't a way to guarantee a warning or error for out of bounds access of a vector.
What you can do though is switch from using [] to using at like
int main()
{
std::vector<int> a(1);
std::cout << a.at(3);
}
which will generate an exception at run time and you can write code to handle that exception.

VSCode Extension: problem matching on compiler output with blank line

Im currently writing a vscode extension for a language used with Campbell Scientific dataloggers. Part of the extension I have made specifies a custom problem matcher for the compiler results.
The issue I am having is when I send the crbasic program (test.cr300) to the compiler using a task in tasks.json, I am unable to match the file name from the top of the compiler results.
Edit: This is due to the blank line in between the filename and the error messages. I currently am unable to find a way around this. Adding \n to the pattern doesn't work as expected even though it works on https://regex101.com/.
Example of returned text from the compiler:
test.cr300 -- Compile Failed!
line 12: Undeclared variable U1.
line 18: gmx600 not yet declared so cannot be aliased.
line 19: gmx600 not yet declared so cannot be aliased.
line 20: gmx600 not yet declared so cannot be aliased.
line 21: gmx600 not yet declared so cannot be aliased.
Pattern as specified in my package.json:
"pattern": [
{
"regexp": "^(.*\\.cr300).*\\n$",
"file": 1
},
{
"regexp": "^line\\s(\\d+):\\s(.+)$",
"line": 1,
"message": 2,
"loop": true
}
]
and here is my task from tasks.json when running the extension in debug.
{
"label": "CRBasic: Compiler",
"type": "shell",
"group": {
"kind": "build",
"isDefault": true
},
"options": {
"shell": {
"executable": "powershell.exe"
}
},
"command": "${config:CRBasic.Path.Compiler path}\\cr300comp.exe",
"args": [
"${file}"
],
"problemMatcher": [
"$crbasicCompiler"
]
}
You need to change the order of the regexes and add a loop attribute and change the group number for line and message
"pattern": [
{
"regexp": "^(.*\\.cr300) -- Compile Failed.*$",
"file": 1
},
{ "regexp": "^\\s*$" },
{
"regexp": "^line\\s(\\d+):\\s(.+)$",
"line": 1,
"message": 2,
"loop": true
}
]
After narrowing down the issue to a blank line, the below pattern now problem matches the compile output. In hindsight it seems obvious. Thanks to #rioV8 for the help.
"pattern": [
{
"regexp": "^(.*\\.cr300) -- Compile Failed.*$",
"file": 1
},
{
"regexp": "^.*$"
},
{
"regexp": "^line\\s(\\d+):\\s(.+)$",
"line": 1,
"message": 2,
"loop": true
}
]

Linux Visual Studio Code C++ File Not Found

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

Visual Studio Code Integrated Terminal not Displaying Text

I'm brand new to MacOS and I am attempting to set up a programming environment, my IDE of choice being Visual Studio Code. When the program runs it, by default, prints in output. However, output crashes when asked to gather input. The online solution the I found for that was to output the code through the terminal, yet now nothing is displayed in the terminal.
I'm posting this here instead of a bug report as I'm unsure whether the fault is mine or the program's.
Here is the simple code I am attempting to run:
#include <iostream>
int main()
{
int i;
std::cout << "Enter a number: ";
std::cin >> i;
std::cout << "\n" << i;
return 0;
}
When run through output, it will display the first part, then crash when input is requested. When run through the terminal, the terminal only displays: "cd "(directory location)" && g++ main.cpp -o main && "(directory location)"main" and nothing else.
Below are my tasks.json and launch.json:
tasks.json:
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"tasks": [
{
"taskName": "c++ test program",
"type": "shell",
"command": "g++",
"args": [
"-g", "main.cpp"
],
"group": {
"kind": "build",
"isDefault": true
}
}
]
}
launch.json:
{
"version": "0.2.0",
"configurations": [
{
"name": "(lldb) Launch",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceRoot}/a.out",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceRoot}",
"environment": [],
"externalConsole": true,
"MIMode": "lldb"
}
]
}
The only setting that has been changed would be "code-runner.runInTerminal" which has been set to true.
This is true when using Code Runner
so the solution is:
use Code Runner and then press Ctrl+, to edit settings then search for
code-runner.runInTerminal and set code-runner.runInTerminal to true, like so:
{
"code-runner.runInTerminal": true
}
this works fine for me.
I hope this helps.