0

I'm pretty new to C++, and here's my problem. I downloaded the g++ compiler on windows 10, and followed the vs code microsoft tutorial on how to set it up with vs code. In my main.cpp file, I include a #include "Line.h", which is a header file for a class. I also have a Line.cpp with constructor definition. However when building the main.cpp file using the built in vs code run command, I get an error: undefined reference to Line::Line(bla, bla, bla)... I did some research and found out that I have to explicitely tell the compiler to build the Line.cpp file, using the command: g++ main.cpp Line.cpp -o main. This seems like a really terrible solution, given that I might create many classes. Does that mean that I have to write a really long command, including each file that has to be compiled? I'm sure I'm missing something here... Is there a solution where I can just run "g++ main.cpp -o main" and it will link all the necessary files automatically?

#include <iostream>
#include <string>
#include <list>
#include <vector>
#include <sstream>
#include "Line.h"

using namespace std;

int main(int argc, char const *argv[])
{
    string ip;
    string port;

    string s=argv[1];
    if (s=="IP")
    {
        string tmp=argv[2];
        string::size_type p;
        if ((p=tmp.find(":"))!=string::npos)
        {
            ip=tmp.substr(0,p);
            tmp=tmp.substr(p);
            tmp.erase(tmp.begin());
            if (tmp.size()) port=tmp;
        }
        else
            ip=tmp;

        std::stringstream test("Game Time,0,150");
        std::string segment;
        std::vector<std::string> seglist;
        list<Line> lines;

        while(std::getline(test, segment, ','))
        {
            seglist.push_back(segment);
        }

        Line line(seglist[0], seglist[1], seglist[2]);
        lines.push_back(line);

    }
    /* code */
    return 0;
}

user14850280
  • 389
  • 3
  • 16
  • It is nicely documented here: https://code.visualstudio.com/docs/languages/cpp – schorsch312 Jul 09 '21 at 09:44
  • I thinks the write one is [this](https://code.visualstudio.com/docs/cpp/config-linux). Check the part of include. – Louis Go Jul 09 '21 at 09:48
  • The include path of `Line.h` is not known to the compiler. Setup a cmake project. There you need to define all include paths. – schorsch312 Jul 09 '21 at 09:48
  • Did you need all this code to provide an example? It'd be better to provide a [minimal example](https://stackoverflow.com/help/minimal-reproducible-example) of your problem. – Elliott Jul 09 '21 at 09:54
  • Does this answer your question? [How i have to configure VS Code to don't get a "undefined reference" error message?](https://stackoverflow.com/questions/52666791/how-i-have-to-configure-vs-code-to-dont-get-a-undefined-reference-error-messa) – Zoe Jul 09 '21 at 09:58

2 Answers2

1

I figured out a solution. I editted my tasks.json file with the following code:

{
    "version": "2.0.0",
    "tasks": [
      {
        "type": "shell",
        "label": "C/C++: g++.exe build active file",
        "command": "C:\\Program Files\\mingw-w64\\x86_64-8.1.0-posix-seh-rt_v6-rev0\\mingw64\\bin\\g++.exe",
        "args": ["-g", "${fileDirname}\\**.cpp", "-o", "${fileDirname}\\${fileBasenameNoExtension}.exe"],
        "options": {
          "cwd": "${workspaceFolder}"
        },
        "problemMatcher": ["$gcc"],
        "group": {
          "kind": "build",
          "isDefault": true
        }
      }
    ]
  }
user14850280
  • 389
  • 3
  • 16
0

You can use tools like CMake, QMake etc to build multiple translation units as part of a single project. CMake's syntax is not so user friendly in my opinion. QMake is much more simple. However CMake is used more often and it's more portable. Depends on your choice.
For QMake you would need to make a .pro file in which you list all your source & header files and just use 'make' to build your project. .pro (qmake) file looks something like this.

CONFIG += c++17
SOURCES += a.cpp b.cpp c.cpp
HEADERS += a.hpp b.hpp c.hpp
TARGET = executableName

CMake documentation
QMake documentation

Staz
  • 348
  • 1
  • 2
  • 6