Background:
C++运行环境:#VScode #MinGW-64 #Win10-64.
Error:
1: cannot open source file "string".
2: #include errors detected. Please update your includePath. IntelliSense features for this translation unit (C:\Code\Will\Cpp\selectionSort.cpp) will be provided by the Tag Parser.
3: identifier "string" is undefined". (even have #include <string> and using namespace std;)
4: string is not in std.
//其实这个Error很常见。目前理解是由于编译配置出现问题。
备注:对于Error2也可以在VSCode中File->Preferences->Settings在右侧的自定义配置中添加 "C_Cpp.intelliSenseEngine": "Tag Parser"。但不推荐这样做。
Solution steps:
- 在cmd中执行 gcc -v -E -x c++ -
- 复制输出结果中 #include <...> search starts here: 下的所有路径
- 在VSCode中 Ctrl+Shift+P, 输入Edit Configuration, 打开 c_cpp_properties.json,找到"name":"Win32"。
-
把第二步复制的路径添加到 c_cpp_properties.json 中的"name":"Win32"下"includePath"和的 "browse"的 "path"中,添加的路径要注有双引号。注意要保持原有顺序!路径中的 .. 不用修改。
例如:
"${workspaceRoot}",
"C:/Program Files (x86)/mingw-w64/i686-7.2.0-posix-dwarf-rt_v5-rev1/mingw32/bin/../lib/gcc/i686-w64-mingw32/7.2.0/include/c++",
"C:/Program Files (x86)/mingw-w64/i686-7.2.0-posix-dwarf-rt_v5-rev1/mingw32/bin/../lib/gcc/i686-w64-mingw32/7.2.0/include/c++/i686-w64-mingw32",
"C:/Program Files (x86)/mingw-w64/i686-7.2.0-posix-dwarf-rt_v5-rev1/mingw32/bin/../lib/gcc/i686-w64-mingw32/7.2.0/include/c++/backward",
"C:/Program Files (x86)/mingw-w64/i686-7.2.0-posix-dwarf-rt_v5-rev1/mingw32/bin/../lib/gcc/i686-w64-mingw32/7.2.0/include",
"C:/Program Files (x86)/mingw-w64/i686-7.2.0-posix-dwarf-rt_v5-rev1/mingw32/bin/../lib/gcc/i686-w64-mingw32/7.2.0/include-fixed",
"C:/Program Files (x86)/mingw-w64/i686-7.2.0-posix-dwarf-rt_v5-rev1/mingw32/bin/../lib/gcc/i686-w64-mingw32/7.2.0/../../../../i686-w64-mingw32/include""defines": [ "_DEBUG", "UNICODE", "_UNICODE", "__GNUC__=7", "__cdecl=__attribute__((__cdecl__))" ], "intelliSenseMode": "clang-x64",
以上内容也可延伸至如何用MinGW和VSCode正确配置一个C++开发环境,特别需要注意在VSCode中.vscode文件夹下的四个配置文件 c_cpp_properties.json, launch.json, settings.json, tasks.json 。
写这篇文章希望对使用C++/MinGW/VSCode初学者有所帮助,少走弯路。
相关知识还在摸索中,如有误导,欢迎各位大神留言更正,非常感谢!