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

macfreek57

macrumors 6502
Original poster
Jan 1, 2002
379
0
Baton Rouge, Louisiana
does anybody here program in c++? or c?
i'm trying to learn but i can't figure out how to use the gcc compiler that i got from Apple's developer tools. I've read plenty and I know how it should work, but it just doesn't! I've used SimpleText and vi to write the files. i have been using .cpp as the file extension most of the time and the compiler will recognize the code that i write: the problem is that it always gives me error messages. and they're the most vague error messages too. i wrote and tried to compile this code:

#include <iostream.h>
int main{}
{
cout<<"Hello";
return 0;
}


and the error message i got was
"prog.cpp:2: syntax error before `{' token"
that's kind of vague

can anyone help me? is there something wrong with my code or is there something wrong with the way I'm using the compiler?
 
Re: C++ on OS X

Originally posted by macfreek57
does anybody here program in c++? or c?
i'm trying to learn but i can't figure out how to use the gcc compiler that i got from Apple's developer tools. I've read plenty and I know how it should work, but it just doesn't! I've used SimpleText and vi to write the files. i have been using .cpp as the file extension most of the time and the compiler will recognize the code that i write: the problem is that it always gives me error messages. and they're the most vague error messages too. i wrote and tried to compile this code:

#include <iostream.h>
int main{}
{
cout<<"Hello";
return 0;
}


and the error message i got was
"prog.cpp:2: syntax error before `{' token"
that's kind of vague

can anyone help me? is there something wrong with my code or is there something wrong with the way I'm using the compiler?

Well, I might be 100% wrong because I don't program in C++, but in most programming languages you have a statement more like:

int main()
{
code here
}

In fact, I would be willing to bet that that is the issue. int main{} is just really weird. Hope this helps:)
 
Yeah, it does need tot been int main(). Also, I think you need a space between 'cout' and '<<' and that and whatever you're cout-ing.

#include <iostream.h>

int main()
{
cout << "hello world!";
// the return statement is technically unnecessary, but you can put it in if you want.
}
 
You'll probably also want to send an end-of-line:

cout << "hello world!" << endl;

You should normally return 0 from main unless there's been a problem.

Any block of code should have braces {} to keep everything packaged. You may find that what you're doing is simple at the start and yet later, it becomes more complex and you'll add them to keep things orderly.

You may use them as such:

if(condition)
{
code
}
else
{
code
}

while(condition)
{
code
}

do
{
code
} while(condition);

switch
{

case condition:
{
code
break;
}

default:
{
code
break;
}
}

and then of course, you use braces to define the contents of a function, as you've done with main().
 
you guys have been really helpfull
you were right about the main() thing
i have up to this point been only familiar with html and BASIC coding so i am not familiar with languages arranged like C++ (which looks a lot like java, superficially anyway, to me).

but i'm still having problems.

could someone rewrite my code or even a simpler program just so that i can get something to compile? it's really frustrating
 
you've been using GCC from the terminal? why not just use project builder? IDEs are more fun :)

i never could get my C++ programs to compile using GCC in the teminal. i tried to do it with hello world, but it always screwed up with the iostream.h.... i don't know why. it works fine when i use plain C, with stdio.h.
 
After reading this thread, I couldn't figure out why your code wouldn't compile (after giving the correct int main() syntax, that is). So I did some invetigating. This is what I found...

I wrote the same code that you did. Here is what I wrote:

Code:
#include <iostream>

int main()
{
        cout << "Hello world!";
        return 0;
}
Notice how iostream is included. This is important.

I then tried building it with the following command.
Code:
gcc test.C
This didn't work. It failed with the following error:
Code:
test.cpp: In function `int main()':
test.cpp:5: error: `cout' undeclared (first use this function)
test.cpp:5: error: (Each undeclared identifier is reported only once for each 
   function it appears in.)
This basically means the linker couldn't find the library in which cout is defined in. This is an odd error for a symbol that should have been defined in libraries that are included by standard.

