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

novicew

macrumors member
Original poster
Jan 4, 2006
71
0
Hamburg
I am pretty new to Apple and trying to write my first c++ program on XCode platform. However due to it's complexity I am kind of stuck not knowing from where to start with. I would really appreciate if someone can explain me how to do that.

#include<iostream>
using namespace std;
int main()
{
cout <<"Hello World!\n";
cin.get();
}

By the way, I had no problem running it on Terminal.

Thank you in advance.
 

mwpeters8182

macrumors 6502
Apr 16, 2003
411
0
Boston, MA
novicew said:
I am pretty new to Apple and trying to write my first c++ program on XCode platform. However due to it's complexity I am kind of stuck not knowing from where to start with. I would really appreciate if someone can explain me how to do that.

#include<iostream>
using namespace std;
int main()
{
cout <<"Hello World!\n";
cin.get();
}

By the way, I had no problem running it on Terminal.

Thank you in advance.

When you get started in XCode, you want to choose new project, then "C++ Tool" under "Command Line Utility". This will create a project that is of the type you're looking to create. main.cpp even has hello world already in it to start.
 

novicew

macrumors member
Original poster
Jan 4, 2006
71
0
Hamburg
Hi mwpeters8182

I managed to get that far too. My problem is to execute some of the *.cpp files I have already written on Windows. Is there a way to just open a *.cpp file, compile it and execute it in the same platform.

Thank you for the help anyway
 

Catfish_Man

macrumors 68030
Sep 13, 2001
2,579
2
Portland, OR
novicew said:
I managed to get that far too. My problem is to execute some of the *.cpp files I have already written on Windows. Is there a way to just open a *.cpp file, compile it and execute it in the same platform.

Thank you for the help anyway

Use gcc in terminal, like you said originally... that's the easiest way to compile simple CLI C++ programs. Xcode is designed for larger stuff.
 

slooksterPSV

macrumors 68040
Apr 17, 2004
3,544
306
Nowheresville
Catfish_Man said:
Use gcc in terminal, like you said originally... that's the easiest way to compile simple CLI C++ programs. Xcode is designed for larger stuff.
CLI - Command Line Interface - and ummm as long as you don't need a sepcific library like SDL (that one you can just use XCode to set it up and that) -the lib files- then just use the terminals CLI if you know it. Otherwise, maybe I can get Dev-C++ for Linux working or something on Mac. XCode is great though.
 

bousozoku

Moderator emeritus
Jun 25, 2002
16,120
2,397
Lard
novicew said:
I managed to get that far too. My problem is to execute some of the *.cpp files I have already written on Windows. Is there a way to just open a *.cpp file, compile it and execute it in the same platform.

Thank you for the help anyway

Depending on your project, you can just copy and paste the text into the main.cpp file since it's already known to be the main file. Otherwise, you should copy the files into the directory and add them to the project and then, build the project.
 

Soulstorm

macrumors 68000
Feb 1, 2005
1,887
1
novicew said:
I managed to get that far too. My problem is to execute some of the *.cpp files I have already written on Windows. Is there a way to just open a *.cpp file, compile it and execute it in the same platform.

Thank you for the help anyway
Since you have already done that, I suppose that can compile hello world application and run it, but there are some programs written on the PC that just don't compile.

That is because there are some differences in some PC Commands: for example:
Code:
system("PAUSE");
will not work, because it is a Windows specific subroutine and not C++ specific. Another command that won't work is
Code:
clrscr();
This is also windows specific. There are other commands like these. OS X only supports commands that are specified in ANSI C++and not those windows specific sub-routines.
 

bousozoku

Moderator emeritus
Jun 25, 2002
16,120
2,397
Lard
Soulstorm said:
Since you have already done that, I suppose that can compile hello world application and run it, but there are some programs written on the PC that just don't compile.

