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

grapes911

Moderator emeritus
Jul 28, 2003
6,995
10
Citizens Bank Park
Brother Michael said:
Ok I just decided to spend the gig and installed XCode...If that is overkill, then I will just remove the XCode and Interface Builder...I mean I can do that right?

Anyways, I built my first program simple hello world.


#include<iostream>

int main()
{
cout << "Hello World!";

return 0;
}


Pretty sure that is all I need right? I mean I compiled it with "gcc" and it didn't like my "cout" command. Alright this program is stored in "~/documents/programs" so that I know where they all are. Is this a problem? Can my compiler not locate my "iostream" library? For 1.5 gig of an install for XCode I better have a simple input output library...

Keep in mind though I have never programmed before and even though I am fimiliar with UNIX through using Linux, I am very much a novice...

Mike

Try g++ instead of gcc
 

Brother Michael

macrumors 6502a
Original poster
Apr 14, 2004
717
0
Right after I asked this I did some looking and found I had g++ which is good considering that is what the school's UNIX system uses.

Here are the results:

micmill:~/documents/programs mike$ g++ hello.cpp -o hello
hello.cpp: In function `int main()':
hello.cpp:5: error: `cout' undeclared (first use this function)
hello.cpp:5: error: (Each undeclared identifier is reported only once for each
function it appears in.)
micmill:~/documents/programs mike$

errrm...

Mike
 

WebMongol

macrumors member
Sep 19, 2004
50
0
Bay Area, CA
Brother Michael said:
Right after I asked this I did some looking and found I had g++ which is good considering that is what the school's UNIX system uses.

Here are the results:

micmill:~/documents/programs mike$ g++ hello.cpp -o hello
hello.cpp: In function `int main()':
hello.cpp:5: error: `cout' undeclared (first use this function)
hello.cpp:5: error: (Each undeclared identifier is reported only once for each
function it appears in.)
micmill:~/documents/programs mike$

errrm...

Mike
replace line
cout << "Hello World!";
with
std::cout << "Hello World!";

cout is located in std namespace.
OR
you can add following line after #includes

using namespace std;

this means: Use the standard library namespace

For starters you can stick with old good C and use stdio.h functions

#include <stdio.h>
int main()
{
printf("Hello World (stdio)!\n");
return 0;
}
 

broken_keyboard

macrumors 65816
Apr 19, 2004
1,144
0
Secret Moon base
Brother Michael said:
Right after I asked this I did some looking and found I had g++ which is good considering that is what the school's UNIX system uses.

Here are the results:

micmill:~/documents/programs mike$ g++ hello.cpp -o hello
hello.cpp: In function `int main()':
hello.cpp:5: error: `cout' undeclared (first use this function)
hello.cpp:5: error: (Each undeclared identifier is reported only once for each
function it appears in.)
micmill:~/documents/programs mike$

errrm...

Mike

