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

kulimer

macrumors 6502
Original poster
Aug 30, 2011
330
2
I am pretty new to Mac OS X, I want to program C code on Mac, what software/program should I use?

I was told by my prof to use Cygwin in Windows, but I have a Mac, what would be the equivalent? Or can I just run it in terminal?
 
Last edited:
The IDE I think most people will recommend is xcode. That is where I started to learn C on and most books you find that teach C on the Mac like "Learn C on the Mac" which I learned from was working within xcode.
 
Using an IDE (ie. XCode) is IMO a horrible way to learn to start programming.

You miss out on how compilers and build tools work which IMO is an important part to being a good systems programmer.

If your professor is using cygwin, then the equivalent would be to install the Command Line Tools for XCode(you don't actually have to install XCode to use it) and use gcc from the Terminal.
 
Last edited:
Using an IDE (ie. XCode) is IMO a horrible way to learn to start programming.

You miss out on how compilers and build tools work which IMO is an important part to being a good systems programmer.

If your professor is using cygwin, then the equivalent would be to install the Command Line Tools for XCode(you don't actually have to install XCode to use it) and use gcc from the Terminal.

I agree with this - IDEs add an unnecessary level of complication for learning the language. If your professor is pointing you toward cygwin, it means the standard you'll be learning from is likely a "text editor + complier". Follow AzN's directions and use gcc from the terminal to compile your code.

This, incidentally, is also the advice from 'Learn C the Hard Way'. It has the benefit of leaving you equipped to handle programming whether or not XCode is installed, or on another OS.
 
Agree with gcc way! I am 13 years old and i am learning objective c and i only use command line tools.
 
Sorry about the late reply. I have XCode installed, but did not know how to use it for C programming, only to make apps.

How does gcc work? I am always wanted to learn it, seems like it's a really useful tool. Could anyone tell me how to gcc a C code on Lion? Thanks everyone!
 
Sorry about the late reply. I have XCode installed, but did not know how to use it for C programming, only to make apps.

How does gcc work? I am always wanted to learn it, seems like it's a really useful tool. Could anyone tell me how to gcc a C code on Lion? Thanks everyone!

Doing a quick search on google, gets me this:
http://www.informit.com/articles/article.aspx?p=1315356

Follow the directions under the one talking about Terminal to get an example of using gcc for ObjC. It's basically a little bit more complex than using gcc for C, where you'd be using a .c file instead of .m.

You'd best get yourself a basic C book to learn from if you want to proceed from that.
 
Sorry about the late reply. I have XCode installed, but did not know how to use it for C programming, only to make apps.

How does gcc work? I am always wanted to learn it, seems like it's a really useful tool. Could anyone tell me how to gcc a C code on Lion? Thanks everyone!

See if you can find these books:

elUOS.jpg


rVWIS.jpg
 
3rd year Comp Sci major here. Not to discourage you from using the Terminal, you really should give it a shot, but you can also check out CodeRunner. It's a cheap, no-frills program that allows you to write Java, C, C++, Objective-C, etc.

When I first started writing in Java, I used Coda 2 as a text editor and the Terminal. It's the best way to start learning. As a matter of fact, in my Systems Programming class, all we do is use the Terminal.

However, I've used CodeRunner for small C programs and if you are reading from a book, CodeRunner is perfect for that. I've also used Eclipse... and while people swear by it, I really couldn't get into it. I used it for like a semester. On the other hand, I've used Xcode and I think it's too bloated for someone beginning.

It's EXTREMELY important (but maybe not necessary) to learn command line tools, really helpful.

----------

As for the actual book, you can use "Learn C the Hard Way" but hopefully you are just using the K&R C book. They created C and the book is extremely applicable, even today.

It discusses compiling from the command line. If you do decide to use the Terminal, you might want to look into using vi or emacs as well.
 
As an Amazon Associate, MacRumors earns a commission from qualifying purchases made through links in this post.
The "learn C on the Mac" was the one I learned from, a great book. But unfortunately they had a different writer for the second Objective C book and was not nearly as good. I stopped reading that one 1/2 way through.
 
Thanks for the recommendation. I tried learning C on the mac and failed to even get a compiler going that worked with the book I was reading. xCode just gave me errors!

I gave up on Apress for programming books. Ive gotten so many that had errata in them and which wasn't necessarily noted on the website.
 
As an Amazon Associate, MacRumors earns a commission from qualifying purchases made through links in this post.
Agree with the recommendations for Kochan's book. Great balance between background and experimentation. Starts out basic and slow, simultaneously building your confidence in basic programming logic, structures, then objects, objective-C, and finally app building. Great read. Don't be afraid of X-Code. It's the best IDE I've ever used. I couldn't imagine using a basic text editor for programming. I've used an IDE with syntax highlighting since Turbo Pascal 7.0 back in 1993 . . .
 
Doing a quick search on google, gets me this:
http://www.informit.com/articles/article.aspx?p=1315356

Follow the directions under the one talking about Terminal to get an example of using gcc for ObjC. It's basically a little bit more complex than using gcc for C, where you'd be using a .c file instead of .m.

You'd best get yourself a basic C book to learn from if you want to proceed from that.

Thanks that was really helpful, I wonder why I didn't even see this.

I was able to compile, but got an error saying "gsl/gsl_math.h" not found. I haven't used cygwin, but heard there's no problem. Does Mac not have all necessary libraries equipped?
 
I am pretty new to Mac OS X, I want to program C code on Mac, what software/program should I use?

I was told by my prof to use Cygwin in Windows, but I have a Mac, what would be the equivalent? Or can I just run it in terminal?

If you install the developer tools, you will get gcc and clang, which are the equivalent of cygwin.

You can then write code with the text editor of your choice, and compile with gcc. I'd suggest textwrangler, as it does C syntax highlighting amongst other languages, which will give you the most helpful IDE feature without forcing you to learn how to drive the IDE.

If your professor is recommending cygin, you'll just cause problems trying to use xcode.


As above, if you try to start out in xcode, you'll waste more time trying to learn the IDE than actually trying to learn to code.
 
Last edited:
...I've used CodeRunner for small C programs and if you are reading from a book, CodeRunner is perfect for that. I've also used Eclipse... and while people swear by it, I really couldn't get into it. I used it for like a semester. On the other hand, I've used Xcode and I think it's too bloated for someone beginning...

I went over to CodeRunner website, the customizable template looks good. But my code are pretty large, and this thing is not free.

Anyways, I didn't feel XCode was that bad. I use it as a text editor, then go to terminal and compile.

----------

If you install the developer tools, you will get gcc and clang, which are the equivalent of cygwin.

You can then write code with the text editor of your choice, and compile with gcc. I'd suggest textwrangler, as it does C syntax highlighting amongst other languages, which will give you the most helpful IDE feature without forcing you to learn how to drive the IDE.

Yes, just installed it. Looks pretty good so far. For compiling on Cygwin I remember,

1. save hello.c in the same folder as Cygwin.
2. open up Cygwin, type gcc hello.c
3. done!

Since I don't have Cygwin here, where should I save the file for compiling, or just anywhere?
 
Yes, just installed it. Looks pretty good so far. For compiling on Cygwin I remember,

1. save hello.c in the same folder as Cygwin.
2. open up Cygwin, type gcc hello.c
3. done!

Since I don't have Cygwin here, where should I save the file for compiling, or just anywhere?


You don't need cygwin, because the tools that are part of that for Windows are installed as part of the Mac dev tools. Cygwin is just a wrapper (sort of like an emulator - yes, techies its not really true but for lack of a better explanation...) to allow the Unix tools (gcc is a unix application) to run on Windows. Because OS X is Unix, there's no need for cygwin.

All you need to do is open a terminal window, and it should default to your home directory.

If you save the file in your home directory, just type

gcc hello.c

and you're good.


You may want to learn how basic command line navigation works in the terminal (so you can save things in folders, navigate to them in the terminal and run gcc there, etc), but its as simple as that.
 
...You may want to learn how basic command line navigation works in the terminal (so you can save things in folders, navigate to them in the terminal and run gcc there, etc), but its as simple as that.

Thanks for the quick response, I asked on post #16,

"I was able to compile, but got an error saying "gsl/gsl_math.h" not found. I haven't used cygwin, but heard there's no problem. Does Mac not have all necessary libraries equipped?"

Compiler works, yay. I am mainly stuck with the library error now. Am I actually miss this library? Or am I missing the right command? I heard on Cygwin, this comes with it.
 
Thanks for the quick response, I asked on post #16,

"I was able to compile, but got an error saying "gsl/gsl_math.h" not found. I haven't used cygwin, but heard there's no problem. Does Mac not have all necessary libraries equipped?"

Compiler works, yay. I am mainly stuck with the library error now. Am I actually miss this library? Or am I missing the right command? I heard on Cygwin, this comes with it.

GNU Scientific Library will need to be compiled separately and installed before your code will compile. It's not part of the Xcode, and I actually don't recall it being part of cygwin either. But then again, I haven't used cygwin in years so I could be wrong.
 
I was able to compile, but got an error saying "gsl/gsl_math.h" not found. I haven't used cygwin, but heard there's no problem. Does Mac not have all necessary libraries equipped?

Probably not. I see that it is available from macports, though:

Code:
$ port provides /opt/local/include/gsl/gsl_math.h 
/opt/local/include/gsl/gsl_math.h is provided by: gsl

$ port info gsl
gsl @1.15, Revision 1 (math, science)
Variants:             doc, gcc43, gcc44, gcc45, gcc46, gcc47, optimize, universal

Description:          The GNU Scientific Library (GSL) is a numerical library for C
                      and C++ programmers. It is free software under the GNU General
                      Public License. The library provides a wide range of
                      mathematical routines such as random number generators, special
                      functions and least-squares fitting. There are over 1000
                      functions in total.
Homepage:             http://www.gnu.org/software/gsl

Build Dependencies:   texinfo
Conflicts with:       gsl-devel
Platforms:            darwin
License:              GPL-3+
Maintainers:

If you install macports, run:
Code:
sudo port install gsl

The gsl man page is pretty sparse, but you might want to try "info gsl".

Here's the example program from the info documentation:
Code:
2.1 An Example Program
======================

The following short program demonstrates the use of the library by
computing the value of the Bessel function J_0(x) for x=5,

     #include <stdio.h>
     #include <gsl/gsl_sf_bessel.h>

     int
     main (void)
     {
       double x = 5.0;
       double y = gsl_sf_bessel_J0 (x);
       printf ("J0(%g) = %.18e\n", x, y);
       return 0;
     }

The output is shown below, and should be correct to double-precision
accuracy,(1)

     J0(5) = -1.775967713143382920e-01

The steps needed to compile this program are described in the following
sections.

   ---------- Footnotes ----------

   (1) The last few digits may vary slightly depending on the compiler
and platform used--this is normal.

Let's try to compile it.

Code:
$ cat > bessel.c << EOF
>      #include <stdio.h>
>      #include <gsl/gsl_sf_bessel.h>
> 
>      int
>      main (void)
>      {
>        double x = 5.0;
>        double y = gsl_sf_bessel_J0 (x);
>        printf ("J0(%g) = %.18e\n", x, y);
>        return 0;
>      }
> EOF
$ gcc -Wall -I/opt/local/include -L/opt/local/lib -lgsl -o bessel bessel.c
$ ./bessel 
J0(5) = -1.775967713143382642e-01
$

edit:
I heard on Cygwin, this comes with it.
I'd be surprised if it did. Is your professor handing out a preconfigured cygwin environment, somehow?

Also, as far as text editors go, I'd just like to mention that "vimtutor" comes with OS X.
 
Last edited:
I write code with Xcode and compile it with GCC.

Download xcode, prefences, find additional downloads and download command line tools.

From there write the code, save it, then open terminal and type gcc filenameofyourcode

it'll compile (hopefully!) then you should be able to run it in terminal
 
I write code with Xcode and compile it with GCC.

Download xcode, prefences, find additional downloads and download command line tools.

From there write the code, save it, then open terminal and type gcc filenameofyourcode

it'll compile (hopefully!) then you should be able to run it in terminal

Xcode has a command line project - that way you can run and debug it from Xcode
 
If you install macports, run:
Code:
sudo port install gsl
Thanks for your example. I am following what you did in terminal, does it look like it is installed?
Code:
nbp-152-194:~ Colin$ sudo port install gsl
Password:
--->  Fetching archive for gsl
--->  Attempting to fetch gsl-1.15_2.darwin_11.x86_64.tbz2 from http://packages.macports.org/gsl
--->  Attempting to fetch gsl-1.15_2.darwin_11.x86_64.tbz2.rmd160 from http://packages.macports.org/gsl
--->  Installing gsl @1.15_2
--->  Deactivating gsl @1.15_1
--->  Cleaning gsl
--->  Activating gsl @1.15_2
--->  Cleaning gsl
--->  Updating database of binaries: 100.0%
--->  Scanning binaries for linking errors: 100.0%
--->  No broken files found.
nbp-152-194:~ Mike$
But I still get an error within the editor saying file not found. I am attaching a screenshot what I am seeing in XCode.


I think gsl is installed, when I try "info gsl", there is the manual.
But my question is, where is gsl inside my computer? I guess it comes with Mac Port? So, do I just go into Mac Port copy and paste the file into my code?

So, do I have to just set the directory? I am kind of lost.
 

Attachments

  • Screen Shot 2012-09-18 at 2.31.36 PM.png
    Screen Shot 2012-09-18 at 2.31.36 PM.png
    291.4 KB · Views: 510
Last edited:
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.