launch: program "directory of the program " does not exit error - c++

I have a header file, a CPP file that defines member functions of the header file and a caller main file that sets and gets some values. When I run the code in ubuntu 20.04 vs code, I get the error "launch:program does not exist error" all the time.
When it comes to running CPP programs without headers involved, it seems that the program builds and runs but after opening header files inside the project folder, I started to get the same type of error. I create the launch json file and try to build it basically. Should I configure json files for the headers as well. They are in the same folder so I thought this would not be required.
I have run the codes in visual studio already and there it works. As you can see from the terminal window, I get undefined reference error. I haven't uploaded the codes because the class and the member functions and all the data variables seem to match with each other and doesn't give any error. I would appreciate it if anyone would help me with this code, I switched to linux and trying to get used to vs code.
this is the json file I created.
{
"version": "2.0.0",
"tasks": [
{
"type": "cppbuild",
"label": "C/C++: g++ build active file",
"command": "/usr/bin/g++",
"args": [
"-fdiagnostics-color=always",
"-g",
"${file}",
"-o",
"${fileDirname}/${fileBasenameNoExtension}"
],
"options": {
"cwd": "${fileDirname}"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "compiler: /usr/bin/g++"
}
]
}
I followed the exact steps explained in the vs code's Getting started c++ on Linux.

Related

Making debug and release configs for c++ in VS Code

I've been looking everywhere for an answer to this question. I'm holding off using an IDE until I can confidently use g++, makefiles, and JSON configs. My issue is that in VS Code, setting up SDL2, I'm not sure how to make a release config (by that I mean code cleaning). Here's what I have in my tasks.json file for debugging.
{
"version": "2.0.0",
"tasks": [
{
"type": "shell",
"label": "SDL2 Debug",
"command": "C:\\msys64\\mingw64\\bin\\g++.exe",
"args": [
"-g",
"src\\*.cpp",
"-o",
"bin\\debug\\main.exe",
"-IC:/SDL2/include",
"-LC:/SDL2/lib",
"-lmingw32",
"-lSDL2main",
"-lSDL2",
"-mwindows"
],
"options": {
"cwd": "${workspaceFolder}"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "Task generated by Debugger."
}
],
}
A lot of this I just copied off the internet. Which is fine because it works, but not fine because I don't understand JSON or how VS Code uses it. And I can't find any references to explain what all this fancy stuff means. And the main problem is that this just creates a debug build, I'm not even sure if the arguments are correct or if it's messy. I want to create a release config alongside this to clean the code once the project is finished. All I'm looking for is a push in the right direction. Not expecting an outright solution to my problem (although that would be great), I just want to understand what I'm supposed to do here. Any references to read would be awesome, I don't know where to look at this point in time.

Run C++ in Visual Studio Code without opening it with `code .` from CMD

I set up VSCode to compile C++, but in order to be able to run it, I first have to open CMD, navigate to the location of the .cpp file I want to open and then run code . (This opens VSCode and then I can compile the file with Ctrl+Shift+B.) This is tedious and it would be wonderful if I had a script that enabled me to run C++ without having to do the above procedure every time.
Thank you for your help. :)
EDIT
This is my tasks.json file:
{
"version": "2.0.0",
"tasks": [
{
"type": "cppbuild",
"label": "C/C++: g++.exe build active file",
"command": "C:\\msys64\\mingw64\\bin\\g++.exe",
"args": [
"-fdiagnostics-color=always",
"-g",
"${file}",
"-o",
"${fileDirname}\\${fileBasenameNoExtension}.exe"
],
"options": {
"cwd": "C:\\msys64\\mingw64\\bin"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "compiler: C:\\msys64\\mingw64\\bin\\g++.exe"
}
]
}
For your particular case with a single .cpp file and g++ installed, you should be able to compile from the command line using a command similar to below:
g++ -fdiagnostics-color=always -g <.cpp file name> -o <output binary name>
Make sure to replace <.cpp file name> with your actual filename and <output binary name> with whatever you want to name your executable.
It appears you are first starting to learn how to develop in C++. This solution above should work for now.
When you start to write bigger programs and decide to split your source code into multiple files, you will eventually want to learn how to use a build system that can help automate the compilation of multiple source files. A popular build system is GNU Make, this would probably be a good tool to learn. You can write makefiles which instruct how to build the code, and then VSCode can be configured to use make to read the makefiles and build the code.

Undefined reference to vtable when functions are defined in VS Code

I've seen a lot of questions about this, but can't seem to see what I'm missing. I am fairly new at C++. I am using Visual Studio Code with G++ and MINGW32 10.3.0. When I attempt to run test.cpp (below) I receive two errors:
...test.cpp:7: undefined reference to 'QData::getDataPacket(void*)
...undefined reference to 'vtable for QData'
// qdata.h
#ifndef QDATA_H_
#define QDATA_H_
//Define generic queue data
class QData {
private:
int data = 17;
public:
void virtual getDataPacket(void* dataptr);
void virtual setDataPacket(void* dataptr);
};
#endif
// qdata.cpp
#include "qdata.h"
void QData::getDataPacket(void* dataptr) {
*(int*)dataptr = data;
}
void QData::setDataPacket(void* dataptr) {
data = *(int*)dataptr;
}
// test.cpp
#include <iostream>
#include "qdata.h"
int main() {
QData qqq;
int a;
qqq.getDataPacket(&a);
std::cout << a << std::endl;
return 0;
}
I know the code works because it was originally all in one file and compiled fine. From my research this is maybe a linking issue? Most questions related to this refer to needing to define your virtual functions, but I have already done that.
If I use the following command in the terminal, binary.exe runs correctly (the output is 17):
g++ -o binary test.cpp qdata.cpp
Is there a way to get this to compile and run correctly without manually typing in a list of cpp files?
Edit: Since there seems to be some confusion, typically in VSCode you can compile and debug in one go by pushing F5. This is the part where I get the errors above. I am hoping someone can help me understand why that is failing and how to fix it so I can continue testing/debugging in VSCode.
Edit: I'm still voting that this question is unique since I was simply following the trace of the compile error in VS Code. I had actually found this article before, and it did not solve my problem. It is also extremely dense and as a beginner was difficult to understand how it might explain my problem. I will add a visual studio code tag to help people find this question. But every other reference to the vtable error I found was to do with the vtable itself and not following the troubleshooting path to a solution in VS Code.
#JaMiT shared a link that helped me get to the answer. As it turns out, the code is fine but the issue is with how VisualStudio Code is configured in the tasks.json file. If you want to compile multiple cpp files with g++ (assuming you used the Using GCC with MINGW getting started guide) you need to modify the "args". Here is my tasks.json that specifies which cpp files to compile:
tasks.json
{
"tasks": [
{
"type": "cppbuild",
"label": "C/C++: g++.exe build active file",
"command": "C:\\msys64\\mingw64\\bin\\g++.exe",
"args": [
"-g",
"test.cpp",
"qdata.cpp",
"-o",
"${fileDirname}\\${fileBasenameNoExtension}.exe"
],
"options": {
"cwd": "${fileDirname}"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "Task generated by Debugger."
}
],
"version": "2.0.0"
}
If you simply want to compile all cpp files in the active directory, then you can modify it as follows:
{
"tasks": [
{
"type": "cppbuild",
"label": "C/C++: g++.exe build active file",
"command": "C:\\msys64\\mingw64\\bin\\g++.exe",
"args": [
"-g",
"*.cpp",
"-o",
"${fileDirname}\\${fileBasenameNoExtension}.exe"
],
"options": {
"cwd": "${fileDirname}"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "Task generated by Debugger."
}
],
"version": "2.0.0"
}
Notice that "${file}" has been removed from the "args" in the default tasks.json file.

How to install a C++ library (OpenAL)

I'm having difficulties installing the OpenAL audio library in C++. I guess the headers are in the correct folder and the problem is in the lib files, because VS Code doesn't show any error when I include the library (#include <AL/al.h> and #include <AL/alc.h>), but when I try to compile my code from the terminal I get the following error:
C:\Users\ALEXAN~1\AppData\Local\Temp\ccNZ4t3C.o:Proves.cpp:(.text+0x1c): undefined reference to `__imp_alcOpenDevice'
C:\Users\ALEXAN~1\AppData\Local\Temp\ccNZ4t3C.o:Proves.cpp:(.text+0x30): undefined reference to `__imp_alcCloseDevice'
collect2.exe: error: ld returned 1 exit status
Where and how I've installed the library:
I've downloaded and unziped the Windows binaries. Then I've run the cpp -v command to find the C++ include directories, which showed 3 directories. I moved the AL folder (that contains the headers) to the C:\ProgramData\chocolatey\lib\mingw\tools\install\mingw64\x86_64-w64-mingw32\include directory and the Win32 lib files (called libOpenAL32.dll.a, OpenAL32.def and OpenAL32.lib) to the C:\ProgramData\chocolatey\lib\mingw\tools\install\mingw64\x86_64-w64-mingw32\lib directory. I'm not sure if I have to put there the 3 of them or just the one called libOpenAL32.dll.a.
I know there are similar posts on stackoverflow and I've read some of them, but it's the first time I install a library in C++ and it's difficult for me to understand them. If someone could provide a clear explanation on how to complete the installation I'd be very grateful.
I've found a solution to my problem thanks to #drescherjm and to this answer by #MemzIcon. I've modified a bit the code in the answer so I can use it for any file in the project by changing ${workspaceFolder} by ${fileDirname}.
Moreover, I've created a C:\Libraries directory with two folders: Include, where I'll store my external headers, and Libs, where I'll store my external library files. This way, the libraries I install are not mixed with the other C++ libraries.
This was the first tasks.json I created:
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"tasks": [
{
"label": "Compiler",
"type": "shell",
"command": "g++",
"args": [
"-c",
"${file}",
"-o",
"${fileDirname}\\${fileBasenameNoExtension}.o",
"-IC:\\Libraries\\Include"
]
},
{
"label": "Linker",
"type": "shell",
"command": "g++",
"args": [
"${fileDirname}\\${fileBasenameNoExtension}.o",
"-o",
"${fileDirname}\\${fileBasenameNoExtension}.exe",
"-LC:\\Libraries\\Libs",
"-llibOpenAL32"
]
},
{
"label": "Build OpenAL",
"dependsOn": [
"Compiler",
"Linker"
],
"group": {
"kind": "build",
"isDefault": true
}
}
]
}
However, it didn't work properly, because it executed the second command called "Linker" when the <file>.o hadn't been created yet. In consequence, when I pressed the Ctrl+Shift+B shortcut, the "Linker" command throwed an error because it didn't find the object file. To solve this, I created just ONE command instead of 3. Inside of this command I used the ; symbol to separate the Compiler and the Linker parts. Finally, this is the aspect of my tasks.json:
{
"version": "2.0.0",
"tasks": [
{
"label": "Build OpenAL",
"type": "shell",
"command": "g++",
"args": [
"-c",
"${file}",
"-o",
"${fileDirname}\\${fileBasenameNoExtension}.o",
"-IC:\\Libraries\\Include",
";",
"g++",
"${fileDirname}\\${fileBasenameNoExtension}.o",
"-o",
"${fileDirname}\\${fileBasenameNoExtension}.exe",
"-LC:\\Libraries\\Libs",
"-llibOpenAL32",
],
"group": {
"kind": "build",
"isDefault": true
}
}
]
}

How to stop Task running terminal to exit automatically after the program ends in visual studio code?

I am a new c++ learner and using visual studio code as my IDE. As an example, a cpp file has the hello world program written in it. In VSC, I compile the cpp file with a task named Build( >Tasks: Run Build task), and run it using the task named Run( >Tasks: Run task). (tasks.json is given below)
But when I Run, a new terminal named "Task - Run" starts, shows the output and immediately exits.(Its very hard to see what was the output.)
Is there any command that I can put into the tasks.json file so that the program doesn't disappear after it ends? Or is there any other workaround?
//my tasks.json file
{
"version": "2.0.0",
"tasks": [
{
"taskName": "Build",
"type": "shell",
"command": "g++",
"args": [
"main.cpp"
],
"group": {
"kind": "build",
"isDefault": true
},
"problemMatcher": []
},
{
"taskName": "Run",
"type": "shell",
"command": "./a.out",
"problemMatcher": []
}
]
}
More of a hack than a fix but I have the same issue and I noticed that changing the version from "2.0.0" to "0.1.0" would keep my output window open with all the errors present.
Though I'm quite sure that this is not a long term solution.