Reputation: 419
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;
}
Upvotes: 0
Views: 82
Reputation: 419
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
}
}
]
}
Upvotes: 1
Reputation: 358
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
Upvotes: 0