You need to put iostream.h (don't forget the .h)
It's undeclared because it is not finding the header file.
 

Brother Michael

macrumors 6502a
Original poster
Apr 14, 2004
717
0
WebMongol - oh yeah forgot about the namespace std thing. I would do C but I am learning C++ anyways in a class.

broken_keyboard - is the Apple compiler that old? Our Solaris 7 server doesn't need the .h or does age have nothing to do with it :confused:

also:

micmill:~/documents/programs mike$ g++ hello.cpp
micmill:~/documents/programs mike$ ls
a.out hello.cpp
micmill:~/documents/programs mike$ a.out
-bash: a.out: command not found
micmill:~/documents/programs mike$ wtf?
-bash: wtf?: command not found
micmill:~/documents/programs mike$

Ok I though no biggie:

micmill:~/documents/programs mike$ g++ hello.cpp -o hello
micmill:~/documents/programs mike$ ls
a.out hello hello.cpp
micmill:~/documents/programs mike$ hello
-bash: hello: command not found
micmill:~/documents/programs mike$ what?
-bash: what?: command not found
micmill:~/documents/programs mike$

umm any ideas?

Mike
 

logicat2001

macrumors regular
Apr 16, 2003
192
0
Minneapolis, MN
Hey brother michael,

I just read through this thread and strongly suggest you drop it right now and find some solid reference materials to work with. You are rushing in to a very complex set of concepts and skills, but are doing it in a way that is haphazard, filled with conjecture and accidental.

Did you look at /Developer/ADC Reference Library? You'll find half a GB of text files; a heck of a lot of text.

Open your web browser and visit this URL:
file:///Developer/ADC%20Reference%20Library/referencelibrary/index.html

You'll entertain yourself for hours and find more than enough helpful information about the compiler, the run-time system, the functional design of the OS, the frameworks, the dev tools, etc. etc.

If you're truly interested in learning, don't waste your time starting a thread like this. Go learn and come back when you understand the questions a bit more.


Good luck,
Logicat
 

Brother Michael

macrumors 6502a
Original poster
Apr 14, 2004
717
0
logicat2001 said:
Hey brother michael,

I just read through this thread and strongly suggest you drop it right now and find some solid reference materials to work with. You are rushing in to a very complex set of concepts and skills, but are doing it in a way that is haphazard, filled with conjecture and accidental.

Did you look at /Developer/ADC Reference Library? You'll find half a GB of text files; a heck of a lot of text.

Open your web browser and visit this URL:
file:///Developer/ADC%20Reference%20Library/referencelibrary/index.html

You'll entertain yourself for hours and find more than enough helpful information about the compiler, the run-time system, the functional design of the OS, the frameworks, the dev tools, etc. etc.

If you're truly interested in learning, don't waste your time starting a thread like this. Go learn and come back when you understand the questions a bit more.


Good luck,
Logicat

I have never been a very large fan of reading, especially when it comes to computer/programming documentation. I have learned more through 5 minutes of discussion about something than I have in an hour with a book.

However, I suppose that with computers this is a different story. Very well then. I read through the documentation and go from there.

Mike

EDIT: Also the link did not work.
 

logicat2001

macrumors regular
Apr 16, 2003
192
0
Minneapolis, MN
With all due respect, you might want to change your course of learning. If you're going to be programming, you're going to be reading a heck of a lot of API documentation: no ifs ands of buts.

I understand that reading does not feel particularly helpful at times. I also understand that pouring though dry, technical documentation is not very fun. Please reread my first paragraph. I enjoy going though the API documentation because I will eventually understand how to solve whatever programming problem that I'm faced with. It may be difficult, but I'm not programming for the joy of API docs, I'm programming for the joy of solving problems intelligently and elegantly. Reading the docs is par for the course and is a task that must be accomplished.

As for the URL I posted:

- I was under the assumption that you've installed the Dev Tools. If not, do that first.
- Rather than use my link, use the Finder and browse to /Developer/ADC Reference Library/referencelibrary/index.html

Also, please understand that I'm not trying to dissuade you from posting or programming. I first started in the early 80's. I'm just strongly suggesting you do a bit of foundation work now. It's very difficult to unlearn things, and it's very frustrating to realize that you've accumulated bad habits with the basic material.

Learning programming is like building a house: you start with a solid foundation. Otherwise you might find yourself tearing the entire structure down and starting over just because you overlooked something simple at the beginning.

Finally, rather than post onto MacRumors, search for another online forum that caters to programmers. You'll get much more focused and worthy advice from people that joined the forum to help others learn.

Best,
Logicat
 

jcgerm

macrumors member
May 28, 2003
91
0
Brother Michael said:
Ok I just decided to spend the gig and installed XCode...If that is overkill, then I will just remove the XCode and Interface Builder...I mean I can do that right?

Anyways, I built my first program simple hello world.


#include<iostream>

int main()
{
cout << "Hello World!";

return 0;
}


Pretty sure that is all I need right? I mean I compiled it with "gcc" and it didn't like my "cout" command. Alright this program is stored in "~/documents/programs" so that I know where they all are. Is this a problem? Can my compiler not locate my "iostream" library? For 1.5 gig of an install for XCode I better have a simple input output library...

Keep in mind though I have never programmed before and even though I am fimiliar with UNIX through using Linux, I am very much a novice...

Mike

All classes, objects, and functions are defined within what's called the standard namespace (including the iostream library). So, you have 2 choices. You can:

a) write "std::cout" everywhere you want to cout.

or
b) (better way) add a line right below your header include "using namespace std"
 

broken_keyboard

macrumors 65816
Apr 19, 2004
1,144
0
Secret Moon base
Brother Michael said:
broken_keyboard - is the Apple compiler that old? Our Solaris 7 server doesn't need the .h or does age have nothing to do with it :confused:

also:

micmill:~/documents/programs mike$ g++ hello.cpp
micmill:~/documents/programs mike$ ls
a.out hello.cpp
micmill:~/documents/programs mike$ a.out
-bash: a.out: command not found
micmill:~/documents/programs mike$ wtf?
-bash: wtf?: command not found
micmill:~/documents/programs mike$

You need to put ./ in front of the file you want to run,
e.g. ./a.out or ./hello

I don't know why your Solaris box works without the full filename, perhaps there are some filesystem links set up e.g. from iostream to iostream.h
 

iMeowbot

