Undefined reference to vtable when functions are defined in VS Code - c++

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.

Related

Problems getting OpenGL to work in VSCode on Mac

I am trying to complie a basic openGL program in VSCode on mac. I am using glad and GLFW, and I have the Glad files in the same folder as the test.cpp file I am trying to run. However, the include statement throws an error no matter how I type it. This is the program I am writing
#include <iostream>
#include "glad.h"
However, it throws the errors:
screenshot of errors
VSCode cannot find the header files the program needs to run, and the compiler throws errors as a result. I should add, that autocomplete for these libraries is working, however, the file cannot be found by the editor.
Here is the c_cpp_properties.json code
{
"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
}
Perhaps the problem could be realted to how I am trying to compile the file? Here is my tasks.json file
{
"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": "Task generated by Debugger."
}
],
"version": "2.0.0"
}
Any guidance on how to set this project up correctly, any advice would be greatly appreciated. For reference, here is the tutorial I am trying to follow: https://learnopengl.com/Getting-started/Hello-Window.
Thank you so much!
I'll start by pointing out a few things that aren't really important.
VSCode is not a compiler, nor an IDE. VScode is a text editor. As a text editor it may have some extensions that help you with developing (thus turning it into a make-shift IDE).
What is happening here is that the compiler (gcc) doesn't know where to look for include files. It is even hinting you that you need to update the includePath in a way that you point it to the correct folder with the header files. In the CLI this is done via the -I flag i.e. g++ main.cpp -Ipath/to/your/include_folder/.
If you look into the Getting Started section they even go through these steps and go as far as to suggest the usage of CMake (which is a build tool that helps you generate the makefile with which you build your binaries).
By looking at the tasks.json I can conclude that the extension which you are using in VSCode uses this file to pass down the correct arguments to g++. So the quickest solution for you right now is to specify another element in args that points to the header files i.e.:
"args": [
"-fdiagnostics-color=always",
"-g",
"${file}",
"-o",
"-Ipath/to/your/include_folder/with_the_headers", <----
"${fileDirname}/${fileBasenameNoExtension}",
],
Hope it helps!

launch: program "directory of the program " does not exit error

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.

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.

vector<string> msg {} not buildingfor C++ in VS Code on Mac

I'm trying to configure VS Code on a Mac (Intel) to develop with C++. I'm following the setup on the VS Code web site. Following all of the steps when I get to the Terminal - Run Build Task the build fails and indicates the it expected a ";" after 'msg'. I can run the same file in XCode with no issues, but VS Code fails. Here's the full code from the VS Code Setup site.
#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;
}
I, too, had this same problem/error message with the same source code using my Mac Studio with M1 based on Apple arm64 silicon.
First, the source code comes from a tutorial: Using Clang in Visual Studio Code.
When I run the code, I get this error for vector<string> msg:
expected ';' at end of declaration
range-based for loop is a C++11 extension [-Wc++11-extensions]
Second, as #Harry mentioned in a comment for this question:
initializing std::vector with initializer list is supported from c++11 on-wards.
Solution: add a compiler version to tasks.json that supports vector<string> msg. For me, I used -std=c++17 to the "args" list to solve the problem. As an FYI, I verified that -std=c++11 also solves the problem. Even -std=c++2b for the 2023 standard works with the Xcode version 14.2 command line extensions.
So, the default tasks.json file created for me in the tutorial from code.visualstudio.com was missing any reference to a compiler version directive.
Here is my updated tasks.json file with the one new element added to the args list:
{
"tasks": [
{
"type": "cppbuild",
"label": "C/C++: clang++ build active file",
"command": "/usr/bin/clang++",
"args": [
"-fcolor-diagnostics",
"-fansi-escape-codes",
"-std=c++17",
"-g",
"${file}",
"-o",
"${fileDirname}/${fileBasenameNoExtension}"
],
"options": {
"cwd": "${fileDirname}"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "Task generated by Debugger."
}
],
"version": "2.0.0"
}

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.