0

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.

  • 3
    Neither of your tasks.json changes are correct. Never pass header files to the compiler, and the correct option for multiple cpp files is [here](https://code.visualstudio.com/docs/cpp/config-clang-mac#_modifying-tasksjson) – john Nov 19 '22 at 22:28
  • @john I just noticed that the post ignores the "\*" character for some odd reason. Is "${workspaceFolder}/\*.cpp" the correct format, because that's what I have in my original task.json – Epsilon-Indii Nov 19 '22 at 22:34
  • Please do not describe your files in free prose, copy and paste them instead as code fragments. – n. m. could be an AI Nov 19 '22 at 22:36
  • OK, I thought that might be the case, but adding hpp files as well is definitely wrong. Header files get included because of `#include`. You don't need to tell the compiler about them (and it's wrong to do so). – john Nov 19 '22 at 22:37
  • @n.m. I'm sorry, I don't really understand. Did I incorrectly format my post? I thought that I did paste my files as code fragments. – Epsilon-Indii Nov 19 '22 at 22:42
  • 1
    "tasks.json file has (description)" bad, "this is the contents of my tasks.json (paste)" good. – n. m. could be an AI Nov 19 '22 at 23:00
  • Your `tasks.json` should be modified to build all cpp files in the folder not just the 2 you mention. I have answered a similar but a little more complicated question where the user had multiple folders with multiple source files: [https://stackoverflow.com/questions/70856563/vscode-g-it-isnt-finding-cpp-definition-files/70856902#70856902](https://stackoverflow.com/questions/70856563/vscode-g-it-isnt-finding-cpp-definition-files/70856902#70856902) – drescherjm Nov 20 '22 at 00:04
  • "the debugger properly builds the file, no errors there, but when running the code regularly, I still get the linker error" Perhaps you also have a code runner or similar extension? These are only designed to take one file. If you have any of those, just uninstall, you don't need them. – n. m. could be an AI Nov 20 '22 at 06:44

0 Answers0