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

equiv
  • 5
  • 2
  • "*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. – Nicol Bolas Sep 20 '21 at 03: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. – equiv Sep 20 '21 at 04:11
  • Does this answer your question? [What is an undefined reference/unresolved external symbol error and how do I fix it?](https://stackoverflow.com/questions/12573816/what-is-an-undefined-reference-unresolved-external-symbol-error-and-how-do-i-fix) – JaMiT Sep 20 '21 at 04: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](https://stackoverflow.com/questions/65530397/) in VS Code. You can skip the error message about the undefined reference and vtable since you apparently already know the solution to that. – JaMiT Sep 20 '21 at 04: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. – equiv Sep 20 '21 at 06:19
  • @equiv Writing a good question does not depend on your programming experience. *"I don’t think I’m asking how to do something in VSCode, [...] Getting it working with F5 is my end goal."* -- Do you see the inherent contradiction here? You say your are not asking about how to do something in VS Code. Rather, you are asking how to do something with F5 (in VS Code). ??? – JaMiT Sep 20 '21 at 06:43
  • @equiv *"I assume that it is a coding error preventing it from linking."* -- it is reasonable to keep this open as a *possibility*, but *assuming* it is the case seems unwise, given that it did link when you ran `g++ -o binary test.cpp qdata.cpp`. – JaMiT Sep 20 '21 at 06:46
  • @JaMiT I see what's going on here. In other languages, when my code doesn't run it is a problem with the code, not the tool I am using. So I assumed the code was at fault. I am a C++ beginner, so I didn't know that running that g++ command meant that the code was ok and the environment needed to be reconfigured. Clearly you knew this, but you chose to pick at how I asked the question rather than explaining the code was fine and suggest I look at the environment. I specifically said I might not know what I'm asking for. I was following the errors. In any case, thanks for the links. – equiv Sep 20 '21 at 15:50
  • @equiv *"In other languages, when my code doesn't run it is a problem with the code, not the tool I am using.* -- this is news to me. Please enlighten me. In which languages does code running with one tool but not with another tool indicate that the problem is (most likely) with the code, not with the other tool? – JaMiT Sep 20 '21 at 21:58
  • @equiv *"[...] rather than explaining [...]"* -- you seem to be under the impression that my intent is to answer your question. That is incorrect. Your question is a duplicate of others already on the site. Stack Overflow's procedure in such a case is to close the question so that the answers are centralized in one location (easier to find), rather than spread across dozens of questions. So I voted to close as a duplicate and did not attempt to answer. I also posted comments to encourage you to improve your question. That - not answering - is how comments are intended to be used on this site. – JaMiT Sep 21 '21 at 10:53
  • @equiv Face it. You did something good by trying gcc in addition to VS Code. At some level, you knew your real question was about VS Code. Yet you disregarded that when writing your question. As a result, the question you wrote focused on the wrong issue, hence it is in the process of being closed as a duplicate of a question that did not help you. If you had focused on the difference between your tools, a more useful duplicate would have been submitted. – JaMiT Sep 21 '21 at 11:00
  • @JaMiT Just using one tool... VSCode using default settings from a getting started guide... – equiv Sep 22 '21 at 13:09
  • @equiv No, you are using two tools. The command line (a.k.a. the terminal) is much less flashy than VSCode, but both count as tools for accessing your compiler. It's useful to have a backup tool so you can do a quick check, like you did, when your main tool fails. Not everyone learns how to compile via the command line, which is unfortunate. You appear to be more accomplished than you think. – JaMiT Sep 23 '21 at 00:30
  • @JaMiT Ah, now that is a fundamental piece of knowledge that helps explain things. So the command line was demonstrating that the code was in fact functional as-is. I thought it was showing me that I had missed some sort of linking in the code, hence why I had to tell it explicitly what to include. I hope this question stays live. For anyone who thinks the way I do, they will actually find this answer. My other comments have disappeared; not sure what is up with that. So I think I am done here. Thanks. – equiv Sep 24 '21 at 15:32

1 Answers1

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.

equiv
  • 5
  • 2