macrumors G3
Aug 30, 2003
8,634
0
broken_keyboard said:
You need to put iostream.h (don't forget the .h)
It's undeclared because it is not finding the header file.
With modern C++, it is correct to omit the .h suffix. With some systems, using the obsolescent form will even result in slightly different include trees and linkage, so sticking to the standardized suffix-free form is good practice.
 

iMeowbot

macrumors G3
Aug 30, 2003
8,634
0
jcgerm said:
b) (better way) add a line right below your header include "using namespace std"
Use "using namespace" with caution, though; the statement's purpose is to bypass namespace collision protection, and that protection is there for a reason. In particular, one should never attempt to use it in a header file.
 

WebMongol

macrumors member
Sep 19, 2004
50
0
Bay Area, CA
Brother Michael said:
WebMongol - oh yeah forgot about the namespace std thing. I would do C but I am learning C++ anyways in a class.

broken_keyboard - is the Apple compiler that old? Our Solaris 7 server doesn't need the .h or does age have nothing to do with it :confused:

also:

micmill:~/documents/programs mike$ g++ hello.cpp
micmill:~/documents/programs mike$ ls
a.out hello.cpp
micmill:~/documents/programs mike$ a.out
-bash: a.out: command not found
Mike

- To check version of your gcc compiler use following command:
gcc -v
With 10.3 Apple ships pretty recent gcc - 3.3

- To run your programm you must
- specify path to it:
$ g++ hello.cpp && ./a.out
- OR add current directory "." to your PATH (following is for bash shell)
$ export PATH=.:$PATH
$ echo $PATH
- OR add "." to the PATH in .bashrc
 

Brother Michael

macrumors 6502a
Original poster
Apr 14, 2004
717
0
logicat2001: Sorry my response was out of drowsiness and hunger (long story, haven't had a bite to eat all weekend), and so I let my temper get the best of me. I do not plan on programming for a career, it's a hobby that I want to take up. I know, I know I do need to change my ways a bit aboiut the learning process I am still young enough to do that.

Here's the thing, I was really just trying to do something very specific. Looking back on my posts, I can see I didn't really state that. Basically, for now all I want to be able to do is write, compile and run programs just like I can on the school's UNIX server or as close to it as possible. I do not intend to re-write OS X with a 4 week background in C++ ;) (basically run pico, and compile with g++)

The XCode questions spawned more from seeing all the tools and stuff and wondering what they were and do. I have no intention in diving straight into them jsut yet.

I will look for a programming web forum, any suggestions on you?

broken_keyboard: Gave that one a shot. Worked just fine thank you! And I guess there is a big difference between BSD and Solaris. I should be smarter than this to know that considering different distros of Linux have different commands.

iMeowbot: The compiler returns errors without the ".h" on my iBook.

iMeowbot (again): Ok. A friend of mine explained that one to me.

WebMongol: Thanks. I'll look into that one. And again I should have been smarter than that to understand that different distros do different things.

Mike
 

jcgerm

macrumors member
May 28, 2003
91
0
iMeowbot said:
Use "using namespace" with caution, though; the statement's purpose is to bypass namespace collision protection, and that protection is there for a reason. In particular, one should never attempt to use it in a header file.

Yes, however I didn't say to put it in a header file.
 

iMeowbot

macrumors G3
Aug 30, 2003
8,634
0
Brother Michael said:
iMeowbot: The compiler returns errors without the ".h" on my iBook.
Something is definitely missing then. Did you go ahead and install the developer tools/Xcode? You will probably need them even if you don't use the GUI.

The ISO standard includes definitely work with OS X, but the developer tools are needed to get the up-to-date compilers. Older versions of GCC, such as what ships with some releases of OS X (without dev tools installed), have extremely broken and out of date C++ implementations.

Code:
Meowcintosh:~/junk imb$ uname -a
Darwin Meowcintosh.local 7.5.0 Darwin Kernel Version 7.5.0: Thu Aug  5 19:26:16 PDT 2004; root:xnu/xnu-517.7.21.obj~3/RELEASE_PPC  Power Macintosh powerpc
Meowcintosh:~/junk imb$ gcc --version
gcc (GCC) 3.3 20030304 (Apple Computer, Inc. build 1666)
Copyright (C) 2002 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

Meowcintosh:~/junk imb$ cat foo.cc
#include <iostream>

int main() {
    std::cout << "Howdy.\n";
    return 0;
}
Meowcintosh:~/junk imb$ g++ -o foo foo.cc
Meowcintosh:~/junk imb$ ./foo
Howdy.
 

iMeowbot

macrumors G3
Aug 30, 2003
8,634
0
jcgerm said:
Yes, however I didn't say to put it in a header file.
You didn't, but regardless, one still has to be cautious when using the statement even outside header files. Namespace collisions are a problem in mainline code too.
 

takao

macrumors 68040
Dec 25, 2003
3,827
605
Dornbirn (Austria)
Brother Michael said:
Ok I just decided to spend the gig and installed XCode...If that is overkill, then I will just remove the XCode and Interface Builder...I mean I can do that right?

Anyways, I built my first program simple hello world.


#include<iostream>

int main()
{
cout << "Hello World!";

return 0;
}


Pretty sure that is all I need right? I mean I compiled it with "gcc" and it didn't like my "cout" command. Alright this program is stored in "~/documents/programs" so that I know where they all are. Is this a problem? Can my compiler not locate my "iostream" library? For 1.5 gig of an install for XCode I better have a simple input output library...

edit: sorry confused page and and 2 didn't realize that was already answedered

sounds like the obvous namespace error (hints: google for "using namespace") the compiler doesn't know where you want that 'cout' output
try it with
' std::cout ' instead of ' cout ' ,so it uses the standard 'namespace'
(same for ' cin ')
or you can make your whole input-out put thing and being lazy with writing ' using namespace std; ' right after your include part: but that should only be used on your small testing things and novice programms..not so nice on bigger projects etc.

version 1:
#include<iostream>
using namespace std;

int main()
{
cout << "Hello World!";

return 0;
}

version 2:
#include<iostream>

int main()
{
std::cout << "Hello World!";

return 0;
}

(i have neither a mac, xcode,linux,windowswith compiler handy so i suggest trying it yourself ;) )
 

