如何在vs代码中将多个cpp文件链接在一起进行编译?

时间:2019-06-29 18:47:14

标签: c++ visual-studio-code

我想使用.h和.cpp文件,但我不知道我的错误是什么

这是我的树

header
   alphabet.h
source
  alphabet.cpp
  main.cpp

我正在使用Visual Studio代码

这是我的git:https://github.com/hubmanS/c-Demo

alphabet.h

int add(int , int ); // function prototype for add.h -- don't forget the semicolon!

alphabet.cpp

#include "../header/alphabet.h"

int add(int x, int y)
{
    return x + y;
}

主要

#include <iostream>
#include "../header/alphabet.h" // Insert contents of add.h at this point.  Note use of double quotes here.

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

错误是:Executing task: g++ -g automata/source/main.cpp -o main.out && clear && ./main.out < /tmp/ccHdhAVd.o: In function main': /home/ubuntu/Escritorio/competitive/compile/automata/source/main.cpp:6: undefined reference to add(int, int)' collect2: error: ld returned 1 exit status The terminal process terminated with exit code: 1 Terminal will be reused by tasks, press any key to close it

这是用于运行和编译.cpp的task.json

{
    "version": "2.0.0",
    "tasks": [
        {
            "label": "debug",
            "type": "shell",
            "command": "",
            "args": ["g++","-g", "${relativeFile}", "-o","a.exe"]
        },
        {
            "label": "Compile and run",
            "type": "shell",
            "command": "",
            "args": [
                "g++","-g", "${relativeFile}", "-o","${fileBasenameNoExtension}.out", "&&", "clear" , "&&" , "./${fileBasenameNoExtension}.out"
            ],
            "group": {
                "kind": "build",
                "isDefault": true  
            },
            "problemMatcher": {
                "owner": "cpp",
                "fileLocation": ["relative", "${workspaceRoot}"],
                "pattern": {
                    "regexp": "^(.*):(\\d+):(\\d+):\\s+(warning|error):\\s+(.*)$",
                    "file": 1,
                    "line": 2,
                    "column": 3,
                    "severity": 4,
                    "message": 5
                }
            }
        },

    ]
}

0 个答案:

没有答案