LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Software (https://www.linuxquestions.org/questions/linux-software-2/)
-   -   Simple program wont compile (https://www.linuxquestions.org/questions/linux-software-2/simple-program-wont-compile-301445/)

BACTRATE 03-14-2005 09:35 AM

Simple program wont compile
 
This wont compile-

#include <iostream>
int main()
{
cout <<"Hello World\n";
return 0;
}


Get error " cout undeclared."


Thanks for any help in advance

Dave

benjithegreat98 03-14-2005 09:46 AM

#include <iostream>

using namespace std;

int main()
{
cout <<"Hello World\n";
return 0;
}

------------------
or
------------------

#include <iostream>
int main()
{
std::cout <<"Hello World\n";
return 0;
}

supersucker 03-14-2005 09:47 AM

its a long time ago scince i have programmed something in C++,

but i think something like:

using namespace::std;

at the beginning of your programm should solve your problem........:-)

hope this helped

BACTRATE 03-14-2005 10:05 AM

Thanks
 
Sorry but using namespace std throws up even more errors.But thanks for thr help.

Dave

benjithegreat98 03-14-2005 10:17 AM

What's the error message? Did you use the semicolon?

BACTRATE 03-14-2005 11:28 AM

Yes used semicolon get

dave@localhost dave]$ cd downloads
[dave@localhost downloads]$ gcc hello.cpp
/home/dave/tmp/cc7hxXjR.o(.text+0xd): In function `std::__verify_grouping(char const*, unsigned int, std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)':
: undefined reference to `std::basic_string<char, std::char_traits<char>, std::allocator<char> >::size() const'
/home/dave/tmp/cc7hxXjR.o(.text+0x60): In function `std::__verify_grouping(char const*, unsigned int, std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)':
: undefined reference to `std::basic_string<char, std::char_traits<char>, std::allocator<char> >::operator[](unsigned int) const'
/home/dave/tmp/cc7hxXjR.o(.text+0x9d): In function `std::__verify_grouping(char const*, unsigned int, std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)':
: undefined reference to `std::basic_string<char, std::char_traits<char>, std::allocator<char> >::operator[](unsigned int) const'
/home/dave/tmp/cc7hxXjR.o(.text+0xc8): In function `std::__verify_grouping(char const*, unsigned int, std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)':
: undefined reference to `std::basic_string<char, std::char_traits<char>, std::allocator<char> >::operator[](unsigned int) const'
/home/dave/tmp/cc7hxXjR.o(.text+0x129): In function `main':
: undefined reference to `std::cout'
/home/dave/tmp/cc7hxXjR.o(.text+0x12e): In function `main':
: 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*)'
/home/dave/tmp/cc7hxXjR.o(.text+0x15a): In function `__static_initialization_and_destruction_0(int, int)':
: undefined reference to `std::ios_base::Init::Init()'
/home/dave/tmp/cc7hxXjR.o(.text+0x189): In function `__tcf_0':
: undefined reference to `std::ios_base::Init::~Init()'
/home/dave/tmp/cc7hxXjR.o(.eh_frame+0x11): undefined reference to `__gxx_personality_v0'
collect2: ld returned 1 exit status
[dave@localhost downloads]$



Thanks Dave

__J 03-14-2005 11:43 AM

distro?
do you have libstdc++?

benjithegreat98 03-14-2005 11:51 AM

use the command g++ instead of gcc
g++ hello.cpp -o hello
then you can ./hello

BACTRATE 03-15-2005 06:05 AM

g++ still yhe same but cout error
 
g++ still the same but cout error only .Still looking for libstdc++.

Thanks
Dave

leadazide 03-15-2005 10:25 AM

Use
using namespace std;
not
using namespace::std;

and compile it with
g++ -o hello hello.cpp

or with
gcc -o hello -lstdc++ hello.cpp

reddazz 03-15-2005 10:53 AM

The code should be as follows,

Code:

#include <iostream>
using namespace std;
int main (void)
{
    cout << "Hello, World!\n";
 
    return 0;
}

to compile it you would do something like

Code:

g++ -o hello hello.cpp

BACTRATE 03-15-2005 11:43 AM

Thanks
 
Thanks to all I will try latest suggestions and report.

Cheers

Dave

BACTRATE 03-16-2005 06:15 AM

Thanks -problem solved
 
Using
namespace std;
int main (void)
and compiling with g++ solved the problem.

Thanks to all again much appreciated.
All the best

Dave

Micky_123 07-24-2008 07:39 AM

Hi,

This code works fine on my system.

#include <iostream>
using namespace std;
int main()
{
cout<<"Hello World\n";
return 0;
}

I compiled it using g++.
You need to check the libraries if it doesn't work for u.

cheers,
Micky

Nylex 07-24-2008 12:57 PM

Why did you reply to a thread that's 3 years old and no longer requires a solution?


All times are GMT -5. The time now is 02:15 PM.