-2

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++"
        }
    ]
}
Evg
  • 25,259
  • 5
  • 41
  • 83
Luca90
  • 63
  • 1
  • 2
  • 12
  • What run button? The one that launches the debugger? Or one installed via an extension? In any case, my recommendation is always to move to a build system instead of trying to get tasks.json to do multi-file compilations for you. Your error is a linker error, which could indicate a very brittle design if your code is compiled differently. You also can't be sure what your final executable will be called. Which `{fileBasenameNoExtension}` will get used? – sweenish Mar 28 '23 at 17:13
  • I can also clearly see that `g++ challenge.cpp -o challenge` is only attempting to compile one file. – sweenish Mar 28 '23 at 17:14
  • This question is asked almost daily, so there has got to be a duplicate somewhere. – user4581301 Mar 28 '23 at 17:17
  • Duplicate found by entering the question title into Google. Didn't try with Bing, but who uses Bing anyway? – user4581301 Mar 28 '23 at 17:19
  • Just tried Bing. Still found on the first page, after the official Microsoft documentation tell you how to do it. – user4581301 Mar 28 '23 at 17:22
  • @user4581301 of course I checked the other similar questions but none of them solved my problem, thanks for the clever answer anyway – Luca90 Mar 29 '23 at 17:27
  • Going back to the proposed duplicate, , note the extra `*` in `"${fileDirname}\\**.cpp",`. Do the same to your `"${workspaceFolder}/*.cpp",` and see what happens. – user4581301 Mar 29 '23 at 17:31
  • @sweenish yes exactly I don't know why is attempting to compile only my challenge.cpp file even though in my tasks.json I wrote to compile all the .cpp files. I think is the extension of my run button (Code runner) but also if I run with CTRL+F5 I get an error: Could not find the task 'C/C++: gcc build active file' – Luca90 Mar 29 '23 at 17:32
  • @user4581301 the double * does not work for me – Luca90 Mar 29 '23 at 17:39
  • Bummer. Reopenning. – user4581301 Mar 29 '23 at 17:47
  • Whatever you use to run the code probably ignores `tasks.json`. Try ⇧⌘B. – HolyBlackCat Mar 29 '23 at 18:16
  • @HolyBlackCat with shift+ctrl+B the build is succesfully. * Executing task: C/C++: g++ build active file Starting build... /usr/bin/g++ -fdiagnostics-color=always -std=c++17 -g -Wall /home/luca/develop/Cplusplus_course/Section15/*.cpp -o /home/luca/develop/Cplusplus_course/Section15/challenge Build finished successfully. * Terminal will be reused by tasks, press any key to close it. So is it my extension Coderunner that is not working with tasks.json? – Luca90 Apr 02 '23 at 17:01
  • @Luca90 Yes, it takes its command from elsewhere. Generally I believe CodeRunner it's unsuitable for running multifile programs (I could be wrong). – HolyBlackCat Apr 02 '23 at 18:32
  • @HolyBlackCat no I found that it can be done with code runner as well. Thanks anyway – Luca90 Apr 08 '23 at 13:14

1 Answers1

0

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

Luca90
  • 63
  • 1
  • 2
  • 12