So I tried compiling the program in Project Builder. It worked. So I looked at the compile script for my project and found that it wasn't calling gcc. Instead it was calling g++2.

So, back on the command line, I tried the following command:
Code:
g++2 test.cpp

Compiled and ran just fine. I'm pretty sure g++2 is actually just an older version of the c++ compiler that links to the libraries in /usr/lib/gcc/darwin/2.95.2/. So you might be stuck using the older compiler in order to use iostream. iostream is deprecated on many systems anyway.

Another route you might want to take is just using stdio.h and printf() to print to the command line. Here is an example program:
Code:
#include <stdio.h>

int main()
{
        printf("Hello World!\n");
        return 0;
}

This compiles just fine with gcc.

Also, about the necessity of 'return 0;' Technically, you should need to have a return statement in any function which specifies a return. For instance, you specified the main function as 'int main()' which means you want to return an int. Thus, you should return an int somewhere in the function.

The c++ compiler makees a special except for main, however, and lets you get away with no return. If it was me, I'd get used to returning the correct types in functions, even in main.

Taft
 
I started taking a C++ class a few weeks and the first part of our source files like this:

#include <iostream> // note: not iosteam.h
using namespace std;

int main()
{
code


return 0;
}

I think the the iostream.h is a carry over from C and used by older compilers.
 
Originally posted by Fender2112
I started taking a C++ class a few weeks and the first part of our source files like this:

#include <iostream> // note: not iosteam.h
using namespace std;

int main()
{
code


return 0;
}

I think the the iostream.h is a carry over from C and used by older compilers.

Memories... :) It has been years since my serious c++ coding..

Actually iostream never was in C. Also the .h is required by g++ built into OS X. (At least on my build.)

Also to respond to some previous statements, c++ does not require a space before the << in the cout command.

Gee is right about the return however, which was new to me. I could swear that I used to have "void main()" instead of "int main()" (But if I had int I had to return something...) But the compiler swears main must return an int and doesn't allow it being declared a void. Although it doesn't seem to mind if I do not return an int like Gee pointed out....

Ahhh, I enjoyed stretching my c++ programing muscles again... Boy do I love my Mac! Built in gcc and g++!
 
Originally posted by Fender2112
I started taking a C++ class a few weeks and the first part of our source files like this:

#include <iostream> // note: not iosteam.h
using namespace std;

int main()
{
code


return 0;
}

I think the the iostream.h is a carry over from C and used by older compilers.

Actually, the '"iostream.h"' was valid syntax for the standard c++ library through gcc 2.95.2. Now, only the '<iostream>' is valid when accessing headers from the c++ standard library.

More info...

I figured out why the 'cout' symbol wasn't defined when issuing the 'gcc test.C' compilatio command. Its because the stdc++ library isn't searched automatically by gcc.

The g++ command is just the same as the gcc command, except that it adds the stdc++ library to the search list for the ld command. You can force gcc to do this as well.

As an example, this is the command we used to compile our c++ hello world program:
Code:
g++ test.C
Here is the equivilent command using gcc:
Code:
gcc test.C -lstdc++
Notice the added option -l. This means: search the library 'libstdc++.a' for symbols. Now your program compiles using the gcc command.

Finally, note the the placement of the -l option in the command is very important. For example, the following code will not compile!
Code:
gcc -lstdc++ test.C

This won't compile because it searches the stdc++ library before the symbol is needed. The idea is that you have to ask for the symbols before the symbols are defined. Our test.C file "asks" for the cout symbol and the stdc++ library defines the symbol. Hence the order.

Taft
 
A couple of other notes: first, IIRC, you can still use gcc (as opposed to g++) if you either have a "using namespace std;" line or your "cout"s are done as "std::cout" instead. Second, to find out exactly how to do things, run in the Terminal "cd /; touch foo.txt; man gcc > /foo.txt" and read foo.txt, in your root directory.

It's long and rather confusing, but quite helpful.

