Learning C, but overwhelmed by the Carbon API

Posted:
in Genius Bar edited January 2014
I decided to finally take a serious crack at programming in C. I purchased Ivor Horton's Beginning C and downloaded Project Builder.



Now, this is where the trouble started.



The examples given in his book, and others I've read are obviously not intended with Mac programmers in mind. A simple "Hello World" app is like, 6 lines in books such as these. In PB, it's like 150. With tons and tons of Carbon calls that I can only begin to digest and loads of C syntax I haven't learned yet.



So, out of total frustration, I went to emacs. Dumped the following into the buffer and did the old C-x C-s.



[quote]#include <stdio.h>



void main()



{



printf("Hello World");



}<hr></blockquote>



then did a gcc -c foo.c



then a gcc -o myapp foo.c foo.o



And gcc spit out:

[quote]OK? cc foo.c foo.o? yes

foo.c: In function `main':

foo.c:5: warning: return type of `main' is not `int'

/usr/bin/ld: multiple definitions of symbol _main

/var/tmp/ccdLbaaa.o definition of _main in section (__TEXT,__text)

foo.o definition of _main in section (__TEXT,__text)<hr></blockquote>



I cannot ****ing win, folks.



Any suggestions for how I should proceed from here. I would like to take a class, but that's not possible right now. I wish there was a simple, straightforward introduction on programming C in OS X that assumes you have never done anything more than a few interesting BASIC apps many, many years in the past.

