能有人解释一下为什么我在代码中包含头文件时,它在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;
}
I get this error:
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"
能够正常工作,我会选择使用它。 - user4581301undefined reference to
MyMethod(int)'` - user6600549