EDIT: never mind, anything that was in bold in the man file shows up as a string of four of the same letter in a row. Better to just set scrollback very high, scroll down through the file, and copy and paste into an RTF in TextEdit.
 
Originally posted by GeeYouEye
A couple of other notes: first, IIRC, you can still use gcc (as opposed to g++) if you either have a "using namespace std;" line or your "cout"s are done as "std::cout" instead.

Not unless you specify to search the c++ standard library in the gcc command. See above for more info.

Also, the "std::" specifier is unnecessary using gcc's default options. Its only necessary if you plan on defining a cout symbol of your own, or use an option to enforce full namespace qualification on all symbols.

Taft
 
Originally posted by Fender2112
I started taking a C++ class a few weeks and the first part of our source files like this:

yeah i'm supposed to be doing the same thing right now only it's just a general "Computer Science" class. I'm the only one in the class (high school, by the way) and so I was supposed to be deciding what I wanted to learn for the year (I chose C++). my teacher has an extensive programming knowledge, but the punk wants me to learn MS Access.
bunch of crap
not that I'm not enjoying and learning, just that I wouldn't have to be learning C++ from the internet if we could just hop to it!

anyway
thanks guys you've been a lot of help
and i'm sure i'll understand more of what you guys were talking about when I get a little deeper into C++!
 
Pardon my ignorance, but I couldn't help notice that these tip you folks are giving for GCC seem cryptic, almost like DOS commands. Does GCC or Apple's Project Builder use a GUI or is it all command lines from the terminal.

I've been using a learning edition of CodeWarrior and seems pretty easy after you spend some time with it. It is also simular to Visual .Net that we use in my programming class.

I was thinking about trying Project Builder. But as a beginner, you folks are scaring me with all these command line and terminal commands. I had nightmares lastnight. ;)

Any insight as to how the different IDE's and compilers compare to each other would be really helpful.
 
Fender:

Everything we were talking about was command line compiling. I, personally, haven't tried to use the graphical stuff, but I might give it a try.

MacFreek:

I bet your teacher doesn't know C++ that well. Ask him if you can use Visual C++ for your interface for Access. ;)
 
Originally posted by Fender2112
Pardon my ignorance, but I couldn't help notice that these tip you folks are giving for GCC seem cryptic, almost like DOS commands. Does GCC or Apple's Project Builder use a GUI or is it all command lines from the terminal.

I've been using a learning edition of CodeWarrior and seems pretty easy after you spend some time with it. It is also simular to Visual .Net that we use in my programming class.

I was thinking about trying Project Builder. But as a beginner, you folks are scaring me with all these command line and terminal commands. I had nightmares lastnight. ;)

Any insight as to how the different IDE's and compilers compare to each other would be really helpful.

It's much easier to use CodeWarrior for programmes which output plain text rather than use a GUI because CodeWarrior's SIOUX library outputs the text to a Window.

Project Builder has various success in showing the output correctly. I've found it better to edit the source with PB and compile and run from the command line.

Btw, remember that DOS command line was based on CP/M and UNIX, not the other way around. ;)
 
fender: just in case those responses didn't clarify this, project builder is an IDE a la Code Warrior and MS visual C++, but GCC, in the sense we are talking about, is a command line tool.

granted, though, Project Builder actually uses GCC to compile the programs you make in it, so it's kind of an IDE level of GCC, as written by apple.
 
Originally posted by Fender2112
Pardon my ignorance, but I couldn't help notice that these tip you folks are giving for GCC seem cryptic, almost like DOS commands. Does GCC or Apple's Project Builder use a GUI or is it all command lines from the terminal.


yeah Project Builder is basically the same setup as the MS suite of visual progamming applications: you design the interface graphically and use c ( or c++ or objective-c) or java to code the actions and events and such. it's what developers use to create most programs you see for the mac (cocoa).

by the way, my teacher caved today. we figured out that we couldn't do what we wanted to do without some sort of coding.
so now we're building a Visual BASIC program to handle the Access database. :)
at least we're doing some coding!
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.