Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

Mann

macrumors newbie
Original poster
May 18, 2009
5
0
I am just learning to program using c++ and am having difficulty with the terminal. I have been searching different forums, but cannot seem to find an answer.

to compile the program I first configured textedit to allow me to write .cpp files. Basically, I went into the preferences and configured everything how the a page I found told me to. I also used TextWrangler which came up with the same results.

I saved the file "hello.cpp" on the desktop. I then went to the terminal where I first entered:

cd desktop

I then put:

g++ -Wall -W -pedantic -o hello hello.cpp

And I received error messages no matter what program I wrote.

First, I am trying to learn using Bjarne Stroustrup's, "Programming: Principles and Practice Using c++" In the second chapter the code he says to enter is:

#include "std_lib_facilities.h"

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

That came back with :

hello.cpp: In function ‘int main()’:
hello.cpp:6: error: ‘cout’ was not declared in this scope


So I tried:

#include <iostream.h>

main()
{
cout << "Hello World!";
return 0;
}

That came back with :

hello.cpp:3: error: ‘::main’ must return ‘int’
warren-woodrich-pettines-macbook-air:desktop wwp$ #include <iostream.h>
warren-woodrich-pettines-macbook-air:desktop wwp$
warren-woodrich-pettines-macbook-air:desktop wwp$ main()
> {
> cout << "Hello World!";
-bash: !": event not found
> return 0;
> }

when I entered ./hello it said there was no file

I have tried a couple other variations on the internet and have found none that work. What am I doing wrong? Is Stroupstrup's book compatible with Mac, and if not why?

Thanks so much for the help. Hopefully I am just making a newb mistake
 
I'm not familiar with that book, but either it's using outdated standards (or non-standards) or it's just really old, or both :)

Try this:
Code:
#include <iostream>
using namespace std;

int main()
{
    cout << "Hello World!";
    return 0;
}
 
I'm not familiar with that book, but either it's using outdated standards (or non-standards) or it's just really old, or both :)

Well, if I look here, it seems like it's quite new. It's also by Mr. C++ himself, of course.

I must say that std_lib_facilities.h was new to me, too.
 
As an Amazon Associate, MacRumors earns a commission from qualifying purchases made through links in this post.
it looks like it is custom header file(std_lib_facilities.h), does it exist on your desktop

you could also try

gcc hello.cpp -o hello.a

for compiling
 
Thanks for the responses, but it looks like none of these things are helping.

I used the new code you suggested as well as the gcc hello.cpp -o hello.a
and it came back with:

i686-apple-darwin9-gcc-4.0.1: hello.cpp: No such file or directory
i686-apple-darwin9-gcc-4.0.1: no input files

So I tried prefacing it with cd desktop, and it gave me:

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

Is there a problem with my compiler? I've tried running these using Xcode and came up with similar problems.

Thanks again for taking the time to respond
 
hello.cpp
Code:
#include <iostream>
using namespace std;

int main () {
[INDENT]cout << "Hello world!" < endl;
return 0;[/INDENT]
}

command line
Code:
g++ -o hello hello.cpp
./hello
^ needs to be run where your hello.cpp is, the command line doesn't magically know where to look.

btw g++ and gcc are the same thing

basic format of the g++ compiler:

g++ -o [name of your compiled program] [source code]
 
I don't understand what is the problem, would have to check it out on some gcc forums.

I managed to compile this

#include <stdio.h>
int main ()
{
fprintf(stderr,"Hello world!");
// cout << "Hello world!";//<< endl;
return 1;
}

for now you could continue using this, may be some of these call have been deprecated by gcc(just a assmption).
 
Didn't we just go through this with someone else?

Getting started with C++

Did you install the developer tools or are you trying to use the built-in compiler?

** Added

If you want to use that header, your going to need to add the path where the directory resides on the command line

If you installed source code for the book, you need to put the location of the headers into the command line.

ie. The books source code is installed into /Users/MyName/examples and the header file is located in /Users/MyName/examples/include, your command line should look like this:


g++ -o testFile -I/Users/MyName/examples/include testFile.cpp

This will look for header files in the default directories and also any headers in the examples/include directory.

Mike...
 
That other forum helped a lot. I guess i didn't find it in my search.

Thanks.
 
Well, if I look here, it seems like it's quite new. It's also by Mr. C++ himself, of course.

I must say that std_lib_facilities.h was new to me, too.

Strange. Maybe it's some file that comes with the CD in the book or something...
 
As an Amazon Associate, MacRumors earns a commission from qualifying purchases made through links in this post.
Strange. Maybe it's some file that comes with the CD in the book or something...

The name of the file was not one of the standard header names, C++ standard headers don't end in .h anyway, and the include command was

#include "somefilename.h"

and not

#include <somefilename.h>

which means it is not a standard header, but a file name that the programmer is supposed to supply.
 
There's a few of the books that include those catch-all header files. They do this to avoid having to retype the same 5 headers into the code.

It saves them page space for the more important lines of code...

It's really confusing when you jump into the middle of a book and can't remember what the heck was in that generic header to begin with.
 
the answer

I sent some emails back and forth with Stroustrup and it turns out

std_lib_facilities.h

is a catch all header that simplifies everything till later in the book. I think someone mentioned this earlier in the thread.

He emailed me a copy of the file and now everything is working fine. If anyone reads this who is also having trouble, just hit him with an email and he's really responsive.
 
I sent some emails back and forth with Stroustrup and it turns out

std_lib_facilities.h

is a catch all header that simplifies everything till later in the book. I think someone mentioned this earlier in the thread.

He emailed me a copy of the file and now everything is working fine. If anyone reads this who is also having trouble, just hit him with an email and he's really responsive.

Or google that filename and find this link:
http://www.stroustrup.com/Programming/std_lib_facilities.h
(EDIT: It looks like Dr. Stroustrup's site is down... if you search for the URL, the cached copy is available from google)

Even if Dr. Stroustrup isn't teaching a class during the summer, he probably still has other things to be working on rather than responding to a bevy of requests for this file (not knocking the OP, this is for reference for others that may need to seek out the file).

-Lee
 
Or google that filename and find this link:
http://www.stroustrup.com/Programming/std_lib_facilities.h
(EDIT: It looks like Dr. Stroustrup's site is down... if you search for the URL, the cached copy is available from google)

Even if Dr. Stroustrup isn't teaching a class during the summer, he probably still has other things to be working on rather than responding to a bevy of requests for this file (not knocking the OP, this is for reference for others that may need to seek out the file).

-Lee

Good point. His website was down when I posted.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.