【问题标题】:How to include custom header files in VS Code [duplicate]如何在 VS Code 中包含自定义头文件 [重复]
【发布时间】:2018-07-27 18:27:33
【问题描述】:

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

如果我运行这段代码:

#include <iostream>

int main () {

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

VS Code 工作正常,但如果我运行此代码:

#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
}

【问题讨论】:

  • 如果 #include "lib/Methods.h" 有效,我就这么做。
  • 我建议将 VSCode 生成并运行的命令行添加到问题中。
  • @user4581301 是的,现在 intelisense 可以工作了,但是如果我运行 g++ main.cpp,我又会遇到同样的错误
  • undefined reference to MyMethod(int)'`
  • 如果您在 Windows 上并且正在编写 C++,为什么不直接使用实际的 VS,那里的 IntelliSense 比 Code 中当前提供的要好得多。

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


【解决方案1】:

最后发现是链接器错误

我只是将我的项目文件结构展平为与 main.cpp 位于同一根目录

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-10-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-03-04
    • 2020-06-16
    • 2014-01-28
    相关资源
    最近更新 更多