Learning C, but overwhelmed by the Carbon API
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.
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
sorry.
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.
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 <iostream.h>
void main()
{
cout>>"Hello World">>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>
<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?
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.
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.
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>
<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.
hmmmmmm interesting <img src="graemlins/bugeye.gif" border="0" alt="[Skeptical]" />
Macintosh Programmer's Workshop is a development environment for pre OS X and is available for download from Apple.
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.
<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?