VS Code - Only one cpp file from a project compiles - c++

Okay so i have an error when am trying to run/compile the code.
Am guessing the problem is that my folder is not recognised as a project folder (i do not know how to solve that) and it compiles only one cpp file when it is needed to compile two of them.
error:
undefined reference to `Test2::fun()'
collect2.exe: error: ld returned 1 exit status
class.h
#include <iostream>
class Test2
{
public:
int a = 25;
void fun();
};
class.cpp
#include <iostream>
#include "class.h"
void Test2::fun()
{
std::cout << a;
}
main.cpp
#include <iostream>
#include "class.h"
int main()
{
Test2 e;
e.fun();
}
am not sure but that could be helpful to include my json files so... will include them
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": false,
"MIMode": "gdb",
"miDebuggerPath": "C:\\MinGW\\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"
}
]
}
tasks.json
{
"tasks": [
{
"type": "cppbuild",
"label": "C/C++: g++.exe build active file",
"command": "C:\\MinGW\\mingw64\\bin\\g++.exe",
"args": [
"-g",
"${file}",
"-o",
"${fileDirname}\\${fileBasenameNoExtension}.exe"
],
"options": {
"cwd": "${workspaceFolder}"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "Task generated by Debugger."
}
],
"version": "2.0.0"
}
I am new into vs code, sorry if that is a pretty easy to solve problem but am trying to figure it out and i can not.
As i said am thinking that vs code does not recognise it as a project and runs only main.cpp cause the error looks like that when am trying to run the code.
I was trying to run same thing on codeblocks and visualstudio2019 and it was working corretly so there is a problem with my vs code set up i guess.
Thanks for help and have a goood day <3

Related

Configure c++ debugger in VSCode

I recently moved to VSCode and im a little be lost.
If i compile my program with this console command
g++ -Wall -o main main.cpp src/*.cpp -I included
It compiles and generates the .exe file correctly.
But i have a bug in it, so i want to use the debugger to know what is happening.
When i hit run -> star debuggin in VSCode i get a console message of "not such file or directory" for one of my included files. So, i think i have some wrong configuration in my launch.json
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 - Compilar y depurar el archivo activo",
"type": "cppdbg",
"request": "launch",
"program": "C:\\Users\\Don\\Desktop\\IColections\\main.exe",
"args": ["-I[included/*/*.h]"],
"stopAtEntry": false,
"cwd": "C:\\Users\\Don\\Desktop\\IColections",
"environment": [],
"externalConsole": true,
"MIMode": "gdb",
"miDebuggerPath": "C:\\MinGW\\bin\\gdb.exe",
"setupCommands": [
{
"description": "Habilitar la impresión con sangría para gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
],
"preLaunchTask": "C/C++: g++.exe compilar archivo activo"
}
]
}
i do not relly underestand what should i put in args or environment
EDIT:
this is the error message
C:\MinGW\bin\g++.exe -g *.cpp -o C:\Users\Don\Desktop\IColections\main.exe
main.cpp:2:31: fatal error: ../included/Fruta.h: No such file or directory
#include "../included/Fruta.h"
^
compilation terminated.
and this is main.cpp
#include <iostream>
#include "../included/Fruta.h"
#include "../included/Uva.h"
#include "../included/Naranja.h"
#include "../included/List.h"
#include "../included/List_Iterator.h"
int main(){
ICollection* fruta = new List;
Uva* misUvas = new Uva;
misUvas->setKg(80.5);
misUvas->setRacimos(15000);
Naranja* misNaranjas = new Naranja;
misNaranjas->setKg(200);
misNaranjas->setNaranjas(1000);
fruta->agregar(misNaranjas);
fruta->agregar(misUvas);
IIterator* it = dynamic_cast<List*> (fruta)->getIterator();
ICollectible* elem;
Fruta* frut;
if(!it->hasCurrent()){
}
while(it->hasCurrent()){
elem = it->getCurrent();
frut = dynamic_cast <Fruta*> (elem);
frut->printFruta();
it->next();
}
system("pause");
return 0;
}
tasks.json
{
"tasks": [
{
"type": "cppbuild",
"label": "C/C++: g++.exe compilar archivo activo",
"command": "C:\\MinGW\\bin\\g++.exe",
"args": [
"-g",
"*.cpp",
"-o",
"${fileDirname}\\main.exe"
],
"options": {
"cwd": "${fileDirname}"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "Tarea generada por el depurador."
}
],
"version": "2.0.0"
}
If you already have a compiled version with debug information (-g) then you do not need to include the header files again.
Just remove the line "preLaunchTask": "C/C++: g++.exe compilar archivo activo" from the configuration since your program is already compiled.

Debugging C/C++ in VSCode when using different directories

I want to be able to debug my program when using different directories .
Let's take this example below :
Where main.c contains :
#include <stdio.h>
#include "lib/A.h"
int main()
{
test();
return 0;
}
A.h :
#pragma once
#include <stdio.h>
void test();
A.c :
#include "A.h"
void test()
{
printf("A\n");
printf("B\n");
}
When I try to debug this program(Breakpoint in the main program), I get this error below :
The prelaunchtask C/C++ g++.exe build active file temrinated with exit code -1
However when if I remove A.c and write the whole function in the header file , debugging will work correctly :
A.h :
#pragma once
#include <stdio.h>
void test()
{
printf("A\n");
printf("B\n");
}
I might have to change something in tasks.json or settings.json, but I don't know what should be modified so that I could debug a program with A.c - A.h - main.c .
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": "${workspaceFolder}"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "Task generated by Debugger."
}
],
"version": "2.0.0"
}
launch.json :
{
"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:\\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"
}
]
}
I tried some solutions that I found but none of them worked that's why I wanted to post this question

I'm Unable to debug my C++ code in VSCode

I'm trying to debug my C++ file but whenever I press the Run >> start debugging option, it just runs and quit without any errors or anything. My program is not running...
Here's my code:
#include <vector>
#include <string>
#include <iostream>
using namespace std;
int main()
{
int rows, question;
std::string length;
cin >> rows;
for (int i = 0; i < rows; i++)
{
cin >> length;
}
return 0;
}
Here's what it shows in the terminal after clicking on debug:
cmd /C "c:\Users\intel\.vscode\extensions\ms-vscode.cpptools-0.29.0\debugAdapters\bin\WindowsDebugLauncher.exe --stdin=Microsoft-MIEngine-In-oxw2rz3u.v3w --stdout=Microsoft-MIEngine-Out-5me5n3jp.wq2 --stderr=Microsoft-MIEngine-Error-wg1xnokk.ddw --pid=Microsoft-MIEngine-Pid-kfv0gezm.4wp --dbgExe=C:\MinGW\bin\gdb.exe --interpreter=mi "
C:\Users\intel\Desktop\Python programs>
Here's what it shows in the output window:
Here's my tasks.json:
{
"version": "2.0.0",
"_runner": "terminal",
"tasks": [
{
"type": "shell",
"label": "C/C++: g++.exe build active file",
"command": "C:\\MinGW\\bin\\g++.exe",
"args": [
"-g",
"${file}",
"-o",
"${fileDirname}\\${fileBasenameNoExtension}.exe"
],
"options": {
"cwd": "${workspaceFolder}"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "test",
"isDefault": true
}
}
]
}
Here's my 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": "${fileDirname}/a.exe",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"miDebuggerPath": "C:\\MinGW\\bin\\gdb.exe",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
]
},
{
"name": "Python: Current File",
"type": "python",
"request": "launch",
"program": "${file}",
"console": "integratedTerminal"
},
]
}
I am using a windows machine and a MinGW for C++
I use VSCode to develop in C++ too, but not tryed to use with MinGW yet. I use it with MSVC and GCC (Linux).
My launch configuration is sligh different from yours, using GCC.
{
"linux": {
"MIMode": "gdb",
"miDebuggerPath": "/usr/bin/gdb",
"setupCommands": [
{
"description": "Enable pretty-printing",
"text": "-enable-pretty-printing"
}
]
},
"name": "Run gcc (linux) x64 debug",
"request": "launch",
"program": "/projects/CPP/GHDWS/build/gcc-linux/x64/debug/GHDWS",
"cwd": "/projects/CPP/GHDWS/build/run",
"args": [],
"preLaunchTask": "Build gcc x64 debug",
"type": "cppdbg",
"externalConsole": false
},
I made my own system build, that automatically configure VSCode to your project. If you're willing, you can try use it.
CppMagic

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

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