Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Software - Troubleshooting and Discussion > Developer Center > Ansi C program on OS X

Ansi C program on OS X
Thread Tools
Fresh-Faced Recruit
Join Date: Apr 2002
Status: Offline
Reply With Quote
Apr 18, 2002, 05:01 PM
 
I've been a windows user for a quite a while and have recently bought a cube. I'm interested in doing some simple C programming for a computer science class, and need a way to compile ANSI C and output/input info using printf and scanf. I have installed the developer tools that come with OS X, but I don't see how to use it to write a console app. In windows, there are several tools, including Visual C++, Lcc, and others that will do this.

Do I need to buy something like codewarrior, or is there some tool that is part of BSD that I should be using? Can project builder do it?

I have very little knowledge about Mac's or Unix.

Thanks
     
Addicted to MacNN
Join Date: Mar 2000
Location: London, UK
Status: Offline
Reply With Quote
Apr 18, 2002, 05:18 PM
 
You can either use cc (which is basically gcc 2.95.2 with some apple modifications), or Project Builder, which is a GUI IDE that calls cc under the hood. If you use PB, you'll want the Standard Tool project template, otherwise just write your source files and compile them like "cc -o foo foo.c" or something.
     
Professional Poster
Join Date: Nov 2000
Location: Tasmania, Australia
Status: Offline
Reply With Quote
Apr 18, 2002, 07:35 PM
 
Project Builder will do it (& much more), but all you need for simple C programs is a text editor, and the "cc" command (on the CLI).
     
Senior User
Join Date: Apr 2003
Location: Canada GTA
Status: Offline
Reply With Quote
Sep 14, 2004, 04:19 PM
 
you mentioned just using a text editor and using the cc command? what are they and where do i input these commands? i'm required to use ANSI C in my engineering class, and i really need a good reasonably standardized substitute compiler that is ANSI C compliant.

the one the prof recommends is one from borland.com or OpenWatcom. but those are PC only since my school is rather PC oriented in the engineering dept.

please help me find a way to keep working on my mac.

thx a bunch.
Ryan
     
qyn
Dedicated MacNNer
Join Date: Dec 2000
Location: sj ca
Status: Offline
Reply With Quote
Sep 14, 2004, 08:24 PM
 
The gcc command is definitely ANSI compliant, so no worries there. For editing your files, there are many, many editors. BBEdit is the best (IMHO) but somewhat pricy. Goggling around will turn up many options. My advice is stay away from project builder/xcode. That will be way to overwhelming for your needs. Though it may be useful as an editor...

Once you write your file, then do this in the terminal:

gcc myfile.c -o myprogram

Presto!
     
Registered User
Join Date: Oct 2003
Status: Offline
Reply With Quote
Sep 14, 2004, 08:38 PM
 
Eric and Ryan,

You guys want to follow these instructions exactly:

1. Write your program using a plain text editor, like TextEdit (for now). It will teach you to pay attention to what you type, and also to appreciate advanced editor features later on. Your program text will look something like:

Code:
#include <stdio.h> int main() { printf( "Hello World!" ); return 0; }
2. Create a "dev" directory in /Users/yourname and save your program text file, naming it "main.c".

3. Open the Terminal application. This is where you build and run your programs from. Type "cd dev" to get into the dev directory, then type "gcc main.c" to compile your program.

4. When gcc is done, you'll be left with an executable along with your main.c file. It will be named a.out. Run it by typing "./a.out"

5. *Don't* use ProjectBuilder/Xcode, Borland, Codewarrior, OpenWatcom, or any other fancy programming tool right now -- they will only confuse you and give you the wrong idea about what progamming is. For now, the only tools you need are TextEdit and Terminal.

Ryan, if you have access to a Mac running OS X you should be all set. :) From the terminal app, type "gcc -v" and see what happens. If your computer tells you it can't find gcc, you either need the developer tools cd that comes with current off-the-shelf boxes of OS X, or else I think you need to download the developer tools from Apple's site. If it's a computer in a computer lab on campus, just ask one of the support folks for "gcc" -- they'll know what you mean. :)
(Last edited by johnMG; Sep 14, 2004 at 08:50 PM. )
     
Mac Elite
Join Date: Sep 2000
Location: Tempe, AZ
Status: Offline
Reply With Quote
Sep 14, 2004, 11:08 PM
 
Guys, note that if you're going to use TextEdit as your editor, you need to know that it creates .rtf files by default, not plain text files. Convert to a plain text file by choosing Make Plain Text from the Format menu.

