I'm fairly new to C++ and especially to VS Code, so before I tackle any larger projects, I first wanted to test run a simple hello world program that involves a "person" class that will set and get names inside main (HelloWorld.cpp).
I'm using the "C/C++: clang build active file" as instructed by VS Code documentation, however when starting the program, the terminal outputs this inside debug:
Starting build...
/usr/bin/clang++ -std=c++17 -stdlib=libc++ -fcolor-diagnostics -fansi-escape-codes -g /Users/familyuser/Desktop/Git_Projects/CPP_Test/HelloWorld.cpp /Users/familyuser/Desktop/Git_Projects/CPP_Test/*.cpp /Users/familyuser/Desktop/Git_Projects/CPP_Test/*.hpp -o /Users/familyuser/Desktop/Git_Projects/CPP_Test/HelloWorld
clang: error: cannot specify -o when generating multiple output files
Build finished with error(s).
* The terminal process failed to launch (exit code: -1).
* Terminal will be reused by tasks, press any key to close it.
and this when attempting to run the program:
Undefined symbols for architecture x86_64:
"person::GetName()", referenced from:
_main in HelloWorld-ef1483.o
"person::SetName(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >)", referenced from:
_main in HelloWorld-ef1483.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
HelloWorld.cpp:
#include <iostream>
#include <string>
#include "Person.hpp"
int main() {
person personObj;
std::string userInput;
std::cout << "input person's name:" << std::endl;
std::cin >> userInput;
personObj.SetName(userInput);
std::cout << "Person's name is: " << personObj.GetName() << "!" << std::endl;
return 0;
}
Person.cpp:
#include <string>
#include "Person.hpp"
void person::SetName(std::string userName) {
this->name = userName;
}
std::string person::GetName() {
return this->name;
}
Person.hpp:
#ifndef PERSON_H_
#define PERSON_H_
#include <string>
class person {
public:
void SetName(std::string userName);
std::string GetName();
private:
std::string name;
};
#endif
If I'm missing info, or if I could be redirected to similar posts, it would be appreciated.
I've looked at similar posts addressing this error and I've made sure to consider a few things first:
- All files are located inside the same workspace folder
- Methods inside Person.cpp are properly declared & defined
- tasks.json file has "${workspaceFolder}/.hpp" & "${workspaceFolder}/.cpp" passed inside "args:"
(I thought that this change alone would be enough to fix the problem, as from my understanding, that command will look inside the entire workspace folder and link any .cpp and .hpp files)
Edit: So I've updated the tasks.json file as shown:
{
"tasks": [
{
"type": "cppbuild",
"label": "C/C++: clang++ build active file",
"command": "/usr/bin/clang++",
"args": [
"-std=c++17",
"-stdlib=libc++",
"-fcolor-diagnostics",
"-fansi-escape-codes",
"-g",
"${workspaceFolder}/*.cpp",
"-o",
"${fileDirname}/${fileBasenameNoExtension}"
],
"options": {
"cwd": "${fileDirname}"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "Task generated by Debugger."
}
],
"version": "2.0.0"
}
and fortunately, the debugger properly builds the file, no errors there, but when running the code regularly, I still get the linker error as mentioned previously.