PDA

View Full Version : C Programming on my PB




AAArroyo
Jun 25, 2007, 08:40 AM
I would like to re-learn C by programming the examples in the newest Deitel & Deitel C How To Program book (5/e) on my MAC PB. How do I get started? I gather that GCC is on the MAC Developer tools, but frankly, my Unix usage is over 20 years old and I need some hand holding. I am not sure how to get the developer tools going, either from the MAC GUI, or from a Terminal window. In other words, how do I compile and run a simple "Hello World/n" printf statement on the PB? :)



robbieduncan
Jun 25, 2007, 08:45 AM
Assuming you have created the plain text file with the source code in it (so if you are using TextEdit make sure you have not saved an rtf file) and saved it in your home directory then open the Terminal and type


gcc <filename>

kainjow
Jun 25, 2007, 08:46 AM
First create a free ADC account at connect.apple.com (http://connect.apple.com), login and download "Xcode 2.4.1" under "Developer Tools".

Once you have Xcode, launch it, go to File > New Project, and choose "C++ Tool" under "Command Line Utility", and it will give you a basic "Hello World" C++ cli app.

By the way, it's Mac, not MAC. Two very different things. :)

cblackburn
Jun 25, 2007, 08:46 AM
The first step as you thought is to either download the developer tools from apples website or install them from your CD. Downloading is the better option as you get the latest version of both the compilers and the documentation however it's a 1GB download :).

Second you have two options. You can use Xcode that is provided which means you can set up projects which have multiple source files and compile together easily however the IDE will probably be a bit of a learning curve at the moment.

You can just use the compiler directly from the command line as follows:-
gcc -o executable_name source_file.c

That should get you started

Chris

AAArroyo
Jun 25, 2007, 11:49 AM
Assuming you have created the plain text file with the source code in it (so if you are using TextEdit make sure you have not saved an rtf file) and saved it in your home directory then open the Terminal and type


gcc <filename>


Turns out I had a "Developer" folder, but it had not been installed. After the 3+ hour (1GB) download of XCODE completed, I installed it and tried it. It ran and compiled and generated a file named a.out as expected. However, when I typed a.out and hit <return> I got
tcsh: a.out: Command not found.

It was not until I typed the full path of the file, /Users/arroyo/a.out that I got the program to run.

pwd returns

[AAA-G4-1-67-GHz:~] arroyo% pwd
/Users/arroyo

Why do I have to type the full path for it to work? See example:

[AAA-G4-1-67-GHz:~] arroyo% a.out
tcsh: a.out: Command not found.
[AAA-G4-1-67-GHz:~] arroyo% /Users/arroyo/a.out
Hi, World
[AAA-G4-1-67-GHz:~] arroyo%

I am so rusty in Unix, that it is not obvious why?

Thank you for your help!

robbieduncan
Jun 25, 2007, 11:55 AM
The working directory (./) is not in your PATH. You could edit your profile (say the .bashrc file) to add this to your PATH environment variable, or just learn to live with typing ./a.out

Note that the -o <filename> command switch to gcc suggested above lets you have the compiler create an executable with any name you want :)

AAArroyo
Jun 25, 2007, 12:15 PM
First create a free ADC account at connect.apple.com (http://connect.apple.com), login and download "Xcode 2.4.1" under "Developer Tools".

Once you have Xcode, launch it, go to File > New Project, and choose "C++ Tool" under "Command Line Utility", and it will give you a basic "Hello World" C++ cli app.

By the way, it's Mac, not MAC. Two very different things. :)

Ditto on the Mac...

Turns out I had a "Developer" folder, but it had not been installed. After the 3+ hour (1GB) download of XCODE completed, I installed it and tried it. I guess I can try and learn C++ also...

Thank you for your help!

AAArroyo
Jun 25, 2007, 12:18 PM
The first step as you thought is to either download the developer tools from apples website or install them from your CD. Downloading is the better option as you get the latest version of both the compilers and the documentation however it's a 1GB download :).

Second you have two options. You can use Xcode that is provided which means you can set up projects which have multiple source files and compile together easily however the IDE will probably be a bit of a learning curve at the moment.

You can just use the compiler directly from the command line as follows:-
gcc -o executable_name source_file.c

That should get you started

Chris

After the 3+ hour (1GB) download of XCODE completed, I installed it and tried it. It ran and compiled and generated a file named a.out as expected.

You are right on the learning curve of the IDE...

Thank you for your help! :)

kainjow
Jun 25, 2007, 12:28 PM
I guess I can try and learn C++ also...

You can use normal C in that project, no need to use C++.

lazydog
Jun 25, 2007, 12:36 PM
You are right on the learning curve of the IDE...


I'm not sure this will help you but I find the 'All-In-One' layout the best to work in. You can change the layout from the general tab in the Xcode preferences.

b e n