If you try to compile an RTF file, gcc will choke hard.
Geekspiff - generating spiffdiddlee software since before you began paying attention.
     
Mac Elite
Join Date: Oct 1999
Location: San Jose, Ca
Status: Offline
Reply With Quote
Sep 14, 2004, 11:09 PM
 
As for the text editor... I would recommend SubEthaEdit as it is a pure text editor (you don't have to worry about accidentally saving your text as RTF), and has color coding for a lot of languages (including C).
     
Mac Elite
Join Date: Sep 2000
Location: Tempe, AZ
Status: Offline
Reply With Quote
Sep 15, 2004, 06:06 AM
 
Originally posted by larkost:
As for the text editor... I would recommend SubEthaEdit as it is a pure text editor (you don't have to worry about accidentally saving your text as RTF), and has color coding for a lot of languages (including C).
Yeah, much better than using TextEdit. I think xCode is fine, too.
Geekspiff - generating spiffdiddlee software since before you began paying attention.
     
Forum Regular
Join Date: Jan 2001
Status: Offline
Reply With Quote
Sep 15, 2004, 01:23 PM
 
you migth want to use the -Wall -pedantic -ansi flags on gcc.
     
Senior User
Join Date: Apr 2003
Location: Canada GTA
Status: Offline
Reply With Quote
Sep 15, 2004, 03:52 PM
 
this is great. thx guys. i'll try out each and every suggestions. i've tried compiling and testing a sample c file with the command "gcc -o test filename.c" so far, and then running it with "./test" is this the same as "gcc myfile.c -o myprogram" that was mentioned by qyn?

a friend of mine has already recommended against using XCode because of the more friendly error warnings, etc. and we agreed that it might be a good idea to learn at the most basic level - especially since i'll have to use a DOS Borland C compiler for tests in class, and will have to get used to the less friendly, english-like, error warnings. or so he said.

with this in mind, i'm guessing using barebones text editor and terminal for compiling is the best way to mimic the DOS compiler?
Ryan
     
Professional Poster
Join Date: Nov 2000
Location: Tasmania, Australia
Status: Offline
Reply With Quote
Sep 15, 2004, 05:58 PM
 
Originally posted by Ryan1524:
this is great. thx guys. i'll try out each and every suggestions. i've tried compiling and testing a sample c file with the command "gcc -o test filename.c" so far, and then running it with "./test" is this the same as "gcc myfile.c -o myprogram" that was mentioned by qyn?

a friend of mine has already recommended against using XCode because of the more friendly error warnings, etc. and we agreed that it might be a good idea to learn at the most basic level - especially since i'll have to use a DOS Borland C compiler for tests in class, and will have to get used to the less friendly, english-like, error warnings. or so he said.

with this in mind, i'm guessing using barebones text editor and terminal for compiling is the best way to mimic the DOS compiler?
XCode could still be a useful editor for you. It can help you with syntax, without doing it all for you, in a sense training you (using colour coding, and indenting).

However, don't use XCode to compile while learning. Use the "gcc" command in the Terminal. Use XCode ONLY as an editor for starters.
     
Senior User
Join Date: Apr 2003
Location: Canada GTA
Status: Offline
Reply With Quote
Sep 15, 2004, 06:06 PM
 
funny thing about xcode is that i don't know how to compile with it. granted i've only tried it once or twice, but the Compile button in the Build menu is blanked out.

so with any text editor and the terminal, ANSI C coding will be no probllem for me right? awesome!!


went to the second lecture today went over the history off computing. he didn't mentioon anything about the mac being the first PC and also about being the first mainstream GUI. kinda disappointed. when i asked about mac coding, he said that he was actually thinking about that and wondered what the mac people would do. well...at least he's not adamantly anti-mac. now i need to find a TA that uses a mac. that'd be awesome.

anny more advice regarding coding ANSI C in mac to substitute Borland and Open Watcom's stuff is very welcome. thx again guys.
Ryan
     
Clinically Insane
Join Date: Oct 2001
Location: San Diego, CA, USA
Status: Offline
Reply With Quote
Sep 15, 2004, 07:23 PM
 
Originally posted by Ryan1524:
funny thing about xcode is that i don't know how to compile with it. granted i've only tried it once or twice, but the Compile button in the Build menu is blanked out.
Xcode builds projects. It doesn't compile individual files. A file has to be in a project in order to compile it. You can create a single-file "Command-Line Tool"-type project and build that if you just want a single-file program.
Chuck
___
"Instead of either 'multi-talented' or 'multitalented' use 'bisexual'."
     
Registered User
Join Date: Oct 2003
Status: Offline
Reply With Quote
Sep 15, 2004, 08:12 PM
 
smeger, thanks for catching that. I told TextEdit to use plain text files the first time I ran it and haven't thought about it since. :)

Ryan, yes, stick with any text editor and the terminal and you're golden. With those simple tools in-hand, you'll be able to program in *any* normal environment (as in, Mac OS X, GNU/Linux, Solaris, or any other Unix, not to mention the BSD's along with all the alternative OS's out there).

After a week or two of using TextEdit, you'll be in better shape to choose a nice editor to move on to. At first TextEdit will seem nice, then after some coding you'll start to ask yourself, "why can't this just autocomplete as I type it? Why can't keywords be colored different than the rest of the text?" And so on. Then you'll be ready to try some real text editors. :)

Oh, and yes: "gcc -o foo foo.c" is the same as "gcc foo.c -o foo"

Little by little, you'll learn a few more gcc options that you'll use more and more (like -c, -g, -ansi, -pedantic-errors, -l, -L, and -I).
     
Professional Poster
Join Date: Nov 2000
Location: Tasmania, Australia
Status: Offline
Reply With Quote
Sep 15, 2004, 09:58 PM
 
Originally posted by johnMG:
smeger, thanks for catching that. I told TextEdit to use plain text files the first time I ran it and haven't thought about it since.

Ryan, yes, stick with any text editor and the terminal and you're golden. With those simple tools in-hand, you'll be able to program in *any* normal environment (as in, Mac OS X, GNU/Linux, Solaris, or any other Unix, not to mention the BSD's along with all the alternative OS's out there).

After a week or two of using TextEdit, you'll be in better shape to choose a nice editor to move on to. At first TextEdit will seem nice, then after some coding you'll start to ask yourself, "why can't this just autocomplete as I type it? Why can't keywords be colored different than the rest of the text?" And so on. Then you'll be ready to try some real text editors.

Oh, and yes: "gcc -o foo foo.c" is the same as "gcc foo.c -o foo"

Little by little, you'll learn a few more gcc options that you'll use more and more (like -c, -g, -ansi, -pedantic-errors, -l, -L, and -I).
Although if you REALLY wanted to be able to work on ANY unix-like system, you should use "vi" not TextEdit. "vi" is the only editor that you can guarentee is available on a unix system (the exceptions would be very few). "pico" is on most these days, I guess, and "ed" is of course still out there.

Not that I'm recommending you actually use any of these, but in some situations (eg, where I work), "vi" is the ONLY text editor available in some circumstances (eg, remote console). Not that I'd be coding C applications in such circumstances, but "vi" is still quicker at many complex tasks than any GUI editor (once you get the hang of it).
     
Senior User
Join Date: Apr 2003
Location: Canada GTA
Status: Offline
Reply With Quote
Sep 15, 2004, 11:36 PM
 
how bout ANSI C compatibility with the windows Borland compiler? should be fine, right? by that i mean whatever i code with textedit and terminal should also work on the windows borland C compiler. since that's what i'll have to use for tests. i'm basically concerned with getting myself used to a workflow that will allow me to write codes that can run on the windows compiler as well as it does on my mac.
Ryan
     
Clinically Insane
Join Date: Oct 2001
Location: San Diego, CA, USA
Status: Offline
Reply With Quote
Sep 16, 2004, 12:20 AM
 
GCC has some of the best ANSI C compatibility out there. You can check out the Web site, and it will show you how standards-compliant it is and how to change the way it enforces the standards.
Chuck
___
"Instead of either 'multi-talented' or 'multitalented' use 'bisexual'."
     
Registered User
Join Date: Oct 2003
Status: Offline
Reply With Quote
Sep 16, 2004, 09:03 PM
 
Ryan -- yes. Do all your *own* stuff with gcc.

When you're somewhere else that doesn't have gcc but has borland or comeau or something else, your code will compile just fine and dandy.

gcc offers some non-standard extensions that you might make use of later on when you're more experienced, but for now, just always use standard ANSI/ISO C -- and compile your code with -ansi and -pedantic-errors, like so:

Code:
gcc -ansi -pedantic-errors -o someApp main.c
---J
     
   
Thread Tools
Forum Links
Forum Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Top
Privacy Policy
All times are GMT -5. The time now is 09:48 AM.
All contents of these forums © 1995-2011 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.7 © 2000-2011, Jelsoft Enterprises Ltd., Content Relevant URLs by vBSEO 3.3.2