環境
Windows11 64bit
Visual studio code 最新版
MinGWをインストールし、vscodeのpowershell上でgccを用いてコンパイルしようとしました。C言語のhello worldを表示するソースコードは問題なくコンパイルでき、その実行可能ファイルも開けました。
問題
C:\programming gcc sam1.cpp
としてprogrammingのディレクトリにあったソースコードをコンパイルしようとしました。
sam1.cppの中身は以下の通りです。
#include <iostream> using namespace std; int main() { cout << "hello" << endl; }
C:\programming>gcc sam1.cpp c:/mingw/bin/../lib/gcc/mingw32/9.2.0/../../../../mingw32/bin/ld.exe: C:\Users\arc\AppData\Local\Temp\ccE8mkLU.o:sam1.cpp:(.text+0x21): undefined reference to `std::cout' c:/mingw/bin/../lib/gcc/mingw32/9.2.0/../../../../mingw32/bin/ld.exe: C:\Users\arc\AppData\Local\Temp\ccE8mkLU.o:sam1.cpp:(.text+0x26): 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\arc\AppData\Local\Temp\ccE8mkLU.o:sam1.cpp:(.text+0x2d): undefined reference to `std::basic_ostream<char, std::char_traits<char> >& std::endl<char, std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&)' c:/mingw/bin/../lib/gcc/mingw32/9.2.0/../../../../mingw32/bin/ld.exe: C:\Users\arc\AppData\Local\Temp\ccE8mkLU.o:sam1.cpp:(.text+0x34): undefined reference to `std::ostream::operator<<(std::ostream& (*)(std::ostream&))' c:/mingw/bin/../lib/gcc/mingw32/9.2.0/../../../../mingw32/bin/ld.exe: C:\Users\arc\AppData\Local\Temp\ccE8mkLU.o:sam1.cpp:(.text+0x54): undefined reference to `std::ios_base::Init::~Init()' c:/mingw/bin/../lib/gcc/mingw32/9.2.0/../../../../mingw32/bin/ld.exe: C:\Users\arc\AppData\Local\Temp\ccE8mkLU.o:sam1.cpp:(.text+0x75): undefined reference to `std::ios_base::Init::Init()' collect2.exe: error: ld returned 1 exit status
最後の部分でエラーが発生していること自体は把握したのですが、解決方法が全く分かりませんでした。なぜCでは問題なくコンパイルできていたのにC++だとエラーが出るのでしょうか?

バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2022/02/18 14:09
2022/02/18 14:11
退会済みユーザー
2022/02/18 18:00