Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 289
  • Last Modified:

Can't seem to write C++ in Linux

Ok, I'm fairly new to C++ and all of my experience has been in windows.

I have created a file named test.cpp

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

I try to compile it with the following command:
gcc test.cpp

I get the following errors:

/home/d/tmp/cc1vNr7Z.o: In function `main':
/home/d/tmp/cc1vNr7Z.o(.text+0x19): undefined reference to `std::cout'
/home/d/tmp/cc1vNr7Z.o(.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*)'
/home/d/tmp/cc1vNr7Z.o: In function `__static_initialization_and_destruction_0(int, int)':
/home/d/tmp/cc1vNr7Z.o(.text+0x4a): undefined reference to `std::ios_base::Init::Init[in-charge]()'
/home/d/tmp/cc1vNr7Z.o: In function `__tcf_0':
/home/d/tmp/cc1vNr7Z.o(.text+0x79): undefined reference to `std::ios_base::Init::~Init [in-charge]()'
/home/d/tmp/cc1vNr7Z.o(.eh_frame+0x11): undefined reference to `__gxx_personality_v0'
collect2: ld returned 1 exit status

=====================================
I can't seem to find a decent tutorial in English that can help me, so could somebody please help me.

Thanks.
0
jerkface2098
Asked:
jerkface2098
1 Solution
 
GaryFxCommented:
Possibly using g++ instead of gcc might help, otherwise I think there may be a problem with your installation of gcc.

I am surprised that you got to the linker with that code.  The compiler should have complained, since you didn't say std::cout nor did you have a using.  Or was that just omitted for posting?

Gary
0
 
cmaryusCommented:
use #include <iostream.h>
0
 
fl0ydCommented:
No! Don't #include <iostream.h> as it isn't part of the c++ language. Go with what Gary suggested already.

.f
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
cmaryusCommented:
floyd: you must include <iostream> !!!
you have 2 options:
#include <iostream.h>

or

#include <iostream>
using namspace std;

maybe you thought of the second one...
0
 
fl0ydCommented:
No, I said that <iostream.h> _ISN'T_ part of the c++ language. So don't use it in c++ projects, it's only there for backward compatibility. Period.

.f
0
 
SalteCommented:
iostream is in namespace std.

try either one of the following three methods:

1. Replace cout with std::cout etc and endl with std::endl.

std::cout << ..... << std::endl;

2. Try to import those elements from the iostream library into your own namespace.

using std::cout;
using std::endl;

cout << ..... << endl;

3. Import the whole std namespace into your own namespace.

using namespace std;

cout << .... << endl;

Either one of those methods solves your problem.

Also, you might experience problems with gcc linking standard C++ programs since - at least in some versions of gcc - the standard C++ library isn't included as a library to link in for C++ programs. If you use g++ instead of gcc that will be solved, you can also use gcc and add -lstdc++ as an option to the compiler when linking the program:

gcc -o prog source.o source2.o -lstdc++

should link ok

g++ -o prog source.o source2.o

will do the same.

Alf
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now