在visualstudio代码程序中包含自定义的C++.h和.cpp文件



我遇到了一个问题,我有大量的自定义C/C++头文件(以及相应的.cpp文件(,我希望将它们包含在visual studio代码环境中的其他脚本中。我用的是windows 10。我以为我理解C/C++,但显然不是。无论如何,我的目标是能够运行类似以下代码的东西:

#include <iostream>
#include <JeffsLaboratory_Earth.h> // This causes the error, even though VS code opens the correct file if I right-click on it
int main() {
int x = 10;
int y = 20;
earth ear;
float f = ear.atm_alt_temp_dens;
std::cout << "Hello World" << std::endl;
}

其中";JeffsLaboratory_ Earth.h";指向我拥有的一个头文件,它与等价的"文件"在同一文件夹中;JeffsLaboratory_地球.cpp";文件此文件夹是本地文件夹。

然而,要想让它发挥作用,我遇到了很多困难。我还在下面包括JSON文件。正如你所看到的,我已经包含了这个库/头文件所在的路径,但我仍然得到";对地球的未定义引用::earth((";并且它终止。即使VS代码在我正确计时时会指向JeffsLaboratory_Tearth.h的正确位置。我知道这个代码和库是有效的,因为我在其他应用程序中也使用过它,所以这不是问题所在。任何帮助都将不胜感激!谢谢

c_cpp_properties.json

{
"configurations": [
{
"name": "Win32",
"includePath": [
"${workspaceFolder}/**",
"C:/JeffsLaboratory/commonsoftware/C++/PQRIntegratedSim_libraries"
],
"defines": [
"_DEBUG",
"UNICODE",
"_UNICODE"
],
"cStandard": "gnu11",
"cppStandard": "gnu++14",
"intelliSenseMode": "gcc-x64",
"browse": {
"path": [
"C:\MinGW\lib\gcc\mingw32\9.2.0\include\c++"
]
}
}
],
"version": 4

task.json

{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"tasks": [
{
"label": "echo",
"type": "shell",
"command": "g++",
"args": [
"-g", "main.cpp",
"-I", "C:\JeffsLaboratory\commonsoftware\C++\PQRIntegratedSim_libraries",
"${file}"
],
"group": {
"kind": "build",
"isDefault": true
}
}
]
}

launch.json

{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "(gdb) Launch",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceFolder}/a.exe",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"miDebuggerPath": "C:\MinGW\bin\gdb.exe",
"preLaunchTask": "echo",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
]
}
]
}

"对地球的未定义引用::earth((";

与完全不同

#include <JeffsLaboratory_Earth.h> // This causes the error, even though VS code opens the correct file if I right-click on it

第二个错误表示包含路径不完整。第一个指示链接错误。

在我看来,您并没有编译和链接JeffsLaboratory_Earth.cpp。您必须将其添加到生成配置中。

请参阅什么是未定义的引用/未解决的外部符号错误,以及如何修复它?

最新更新