Home > other >  Vscode g it isn't finding .cpp definition files
Vscode g it isn't finding .cpp definition files

Time:01-26

I'm trying to compile a c example with multiple .cpp and .hpp files, but g doesn't find any member function definition.


main.cpp:

#include <iostream>
#include "Person.hpp"

int main()
{
    std::cout << "HELL!\n";
    
    Person a{"Jiraya"};
    std::cout << a.getName() << "\n";
    a.setName("Niko");
    a.do_smt();
}

Person.hpp:

#pragma once

#include <string>

using std::string;

class Person
{
private:
    string name;

public:
    Person();
    Person(const string &n);
    void do_smt();
    string getName(){return name;}
    void setName(const string& n);

Person.cpp:

    #pragma once
    #include <iostream>
    #include "Person.hpp"
    
    Person::Person(const string &n) : name{n}
    {
    }
    
    void Person::setName(const string &n)
    {
        name = n;
    }
    
    void Person::do_smt()
    {
        std::cout << "???";
    }

Tasks.json:

{
    "tasks": [
        {
            "type": "cppbuild",
            "label": "C/C  : g   build active file",
            "command": "/usr/bin/g  ",
            "args": [
                "-fdiagnostics-color=always",
                "-g",
                "${file}",
                "-o",
                "${fileDirname}/${fileBasenameNoExtension}",
                "-iquote${workspaceFolder}/headers"
            ],
            "options": {
                "cwd": "${fileDirname}"
            },
            "problemMatcher": [
                "$gcc"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            },
            "detail": "Task generated by Debugger."
        }
    ],
    "version": "2.0.0"
}

Terminal output:

> Executing task: C/C  : g   build active file <

Starting build...
/usr/bin/g   -fdiagnostics-color=always -g /home/raijin/Documents/Code/C  /sandbox/main.cpp -o /home/raijin/Documents/Code/C  /sandbox/main -iquote/home/raijin/Documents/Code/C  /sandbox/headers
/usr/bin/ld: /tmp/ccN0pJKE.o: in function `main':
/home/raijin/Documents/Code/C  /sandbox/main.cpp:9: undefined reference to `Person::Person(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)'
/usr/bin/ld: /home/raijin/Documents/Code/C  /sandbox/main.cpp:11: undefined reference to `Person::setName(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)'
/usr/bin/ld: /home/raijin/Documents/Code/C  /sandbox/main.cpp:12: undefined reference to `Person::do_smt()'
collect2: error: ld returned 1 exit status

Build finished with error(s).

Terminal will be reused by tasks, press any key to close it.

Here is the project folder structure:

folder structure

I did add "-iquote${workspaceFolder}/headers" arg in tasks.json to locate .hpp in a subdirectory. It doesn't seem to work with .cpp files. What do I do?(Even if I move Person.cpp to ${workspaceFolder} results the same terminal output)

CodePudding user response:

In your tasks.json you are using the default ${file} which means compile only the active file and not all source files in your folder structure.

The VSCode documentation explains how to fix this for the case of all source files in the same folder here: https://code.visualstudio.com/docs/cpp/config-linux#_modifying-tasksjson

The fix is to replace ${file} with "${workspaceFolder}/*.cpp"

In your case you have more than 1 folder containing source files. You can apply a similar fix to the second folder by adding: "${workspaceFolder}/classes/*.cpp"

so the whole tasks.json would be:

{
    "tasks": [
        {
            "type": "cppbuild",
            "label": "C/C  : g   build active file",
            "command": "/usr/bin/g  ",
            "args": [
                "-fdiagnostics-color=always",
                "-g",
                "${workspaceFolder}/*.cpp",
                "${workspaceFolder}/classes/*.cpp",
                "-o",
                "${fileDirname}/${fileBasenameNoExtension}",
                "-iquote${workspaceFolder}/headers"
            ],
            "options": {
                "cwd": "${fileDirname}"
            },
            "problemMatcher": [
                "$gcc"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            },
            "detail": "Task generated by Debugger."
        }
    ],
    "version": "2.0.0"
}
  •  Tags:  
  • Related