C++ compiling

bullfrog-xv

Registered
I hav Xcode and i compile my program using project builder. This works (I think) but then i cannot run it, how do i run the program I just created. If no one can help me then can someone tell me how to compile and run a program using a simple text edit.
Thanks
 
You need to make sure you have a Target set up.

The easiest way to do simple text compiling is still the Terminal and a plain-text editor (like SubEthaEdit, BB Edit, or even the venerable TextEdit in Plain Text Mode).
Just save the file somewhere and type
g++ *filename*
into the Terminal (you'll want to cd to the path of the file first though).

In XCode, look at Targets.
 
I don't mean to sound like a jerk, honest, but does no one read basic manuals any more.

If you're looking to work in a terminal window you're only hurting yourself if you don't spend a few minutes learning the basics of navigating unix/linux. Get yourself a "using linux" or "using unix" book and spend some time getting familiar with the basics.

believe me, it'll save your butt to know how to get around and how to ensure you're not breaking something.

$.02
 
Alright, I'll explain in a little more detail how to do exactly what you'll need to do:

First, create a folder somewhere that you'd like to use as your code folder. It's probably going to be in your ~/Documents folder or in /Developer.

Once that folder has been created, copy this C++ program and save it as "HelloWorld.cpp" into that folder:

Code:
#import <iostream>
using namespace std;

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

return 0;
}


Then, open the Terminal (you can find it in the Utilities folder in the Applications folder). In it, type cd *space*, but don't press return. Now go over to the window showing you the folder you created, showing the files it contains, and drag the tiny folder icon from the title bar (at the very top of the window) into the Terminal. Terminal will dump a path to that folder in. Now, in the Terminal, press return. What you've just done is to change the directory from your home folder (selected by default) to the folder which you create your code in.

You can type pwd and press return to print the path of the directory you're in (print working directory) to make sure you're in the right place.

Now type ls (that's a lowercase 'LS') and press return. That'll output all the visible files in that folder.

Now to compile your C++ program, just type:

g++ HelloWorld.cpp

That'll run HelloWorld.cpp through the GCC 3.3 Compiler (in Panther) or the GCC 4.0 Compiler (in Tiger).

To see your compiled program at work, type
Code:
./a.out
and press enter.

You should see "Hello World!" outputted.
 
Back
Top