如何在VS Code中包含自定义头文件

时间:2018-07-27 18:27:33

标签: c++ visual-studio-code g++

有人可以向我解释为什么当我在代码中包含头文件时,为什么它不在 VS Code 中运行? (操作系统: Windows 10

enter image description here

如果我运行以下代码:

#include <iostream>

int main () {

    std::cout << "Hello from C++ " << std::endl;
}

VS代码工作正常,但是如果我运行以下代码:

#include <iostream>
#include "./lib/Methods.h"

int main () {

    int a = MyMethod(5);
    std::cout << "Hello from C++ " << std::endl;
    std::cout << "a = " << a << std::endl;
}

我收到此错误:

PS C:\Users\giorg\Documents\Development\Tests\node-addons-test\src> g++ main.cpp
C:\Users\giorg\AppData\Local\Temp\ccIQQEE1.o:main.cpp:(.text+0x1e): undefined reference to `MyMethod(int)' collect2.exe: error: ld returned 1 exit status PS
C:\Users\giorg\Documents\Development\Tests\node-addons-test\src>

lib / Methods.cpp

int MyMethod(int x) {
    return x * 2;
}

lib / Methods.h

int MyMethod(int x);

c_cpp_properties.json

{
    "configurations": [
        {
            "name": "Win32",
            "includePath": [
                "${workspaceFolder}/**",
                "/src/lib"
            ],
            "defines": [
                "_DEBUG",
                "UNICODE",
                "_UNICODE"
            ],
            "windowsSdkVersion": "10.0.17134.0",
            "compilerPath": "C:/Program Files (x86)/Microsoft Visual Studio/2017/Community/VC/Tools/MSVC/14.14.26428/bin/Hostx64/x64/cl.exe",
            "cStandard": "c11",
            "cppStandard": "c++17",
            "intelliSenseMode": "msvc-x64"
        }
    ],
    "version": 4
}

1 个答案:

答案 0 :(得分:1)

最后,我发现是链接器错误

我只是将我的项目文件结构展平到与main.cpp相同的根目录

我只是运行此命令g++ *.cpp