?
Solved

Can't seem to write C++ in Linux

Posted on 2003-03-30
6
Medium Priority
?
279 Views
Last Modified: 2010-04-01
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
Comment
Question by:jerkface2098
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 6

Expert Comment

by:GaryFx
ID: 8236456
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
 
LVL 3

Expert Comment

by:cmaryus
ID: 8236633
use #include <iostream.h>
0
 
LVL 8

Expert Comment

by:fl0yd
ID: 8237022
No! Don't #include <iostream.h> as it isn't part of the c++ language. Go with what Gary suggested already.

.f
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 3

Expert Comment

by:cmaryus
ID: 8237051
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
 
LVL 8

Expert Comment

by:fl0yd
ID: 8237082
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
 
LVL 12

Accepted Solution

by:
Salte earned 100 total points
ID: 8237186
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

Enroll in August's Course of the Month

August's CompTIA IT Fundamentals course includes 19 hours of basic computer principle modules and prepares you for the certification exam. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.
Suggested Courses

800 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question