这次的问题起源于我用容器array时需要包含头文件。
而刚刚好用到的容器array是C++11中新加入std库的,但是我的MinGw版本太低,出现了找不到库的情况。遂将自己的MinGw升级到了最新版本,此时问题出现,不知怎么的运行后报错了
报错如下:
PS D:\Work> cd "d:\Work" ; if ($?) { gcc ok.cpp -o ok.exe -std=c++11 -Wall -lm } ; if ($?) { ./ok.exe }
C:\Users\Dylan\AppData\Local\Temp\ccJuQfKx.o:ok.cpp:(.text+0x21): undefined reference to `std::ostream::operator<
C:\Users\Dylan\AppData\Local\Temp\ccJuQfKx.o:ok.cpp:(.text+0x40): undefined reference to `std::ios_base::Init::~Init()'
C:\Users\Dylan\AppData\Local\Temp\ccJuQfKx.o:ok.cpp:(.text+0x70): undefined reference to `std::ios_base::Init::Init()'
C:\Users\Dylan\AppData\Local\Temp\ccJuQfKx.o:ok.cpp:(.rdata$.refptr._ZSt4cout[.refptr._ZSt4cout]+0x0): undefined reference to `std::cout'
collect2.exe: error: ld returned 1 exit status
显然是编译器没找到对应的库
花费大量的时间找了很多办法,包括但不限于大幅修改了tasks.json,launch.json,c_cpp_properties.json,setting.json等文件,无果。
偶然了解到C++用g++进行编译会更好,用gcc的话gcc会无法链接到库,所以出现了上方的报错,但是上面几个文件的参数全都调成了g++的路径,确认无误了还是跑不动。
于是尝试调试:
#include
#include
#include
using namespace std;
int main()
{
arrayarr;
arr[1]=2;
cout<
return 0;
}
结果如下:
PS D:\Work> & 'c:\Users\Dylan\.vscode\extensions\ms-vscode.cpptools-1.1.1\debugAdapters\bin\WindowsDebugLauncher.exe' '--stdin=Microsoft-MIEngine-In-kkr4f2fp.y4m' '--stdout=Microsoft-MIEngine-Out-oqst5aks.rig' '--stderr=Microsoft-MIEngine-Error-wamcvkqu.ev0' '--pid=Microsoft-MIEngine-Pid-sbfykvax.bpj' '--dbgExe=D:\MinGw\bin\gdb.exe' '--interpreter=mi'```
2
神奇的是居然跑动了,那么就是说gdb是被正确地调用了的
又徒劳了半天之后
终于发现报错中的细节
PS D:\Work> cd "d:\Work" ; if ($?) { gcc ok.cpp -o ok.exe -std=c++11 -Wall -lm } ; if ($?) { ./ok.exe }
//上方的"gcc"有大问题
跑的仍然是gcc,一番思索之后,我想既然是用code runner插件跑的,那么就尝试下在code runner的配置文件中寻找答案。抱着最后一丝希望,我找到配置文件
打开首选项的设置
一点开就发现了抓眼的英文字母
"cpp"一栏中的"gcc"
将其改为g++后再跑
感天动地的2出现了这次因为方向走错了,导致浪费了大量时间...
总之还是要细心,要在问题的基础上寻找问题的根源,方向走错了会白费大量时间,还会打击信心,但不论如何,我又可以继续开心地码起来了...