#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main() {
cout << "Hello";
}
当我尝试编译这个程序时,它仍然编译上一个程序:
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main() {
int a, b;
cin >> a >> b;
cout << a + b << endl;
return 0;
}
我在命令提示符下执行gcc code.cpp(文件名)和代码,显示如下:
c:/mingw/bin/../lib/gcc/mingw32/9.2.0/../../../../mingw32/bin/ld.exe:
C:\Users\User\AppData\Local\Temp\cctefhtT.o:code.cpp:(.text+0x19): undefined reference to
`std::cout'
c:/mingw/bin/../lib/gcc/mingw32/9.2.0/../../../../mingw32/bin/ld.exe:
C:\Users\User\AppData\Local\Temp\cctefhtT.o:code.cpp:(.text+0x1e): undefined reference to
`std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >
(std::basic_ostream<char, std::char_traits<char> >&, char const*)'
c:/mingw/bin/../lib/gcc/mingw32/9.2.0/../../../../mingw32/bin/ld.exe:
C:\Users\User\AppData\Local\Temp\cctefhtT.o:code.cpp:(.text+0x35): undefined reference to
`std::ios_base::Init::~Init()'
c:/mingw/bin/../lib/gcc/mingw32/9.2.0/../../../../mingw32/bin/ld.exe:
C:\Users\User\AppData\Local\Temp\cctefhtT.o:code.cpp:(.text+0x56): undefined reference to
`std::ios_base::Init::Init()'
collect2.exe: error: ld returned 1 exit status
我的编辑是Sublime的。
您正在看到您以前程序的结果,因为您的新程序的编译失败了。
编译你的程序失败的原因是因为你的程序依赖于来自C++标准库的代码。 您的程序没有链接到C++标准库,因为您使用了gcc
,它是C的编译器。您需要使用g++
,它是C++的编译器。