0

I am using vscode on ubuntu linux. I am trying to compile/debug the following 3 files that should be linked but I think I am getting a linker error indicating that the function files are not defined:

Main.cpp

#include <iostream>

void Log(const char* message);

int Multiply(int a, int b);

int main()
{
    Log("Hello there! How old are you?");
    std::cout << Multiply(2,3) << std::endl;
    std::cin.get();
}

Log.cpp

#include <iostream>

void Log(const char* message)
{
    std::cout << message << std::endl;
}

Math.cpp

#include <iostream>

int Multiply(int a, int b)
{
    int result = a * b;
    return result;
}

Please note that I declare the functions in the last two files (Log.cpp and Math.cpp) in Main.cpp. I however get the following error when I run the the code (using g++ debugger):

launch: program '~./Dev/Src/Main' does not exist

with the option to either Cancel or Open launch.json

launch.json setup looks like this:

{
    // Use IntelliSense to learn about possible attributes.
    // Hover to view descriptions of existing attributes.
    // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "name": "g++ - Build and debug active file",
            "type": "cppdbg",
            "request": "launch",
            "program": "${fileDirname}/${fileBasenameNoExtension}",
            "args": [],
            "stopAtEntry": false,
            "cwd": "${fileDirname}",
            "environment": [],
            "externalConsole": false,
            "MIMode": "gdb",
            "setupCommands": [
                {
                    "description": "Enable pretty-printing for gdb",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                }
            ],
            "preLaunchTask": "C/C++: g++ build active file",
            "miDebuggerPath": "/usr/bin/gdb"
        }
    ]
}

tasks.json also looks like this:

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

The terminal gives the following error:

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

Starting build...
/usr/bin/g++ -fdiagnostics-color=always -g /Dev/src/Main.cpp -o /Dev/src/Main
/usr/bin/ld: 
/tmp/cchjsd06.o: in function `main':
/Dev/src/Main.cpp:24: undefined reference to `Log(char const*)'
/usr/bin/ld: /Dev/src/Main.cpp:25: undefined reference to `Multiply(int, int)'

collect2: error: ld returned 1 exit status

Build finished with error(s).

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

I cannot determine what exactly seems to be causing the linker error here. I am curious what I may be missing and would appreciate any insights.

463035818_is_not_an_ai
  • 109,796
  • 11
  • 89
  • 185
nigus21
  • 337
  • 2
  • 11
  • You need to build all three of your cpp files and link them together – Alan Birtles Nov 22 '21 at 13:41
  • @AlanBirtles I am a complete novice at this so please provide additional information on how I can do this. A little hand-holding here will help. – nigus21 Nov 22 '21 at 13:46
  • The default behavior of VSCode is to build only the active file into the executable. Your bug is this part of your `tasks.json`: `"${file}",` The documentation tells you how to modify that to support more than cpp file right here: [https://code.visualstudio.com/docs/cpp/config-linux#_modifying-tasksjson](https://code.visualstudio.com/docs/cpp/config-linux#_modifying-tasksjson) – drescherjm Nov 22 '21 at 13:54

0 Answers0