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

AAArroyo

macrumors newbie
Original poster
Jun 25, 2007
4
0
Gainesville, Florida
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

Moderator emeritus
Jul 24, 2002
25,611
893
Harrogate
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

Code:
gcc <filename>
 

kainjow

Moderator emeritus
Jun 15, 2000
7,958
7
First create a free ADC account at 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

macrumors regular
Jul 5, 2005
158
0
London, UK
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

macrumors newbie
Original poster
Jun 25, 2007
4
0
Gainesville, Florida
Thank You

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

Code:
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

Moderator emeritus
Jul 24, 2002
25,611
893
Harrogate
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

macrumors newbie
Original poster
Jun 25, 2007
4
0
Gainesville, Florida
Thank You

First create a free ADC account at 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

macrumors newbie
Original poster
Jun 25, 2007
4
0
Gainesville, Florida
Thank You

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! :)
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.