What software to use to program C on Mac?

Discussion in 'Mac Programming' started by kulimer, Sep 7, 2012.

  1. kulimer, Sep 7, 2012
    Last edited: Sep 7, 2012

    kulimer macrumors 6502

    Joined:
    Aug 30, 2011
    #1
    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?
     
  2. larswik macrumors 68000

    Joined:
    Sep 8, 2006
    #2
    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.
     
  3. AzN1337c0d3r, Sep 7, 2012
    Last edited: Sep 7, 2012

    AzN1337c0d3r macrumors 6502

    Joined:
    Sep 13, 2010
    #3
    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.
     
  4. FluJunkie macrumors 6502a

    Joined:
    Jul 17, 2007
    #4
    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.
     
  5. madsilencerr macrumors member

    Joined:
    Aug 27, 2012
    #5
    Agree with gcc way! I am 13 years old and i am learning objective c and i only use command line tools.
     
  6. kulimer thread starter macrumors 6502

    Joined:
    Aug 30, 2011
    #6
    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!
     
  7. hchung macrumors 6502a

    Joined:
    Oct 2, 2008
    #7
    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.
     
  8. robvas macrumors 68020

    Joined:
    Mar 29, 2009
    Location:
    USA
    #8
    See if you can find these books:

    [​IMG]

    [​IMG]
     
  9. mmendoza27 macrumors 6502

    Joined:
    Oct 18, 2007
    #9
    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.
     
  10. MorphingDragon macrumors 603

    MorphingDragon

    Joined:
    Mar 27, 2009
    Location:
    The World Inbetween
    #10
  11. NathanCH macrumors 65816

    NathanCH

    Joined:
    Oct 5, 2007
    Location:
    Stockholm, Sweden
    #11
  12. larswik macrumors 68000

    Joined:
    Sep 8, 2006
    #12
    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.
     
  13. MorphingDragon macrumors 603

    MorphingDragon

    Joined:
    Mar 27, 2009
    Location:
    The World Inbetween
    #13
    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.
     
  14. robvas macrumors 68020

    Joined:
    Mar 29, 2009
    Location:
    USA
    #14
  15. iMacFarlane macrumors 65816

    iMacFarlane

    Joined:
    Apr 5, 2012
    Location:
    Adrift in a sea of possibilities
    #15
    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 . . .
     
  16. kulimer thread starter macrumors 6502

    Joined:
    Aug 30, 2011
    #16
    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?
     
  17. throAU, Sep 13, 2012
    Last edited: Sep 13, 2012

    throAU macrumors 601

    throAU

    Joined:
    Feb 13, 2012
    Location:
    Perth, Western Australia
    #17
    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.
     
  18. kulimer thread starter macrumors 6502

    Joined:
    Aug 30, 2011
    #18
    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.

    ----------

    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?
     
  19. throAU macrumors 601

    throAU

    Joined:
    Feb 13, 2012
    Location:
    Perth, Western Australia
    #19

    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.
     
  20. kulimer thread starter macrumors 6502

    Joined:
    Aug 30, 2011
    #20
    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.
     
  21. hchung macrumors 6502a

    Joined:
    Oct 2, 2008
    #21
    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.
     
  22. pitaya, Sep 14, 2012
    Last edited: Sep 14, 2012

    pitaya macrumors member

    Joined:
    Jun 17, 2012
    #22
    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'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.
     
  23. divideby macrumors member

    Joined:
    Sep 16, 2009
    #23
    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
     
  24. robvas macrumors 68020

    Joined:
    Mar 29, 2009
    Location:
    USA
    #24
    Xcode has a command line project - that way you can run and debug it from Xcode
     
  25. kulimer, Sep 18, 2012
    Last edited: Sep 18, 2012

    kulimer thread starter macrumors 6502

    Joined:
    Aug 30, 2011
    #25
    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.
     

    Attached Files:

Share This Page