Comments

  • Reply 1 of 16
    i would love to help you, but when i was in college, programming was the only thing that i could not stand. i took one class then stopped taking any future courses and changed my major accordingly. hopefully one of the programming savvy members will be able to help you out.



    sorry.
  • Reply 2 of 16
    I'm in the same boat your in. Cant take a class so I'll teach my self.



    The way in invoke the compiler is wrong. The correct useage is:



    % gcc source.c -o output



    and try making the main function an int. To do this type:



    int main()

    {

    ????

    return 0;

    }



    I read in my C++ book that main must be an int.



    As far as the Carbon API goes, try just using pico or emacs to write single source files. I wouldn't use project builder or any API's yet untill i got experianced.
  • Reply 3 of 16
    kecksykecksy Posts: 1,002member
    I know the main() function can be void in DOS C++, not sure about GCC and Mac OS X. If you want to learn C on a Mac, stay away from Carbon.



    I consider myself fairly fluent in C, but Carbon is hard for me to use. It's not that I can't read the syntax, it's that there are just too many API calls I have to make inorder to write anything useful. It's a real bitch memorizing them all.



    Seriously if you want to learn C, I wouldn't do it on a Mac or in Windows. I'd do it in DOS or Unix. In DOS you can write a function like...



    #include &lt;iostream.h&gt;



    void main()

    {

    cout&gt;&gt;"Hello World"&gt;&gt;endl;

    {



    ...and have it do something. Once you become confortable with C in DOS, I would recomend learning Cocoa. Cocoa's API is a little less daunting than Carbon's. Oreilly has published some very good books on programing for OS X. I'd check them out. <a href="http://www.oreillynet.com"; target="_blank">http://www.oreillynet.com</a>;
  • Reply 4 of 16
    ast3r3xast3r3x Posts: 5,012member
    is there a visual environment to program C++ in OS X? Something like Borland...thats what we use in school. I just need a c++ compiler for os x...any suggestions?
  • Reply 5 of 16
    [quote]Originally posted by ast3r3x:

    <strong>is there a visual environment to program C++ in OS X? Something like Borland...thats what we use in school. I just need a c++ compiler for os x...any suggestions?</strong><hr></blockquote>



    Dont borland make a C compiler for OSX, shite I cant think of the name??? I was using one, as I was doing horrid Java programming and I think it did C?
  • Reply 6 of 16
    To get a compiler all you have to do is install the dev tools from the cd that came with mac os x. You get the gcc compiler which does just about any kind of C.
  • Reply 7 of 16
    a few things have to be addressed here. first, in 10.0 i think apple renamed gcc to cc, so thats why you got the shell try to rename the line. the '-c' option means "don't link". '-o &lt;filename&gt;' means output to the file named &lt;filename&gt;. if you have only 1 source file, you can skip the non-link line, and just do `gcc &lt;source.*&gt; -o myapp`. the `gcc -c &lt;source.*&gt;` makes an object file (source.o) which has the compiled (binary) version of "source.*". for full list of options and uses, do 'gcc --help', or check out gcc.gnu.org.



    main can be just about anything. the regular main is "int main(int argc, char* argv[])". you can also do "void main(int argc, char* argv[])" and "void main(void)". it was only a warning. if you put in the int, and char** then you get the command-line arguments (argc is the number, and argv[x] is the xth arg.)



    the multiply definition you got was because on your second gcc line, you not only recompiled the source, but also linked the source you already compiled. had you just done `gcc -o foo.o foo.c;gcc -o myapp foo.o` it'd link w/o problems. or, `gcc -o myapp foo.c`.



    now, someone mentioned using iostream.h, and cout; that's not C. its c++. while it doesn't matter greatly, it means you have to mv your source from foo.c, to foo.cpp. gcc can take c, c++, objective-c, fortran and a couple of other languages, but requires the correct file extension. and for c++ source your supposed to use g++, tho i don't think it really matters.



    and, ast3r3x, for a visual environment (i assume you mean, an IDE) there's Project Builder. it's only for osx (.0-.2 so far) and its wonderful. it uses gcc as the actual compiler, and gdb as the debugger, but it integrates into a cocoa windowing environment. the version for jaguar is noticeably better.
  • Reply 8 of 16
    kickahakickaha Posts: 8,760member
    [quote]now, someone mentioned using iostream.h, and cout; that's not C. its c++. while it doesn't matter greatly, it means you have to mv your source from foo.c, to foo.cpp. gcc can take c, c++, objective-c, fortran and a couple of other languages, but requires the correct file extension. and for c++ source your supposed to use g++, tho i don't think it really matters.<hr></blockquote>



    Actually, it does.



    gcc and g++ run under different assumptions of linking, etc.



    If it's C, use gcc. If it's C++, use g++ to produce executables.



    Gonzo, you can most certainly use PB to create *exactly* the same Hello World app you tried to do at the command line. Just create a new project for a Standard Tool. It's now set up for C, no GUI, no Carbon, Cocoa, or anything. When it's done building, just run it at the command line. (Or within PB's debugger.) In fact, try it... and you'll see that the default main it gives you *IS* Hello World. Six lines, plus a comment. Hit 'Build and Run' under the Build menu, Cmd-R, or the Hammer and Console icon (third from left in the toolbar). Voila. Your first C program.



    When you feel comfortable with your l33t hacking skills, you can learn more about running it at the command line. It's not hard, but having to learn it all at once can be overwhelming.
  • Reply 9 of 16
    First of all, I must profusely thank all of you for your replies. It has helped me immensely.



    I used emacs again, wrote a HelloWorld with 'int' in place of 'void', along with a 'return 0;' and it ran perfectly.



    Then, I skimmed my book for about a minute, then I wrote an app that asked me my weekly paycheck, stored the amount, and then deducted the amount I pay my mother in law for rent. (don't say a word )



    emacs and gcc have been invaluble. I've had no more problems with either of them. My year or so of experience with running Linux really helped.



    Now I need an idea for a simple application I can write. Any ideas?



    [ 09-29-2002: Message edited by: DoctorGonzo ]</p>
  • Reply 10 of 16
    [quote]Originally posted by DoctorGonzo:

    <strong>Now I need an idea for a simple application I can write. Any ideas?

    </strong><hr></blockquote>



    In my C++ class we did a "Guess the Number" game, "Nihm" game (person to pick up last of 7 sticks loses) and a Hangman game. All were fairly simple to code.
  • Reply 11 of 16
    ast3r3xast3r3x Posts: 5,012member
    doctor gonzo....i thought you died off with hunter s.





    hmmmmmm interesting <img src="graemlins/bugeye.gif" border="0" alt="[Skeptical]" />
  • Reply 12 of 16
    stoostoo Posts: 1,490member
    main doesn't have to return anything: it's a warning, not an error.



    Macintosh Programmer's Workshop is a development environment for pre OS X and is available for download from Apple.
  • Reply 13 of 16
    [quote]



    A simple "Hello World" app is like, 6 lines in books such as these. In PB, it's like 150. With tons and tons of Carbon calls that I can only begin to digest and loads of C syntax I haven't learned yet.



    <hr></blockquote>



    That's why you should use Cocoa-it's much better.Carbon is a hack designed to port legacy code-it's a waste of time to learn unless you have to.
  • Reply 14 of 16
    C++ is fun stuff! I wanna learn Cocoa programming though. Anywayz, is there a C++ app that I can run natively in OS X? If not I'm forced with Virtual Studio 6.0 in VPC.
  • Reply 15 of 16
    whisperwhisper Posts: 735member
    [quote]Originally posted by TigerWoods99:

    <strong>C++ is fun stuff! I wanna learn Cocoa programming though. Anywayz, is there a C++ app that I can run natively in OS X? If not I'm forced with Virtual Studio 6.0 in VPC.</strong><hr></blockquote>



    What's wrong with Project Builder?
  • Reply 16 of 16
    Errr...see my other thread...
Sign In or Register to comment.