-2

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.

14
  • "Is there a way to get this to compile and run correctly without manually typing in a list of cpp files?" It sounds like you're asking if you can compile a C++ program without compiling all of the C++ program. No, you cannot. "qdata.cpp" is not optional. Commented Sep 20, 2021 at 3:30
  • In VSCode you can simply push F5 to compile and run the debugger. When I do that, I get the errors I mentioned. I am asking how to get it to compile and debug with F5. Commented Sep 20, 2021 at 4:11
  • Does this answer your question? What is an undefined reference/unresolved external symbol error and how do I fix it? Commented Sep 20, 2021 at 4:40
  • @equiv " I am asking how to get it to compile and debug with F5." -- sorry, no. Your current question is 95% focused on how to resolve an undefined reference. To ask about how to do something in VS Code, you should re-write your question so that it focuses on how to build c++ programs with multiple .ccp source files in VS Code. You can skip the error message about the undefined reference and vtable since you apparently already know the solution to that. Commented Sep 20, 2021 at 4:44
  • So again, beginner here. Patience plz. Maybe I don’t know what I’m asking? I don’t think I’m asking how to do something in VSCode, I’m explaining that there is a difference between what is happening when I push F5 and call g++ to compile(errors), and when I explicitly tell it to use both cpp files. I thought that information might be helpful to an experienced individual in helping me understand how to resolve my problem. Getting it working with F5 is my end goal. I assume that it is a coding error preventing it from linking. Is it not? I’ll look at your link. Commented Sep 20, 2021 at 6:19

1 Answer 1

0

@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.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.