That is because there are some differences in some PC Commands: for example:
Code:
system("PAUSE");
will not work, because it is a Windows specific subroutine and not C++ specific. Another command that won't work is
Code:
clrscr();
This is also windows specific. There are other commands like these. OS X only supports commands that are specified in ANSI C++and not those windows specific sub-routines.

Many of those include C header files. I believe conio.h is one of those such header files, but it's been a long time since I did any DOS programming.

It would be easy enough to do a pause programme but if u'r not already using a shell to execute the programme directly, I'm not sure what the output would be. Of course, C++ doesn't really offer native unbuffered input, so you would have to resort to C (fgetc using stdin would probably be a good choice) to halt execution.
 

Soulstorm

macrumors 68000
Feb 1, 2005
1,887
1
compiles fine on me. You fail to mention what kind of errors are those 6 arrors that you get...
 

Josh

macrumors 68000
Mar 4, 2004
1,640
1
State College, PA
The one thing I noticed when using XCode for C++ that was different from using the terminal (g++) or MS Visual Studio, is that XCode requires me to use:

Code:
std::cout << whatver is here

Rather than just
Code:
cout << whatver is here
.

I'm sure there are plenty other differences I will notice as I go on, but just started my class last week and this is what I've noticed so far.
 

Soulstorm

macrumors 68000
Feb 1, 2005
1,887
1
Josh said:
The one thing I noticed when using XCode for C++ that was different from using the terminal (g++) or MS Visual Studio, is that XCode requires me to use:

Code:
std::cout << whatver is here

Rather than just
Code:
cout << whatver is here
.

I'm sure there are plenty other differences I will notice as I go on, but just started my class last week and this is what I've noticed so far.
that's just the namespace definition.
at the top of the program, use "use namespace std;" and it will not require you to do so. Perhaps you should read more about namespaces.
 

Sogo

macrumors 6502
Jan 4, 2004
352
0
Ok how do i compile main.cpp using the terminal? This is a c++ program.

Oh, and how do i get it to run...
 

CookieMook

macrumors newbie
Jan 25, 2006
2
0
Sogo said:
Ok how do i compile main.cpp using the terminal? This is a c++ program.

Oh, and how do i get it to run...

type g++ <yourCppFilename.cpp> in the terminal. It should produce an application (executable file) if all is well. Just type the name of the application and it should run.
 
CookieMook: close... If you don't provide the -o argument, it'll simply create the binary executable named "a.out".
Code:
g++ -o myprogram myprogram.cpp
What's even better, learn makefiles. They'll take care of compilation easier than manually compiling.

novicew: why do you have the last two lines uncommented. delete those or comment those out. like:
Code:
// rect area: 12
// rectb area: 30
and just some tip for you with your area() method, you might want to make it const like:
Code:
int area() const; // in the class scope
and move the actual implementation as:
Code:
int CRectangle::area() const { return width * height; }
this way you can guarantee the area() method will NOT mutate the width or height variables.

Though I'd suggest you move the CRectangle:: stuff to a .cpp file and the class declaration in a header file. Xcode will automatically compile and link them all together.

A final suggestion, use 'unsigned int' because neither width nor height can be negative. if you explictly try to add a negative integer the compiler will complain, however if you simply pass from input, it won't check, so you'll have to do the checking code if you use the user's input. So:
Code:
#include <iostream>

using namespace std;

class CRectangle {
        unsigned int width, height;
    public:
        CRectangle( unsigned int w, unsigned h );
        unsigned int area() const;
};

CRectangle::CRectangle( unsigned int w, unsigned int h ) {
    width = w;
    height = h;
}

unsigned int CRectangle::area() const {
    return width * height;
}

int main( int argc, char* argv[] ) {
    CRectangle rectA( 3, 4 );
    CRectangle rectB( 5, 6 );
    
    cout << "rectA area: " << rectA.area() << endl;
    cout << "rectB area: " << rectB.area() << endl;
    
    return 0;
}

// rectA area: 12
// rectB area: 30
compiles great with g++ -o foo foo.cpp
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.