c++linkervscode-tasksrun-configuration

Link multiple files in Visual Studio Code (Ubuntu)


I don't understand where I am wrong. If I run my code through the run button I get this output:

cd "/home/luca/develop/Cplusplus_course/Section13/" && g++ challenge.cpp -o challenge && "/home/luca/develop/Cplusplus_course/Section13/"challenge
/usr/bin/ld: /tmp/ccoxepCS.o: in function `main':
challenge.cpp:(.text+0x27): undefined reference to `Movies::Movies()'
/usr/bin/ld: challenge.cpp:(.text+0x33): undefined reference to `Movies::display() const'
/usr/bin/ld: challenge.cpp:(.text+0x9e): undefined reference to `Movies::add_movie(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, int)'
/usr/bin/ld: challenge.cpp:(.text+0x13f): undefined reference to `Movies::add_movie(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, int)'
/usr/bin/ld: challenge.cpp:(.text+0x1e0): undefined reference to `Movies::add_movie(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, int)'
/usr/bin/ld: challenge.cpp:(.text+0x222): undefined reference to `Movies::display() const'
/usr/bin/ld: challenge.cpp:(.text+0x28d): undefined reference to `Movies::add_movie(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, int)'
/usr/bin/ld: challenge.cpp:(.text+0x32e): undefined reference to `Movies::add_movie(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, int)'
/usr/bin/ld: challenge.cpp:(.text+0x370): undefined reference to `Movies::display() const'
/usr/bin/ld: challenge.cpp:(.text+0x3ac): undefined reference to `Movies::increment_watched(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >)'
/usr/bin/ld: challenge.cpp:(.text+0x403): undefined reference to `Movies::increment_watched(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >)'
/usr/bin/ld: challenge.cpp:(.text+0x45a): undefined reference to `Movies::increment_watched(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >)'
/usr/bin/ld: challenge.cpp:(.text+0x481): undefined reference to `Movies::display() const'
/usr/bin/ld: challenge.cpp:(.text+0x4bd): undefined reference to `Movies::increment_watched(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >)'
/usr/bin/ld: challenge.cpp:(.text+0x4e9): undefined reference to `Movies::~Movies()'
/usr/bin/ld: challenge.cpp:(.text+0x771): undefined reference to `Movies::~Movies()'
collect2: error: ld returned 1 exit status

If I run my code with this command I get a clean run: g++ Movie.cpp Movies.cpp challenge.cpp -o challenge

Here is my ./vscode/tasks.json

{
    // See https://go.microsoft.com/fwlink/?LinkId=733558
    // for the documentation about the tasks.json format
    "version": "2.0.0",
    "tasks": [
        {
            "type": "cppbuild",
            "label": "C/C++: g++ build active file",
            "command": "/usr/bin/g++",
            "args": [
                "-fdiagnostics-color=always",
                "-g",
                "-Wall",
                "${workspaceFolder}/*.cpp",
                "-o",
                "${fileDirname}/${fileBasenameNoExtension}",  
            ],
            "options": {
                "cwd": "${fileDirname}"
            },
            "problemMatcher": [
                "$gcc"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            },
            "detail": "compiler: /usr/bin/g++"
        }
    ]
}

Solution

  • I found that there is a .json for the execution of the CodeRunner as well, modifying it in this way it works

    code-runner.executorMap": {
    
        "cpp": "cd $dir && g++ *.cpp -o $fileNameWithoutExt && $dir$fileNameWithoutExt",
    

    It is answered in the link here: Code-runner configuration for running multiple cpp classes in vscode