CPP First program: can't compile cout errors

Anonymous
2024-03-12T22:10:11.16+00:00

Hi,

I wrote this simple C++ program:

#include <iostream>

int main(){

cout<<"Hello World"<<std::endl;

return 0;

}

It gives me following errors

D:\CPP programs>gcc test2.cpp test2.cpp: In function 'int main()': test2.cpp:3:3: error: 'cout' was not declared in this scope; did you mean 'std::cout'? 3 | cout<<"Hello World"<<std::endl; | ^~~~ | std::cout In file included from test2.cpp:1: c:\mingw\lib\gcc\mingw32\9.2.0\include\c++\iostream:61:18: note: 'std::cout' declared here 61 | extern ostream cout; /// Linked to standard output | ^~~~ D:\CPP programs>

Then I wrote the following:

#include <iostream>

int main(){

std::cout<<"Hello World"<<std::endl;

return 0;

}

I got the following errors

D:\CPP programs>gcc test2.cpp c:/mingw/bin/../lib/gcc/mingw32/9.2.0/../../../../mingw32/bin/ld.exe: C:\Users\zulfi\AppData\Local\Temp\ccalf8tU.o:test2.cpp:(.text+0x21): undefined reference to std::cout' c:/mingw/bin/../lib/gcc/mingw32/9.2.0/../../../../mingw32/bin/ld.exe: C:\Users\zulfi\AppData\Local\Temp\ccalf8tU.o:test2.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\zulfi\AppData\Local\Temp\ccalf8tU.o:test2.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\zulfi\AppData\Local\Temp\ccalf8tU.o:test2.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\zulfi\AppData\Local\Temp\ccalf8tU.o:test2.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\zulfi\AppData\Local\Temp\ccalf8tU.o:test2.cpp:(.text+0x75): undefined reference to std::ios_base::Init::Init()' collect2.exe: error: ld returned 1 exit status D:\CPP programs>

Somebody please guide me.

Zulfi

Developer technologies C++
{count} votes

1 answer

Sort by: Most helpful
  1. Anonymous
    2024-03-12T23:19:53.25+00:00

    Hi,

    I found that I have to use g++ compiler:

    https://www.reddit.com/r/cpp_questions/comments/s5kfex/getting_some_weird_error_form_mingwgcc_compiler_i/

    D:\CPP programs>g++ test2.cpp D:\CPP programs>

    Running the output

    D:\CPP programs>a.exe Hello World D:\CPP programs>

    Zulfi.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.