tags:

views:

208

answers:

3

Hi I'm new to C++ programming and I try to make my first exercise on a mac using gcc in the terminal.

Unfortunately, I can't compile because of issues related to iostream. With a simple program as :

#include "<"iostream">"

int main() {

 std::cout << "hello world";
 std::cout << endl;
 return 0;

}

it gives me the error:

error: ‘endl’ was not declared in this scope

removing the cout << endl; line gives me these errors:

Undefined symbols: "___gxx_personality_v0", referenced from: ___gxx_personality_v0$non_lazy_ptr in cceBlyS2.o "std::ios_base::Init::~Init()", referenced from: ___tcf_0 in cceBlyS2.o "std::basic_ostream >& std::operator<< >(std::basic_ostream >&, char const*)", referenced from: _main in cceBlyS2.o "std::ios_base::Init::Init()", referenced from: __static_initialization_and_destruction_0(int, int)in cceBlyS2.o "std::cout", referenced from: __ZSt4cout$non_lazy_ptr in cceBlyS2.o ld: symbol(s) not found collect2: ld returned 1 exit status

It's evident that the iostream header is not properly linked. I tried "<"iostream.h">" and "iostream.h" with no success.

Does anybody has any hint that could help me? Thanks!

A: 

You just need to use std::endl;. Or, better yet, make use of the handy using directive:

#include <iostream>

using namespace std;

int main()
{
    cout << "hello world";
    cout << endl;
    return 0;
}
e.James
It's less against the purpose of a namespace to `using namespace std::x;` for a given `x` that you need to access, rather than to bring the entire namespace into scope in one fell swoop.
Chris
@Chris: I disagree. While I would certainly avoid something like `using namespace std;` in a *header file*, I think it is perfectly fine inside a source file, especially a simple one like this. I see no reason to pollute the source file with `std::` all over the place unless it is absolutely necessary. I would also prefer to never re-use a name that was already used in the STL. Bringing in the whole namespace would let me catch such a mistake early.
e.James
@e.James: there's one minor problem with that: you don't know what's in the `std` namespace -- it can and does vary between implementations. You can have something work fine for an arbitrary length of time, and then quit working on a different (version of a) compiler. Admittedly, in *this* case, where the *only* name you're defining is `main`, there's not much that can go wrong -- but when/if you do any more than that, it's a whole different story.
Jerry Coffin
@Jerry Coffin: I have to admit that I've never experienced such a thing. Do different implementations really have different public names in the `std` namespace?
e.James
@e.James:Yes, definitely. Just for one example, if you want POSIX compatibility, you *have* to define a fair number of names (some in standard headers) that aren't in the C or C++ standards.
Jerry Coffin
Well, that shouldn't be really an issue....Borland C++ and GCC/G++ follows the same protocol when declaring using namespace std (as far as I'm concerned). I don't know about other compilers but that's never been an issue so far.
The Elite Gentleman
+7  A: 

You need to use "std::endl;" -- the entire standard library is in the std namespace. It also looks like you used gcc instead of g++ on the command line. The latter automatically does the steps necessary to link C++ correctly.

Jerry Coffin
Gratitude to you! Everything works now.Thanks for helping a total newbie like me !
Patof
@Patof, have you considered accepting your answer that solved your problem? You need to build up an acceptance rate.
The Elite Gentleman
+2  A: 

endl; falls under the std namespace

your 2 options are as follows:

1) declaring your namespace, e.g.

#include <iostream>
using namespace std;

int main() {
 cout << "hello world";
 cout << endl;
 return 0;
}

or using std::endl; e.g.

 std::cout << "hello world";
 std::cout << std::endl;
 return 0;

See which one suits you. I recommend 1) (Check that I didn't do std::cout because I've declared my namespace already) as it helps to reduce typing std:: everytime.

The Elite Gentleman