PDA

View Full Version : C++ Coding in OS X...


Dogcow
09-24-2002, 12:26 PM
I'm normally a Java monkey but I'm being forced to re-learn C++ for some huge projects.

I tried to start back up with a classic helloworld program, but can't get it to work. I created a C file in Project Builder (hello.h) and used the following code:

#include <Carbon/Carbon.h>
#include <iostream.h>

main()
{

cout << "Hello World! ";
}

I attempted to compile from the command line via 'gcc hello.h' and got the following errors:

%gcc hello.c
hello.h:11: header file 'iostream.h' not found
cpp-precomp: warning: errors during smart preprocessing, retrying in basic mode

Any help would be appreciated, I have to dive in to some much harder stuff very soon.

-Dogcow "moof!"
Visit The Underground (http://theunderground.eon.com.au)

ateles
09-24-2002, 01:46 PM
The .h termination is unnecesary in C++ for standard headers. Just replace by

#include <iostream>

If you want to use a C header use c in front. For example for stdio.h

#include<cstdio> or
#include<stdio.h> will also work, but this is a C not a C++ header. Good luck,

ateles

blb
09-24-2002, 02:30 PM
Name your file hello.cpp instead of hello.h, then gcc will know it is a C++ file, not just a header file; also, use c++ (or g++) to do the compile

c++ hello.cpp

This will result in an executable called a.out; run it with ./a.out. If you want to call it something else,

c++ -o hello hello.cpp

to call the resulting executable hello.

Since you started off in PB, you can create a project of type C++ Tool, which will create a .cpp file for you, and of course handle building and what not.

Dogcow
09-24-2002, 03:16 PM
Sweet- thanks man, that worked.

Now does anyone know how to access the OpenGL libraries? #include <GL/glut.h> and #include <glut.h> don't work...

TIA,
-Dogcow "moof!"
Visit The Underground (http://theunderground.eon.com.au)

moishe
09-24-2002, 03:29 PM
To include OpenGL and GLUT you need to (If using project builder) add the "OpenGL" and "GLUT" frameworks to the project. I don't know if this automatically adds the "#include" lines to your source file, but here's how it should look:

#include <OpenGL/gl.h>
#include <OpenGL/glu.h>
#include <GLUT/glut.h>

If you compile the source from the command line, you need to link to the framework rather than the library:

cc -framework OpenGL -framework GLUT -lobjc source.c

That's all one line. You need to link to the objective c library "-lobjc" when using plain C (at least that's what I've found). I don't know how to tell Project Builder how to do this and so pass the "-lobjc" to the builder explicity. Anyone know the "correct" way to do this?

Hope this helps.