GeeYouEye

macrumors 68000
Dec 9, 2001
1,669
10
State of Denial
You don't need the .h; in fact, the .h version of the file lives in /usr/include/c++/darwin/gcc-3.3/backward/ for a reason ;), where as iostream lives in (IIRC) /usr/include/
 

Brother Michael

macrumors 6502a
Original poster
Apr 14, 2004
717
0
Ok first never noticed this forum before. So I thought my thread was gone when it wasn't in the software area.

Anyways, I found out that the XCode and Developer Tools I have on the CD is 1.1 I figured that is where the majority of the problems are arising? I just enabled the free account on the developer section can I download the newest XCode from there?

Mike
 

jefhatfield

Retired
Jul 9, 2000
8,803
0
logicat2001 said:
Finally, rather than post onto MacRumors, search for another online forum that caters to programmers. You'll get much more focused and worthy advice from people that joined the forum to help others learn.

Best,
Logicat

i used http://www.experts-exchange.com when i first branched out into being a techie/consultant

there are thousands of users, programmers, and computer hardware types there and the site caters to people in the high tech industry...you have to sign up and register but it's free like macrumors, but if you want special privelidges, it costs a little fee

the mac people on that site know the most obscure mac related stuff and when i can't get an answer here, i go there and there are some hard core macheads/techies there...many of the professional mac techies i know won't go to this site or any that is based on speculation and they like to work with what is around in the mac world, since that's how they make their living, and are not interested in what is around the corner

i like the techie side of macs but i also enjoy all the speculation and i think macrumors has gotten every single release of apple right on the nose...but sometimes it's hard to decipher which rumors are true and which ones are hogwash

i especially liked it when macrumors got the inside on the cube and the ipod...so many people were skeptical and then a few days/weeks later, steve jobs announces those weird products and people basically slammed them...the cube was a failure like many predicted but the ipod really took off and saved apple inc during this recession

the biggest rumor in the business world has been having steve jobs leave apple and pixar and go on to head disney or sony...while that would be great for him personally, it would be hard for apple inc but if steve jobs did a good job in rebuilding apple inc, then it should survive well without him
 

mms

macrumors 6502a
Oct 8, 2003
784
0
CA
Java is being looked upon as a good starting language versus the traditional C/C++, and I'd agree. AP Computer Science A and AB dropped C++ and are teaching in Java now.
 

Mechcozmo

macrumors 603
Jul 17, 2004
5,215
2
My school offered AP Computer Science, to teach in Java. I knew a fair amount of C++ (I could make a basic program) but I wanted to learn and actually use a programming language. Well, a week into the class and only 3 people (me and 2 of my friends) had a clue as to what was going on. So, they drop the AP designation and instead make it Honors Computer Science to teach Visual Basic.NET. I was angry, because I would have been able to do the Java stuff on my Mac. Instead, I had to build a PC.

By the end of the first semester, almost everyone was failing. I had a 125 due to the curve. :eek: A lot of people transfered out, but a few people still couldn't grasp this concept of a "Class"...net result: I passed, my friends passed, everyone else didn't.

I would like to learn C++ more in-depth, and perhaps Cocca, but the Xcode form scared me...I need to be over 18 (I'm not) and stick in a lot of personal info...

That is my rant about programming and my experience with it. Best of luck Brother Michal...may the force be with you.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.