How can i link mutiple files correctly

81 Views Asked by At

im new to programing. i am having problems with linking all my files together. Does anyone know how to fix this error. Thank you very much! Here is the program that i compile:

add.h:

int add(int x, int y);

add.cpp:

#include "add.h"
int add(int x, int y)
{
    return x + y;
}

main.cpp:

#include "add.h"
#include <iostream>

int main()
{
    std::cout << "The sum of 3 and 4 is " << add(3, 4) << '\n';
    return 0;
} 

When i compile this program there is an error: undefined reference to add(int, int)'`. I tried to firgue out how to link these files by changing task.json:

    {
            "type": "cppbuild",
            "label": "C/C++: gcc.exe build active file",
            "command": "C:/msys64/ucrt64/bin/gcc.exe",
            "args": [
                "-fdiagnostics-color=always",
                "-g",
                ${file},
                "-o",
                "${fileDirname}\\${fileBasenameNoExtension}.exe",
                ""
            ],
            "options": {
                "cwd": "C:/msys64/ucrt64/bin"
            },
            "problemMatcher": [
                "$gcc"
            ],
            "group": "build",
            "detail": "compiler: C:/msys64/ucrt64/bin/gcc.exe"
        }
    ]
}

i changed "${file}" in args to "${workspaceFolder}/*.cpp" but there is an error like this:

cc1plus.exe: fatal error: C:\Users\ADMIN\Desktop\Hocbai/**.cpp: Invalid argument
compilation terminated.

Build finished with error(s).

 *  The terminal process failed to launch (exit code: -1). 
 *  Terminal will be reused by tasks, press any key to close it.

Does anyone know how to solve my problem? Thank you very much for reading!!!

I hope to see my program can link together

0

There are 0 best solutions below