0

I started coding C++ and wanted to try in VS Code. I know it isn't the best program for that, I use VS 2019 but I wanted to try Code. I came to conclusion that VS Code builds only on .cpp file but my programs has 2 .cpp and 1 .h files. I followed Microsoft's tutorial on their site and installed all necessary programs and tools.

This is mine task.json:

{
    "tasks": [
        {
            "type": "cppbuild",
            "label": "C/C++: g++.exe build active file",
            "command": "C:\\MinGW\\bin\\g++.exe",
            "args": [
                "-g",
                "${workspaceFolder}\\*.cpp",
                "-o",
                "${fileDirname}\\${fileBasenameNoExtension}.exe"
            ],
            "options": {
                "cwd": "${workspaceFolder}"
            },
            "problemMatcher": [
                "$gcc"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            },
            "detail": "Task generated by Debugger."
        }
    ],
    "version": "2.0.0"
}

This gives me an error undefined reference to `myFunction()'

I believe that task.json is the problem here but I don't understand why isn't it working?

Nikola
  • 3
  • 3
  • What is `myFunction()`? In which file(s) is it declared and defined? Have you linked all the files into the final executable? Compiling is not sufficient; you must also link. – underscore_d Apr 14 '21 at 13:20
  • @underscore_d ```myFunction()``` is declared in myClass.h and defined in second.cpp. They are all linked with ```#include```. After that I call ```myFunction()``` in main.cpp. – Nikola Apr 14 '21 at 13:23
  • `#include` just includes the textual content of the header in the including file. It does not link the object files together. Probably some more background reading on building is required. – underscore_d Apr 14 '21 at 13:28
  • 1
    the simplest answer is don't use vs code for building c++ directly, there's good cmake support via a plugin – Alan Birtles Apr 14 '21 at 13:28
  • Does this answer your question? [What is an undefined reference/unresolved external symbol error and how do I fix it?](https://stackoverflow.com/questions/12573816/what-is-an-undefined-reference-unresolved-external-symbol-error-and-how-do-i-fix) – underscore_d Apr 14 '21 at 13:29

0 Answers0