PDA

View Full Version : Mac OS X Programming



Exile
01-25-2005, 06:30 AM
I recently recieved a Mac, complete with OS X. I was curious about programming on it. From all the reading I've done on it it seems the two C API sets are Carbon and Cocoa.

Cocoa uses something called Objective-C, which is Apple's enhanced C language, not sure why they didn't use C++ since its already pretty popular and includes the object oriented stuff that was included in Objective-C. There may be more there, I'm not keen on learning another language (particularly a proprietary one) for a platform I'm not sure if I'm going to keep using.

Carbon is a straight C API. However, it appears that its an intermediary API for those converting from the old Mac OS API's. Which kinda sounds like Apple will keep it around until everyone is up to date then drop support, thats just speculation by me though and I've been labeled cynical before.

So whats the deal? Anyone know if OS X has a C or C++ API that is guarenteed to be supported in the forseeable future?

sand_man
01-25-2005, 06:50 AM
Are you looking to make GUI apps?

Exile
01-25-2005, 07:28 AM
Thats definately on the list. Most of the things I've programmed of late were various utilities and the like, so I have a requirement for threads, sockets and file/directory access as well.

Rouss
01-25-2005, 08:13 AM
Cocoa uses something called Objective-C, which is Apple's enhanced C language, not sure why they didn't use C++ since its already pretty popular and includes the object oriented stuff that was included in Objective-C.

Unlike C++, Obj C really is an extension to C. An Obj C program is really just a C program, using special C headers to allow you to use the Obj C object designs.

I've done a few projects for class in Obj C and personally I prefer it to C++. I wish it was used more than it is.

Also, I have a few friends that program Cocoa apps. They seem to enjoy it and think it is worthwhile, although I've never heard them mention Carbon.

Exile
01-25-2005, 08:41 AM
Unlike C++, Obj C really is an extension to C. An Obj C program is really just a C program, using special C headers to allow you to use the Obj C object designs.

I'll admit to not researching Objective-C fully before coming to that conclusion, so I'm all set to be proven wrong.

These headers, is it like including iostrem where it allows non-standard lookging function calls like cin and cout? Or do they simply add additional functions?

Since Objective-C is object oriented I imagine they have to be adding more than just additional functions.

I've also heard some talk about Objective-C++ where its Objective-C but also lets you use all of C++ as well. So I guess Mac does permit C++ programming.

I guess the question then becomes how different really is Objective-C from ANSI C? If its just a a few headers, it hardly seems worth renaming it.


Also, I have a few friends that program Cocoa apps. They seem to enjoy it and think it is worthwhile, although I've never heard them mention Carbon.

Carbon apparently is just a transitional API, intended for porting old Mac OS apps to OS X. I don't know if it was intended for any use beyond that. It certainly hasn't gotten much press so at the very least Apple isn't pushing the subject at all if that says anything.

FillYourBrain
01-25-2005, 08:49 AM
mac definitely allows c++ through g++ and X programming. Granted it isn't old school mac programming, but they've gone in the bsd direction for a reason.

Perspective
01-25-2005, 09:43 AM
>>Which kinda sounds like Apple will keep it around until everyone is up to date then drop support,

<my-opinion>Mac will never drop carbon</my-opinion>

Rouss
01-25-2005, 01:05 PM
I'll admit to not researching Objective-C fully before coming to that conclusion, so I'm all set to be proven wrong.

Whoa, I wasn't trying to prove anyone wrong (I don't have enough expertise to do that), I was just explaining the difference between the two.


These headers, is it like including iostrem where it allows non-standard lookging function calls like cin and cout? Or do they simply add additional functions?

Since Objective-C is object oriented I imagine they have to be adding more than just additional functions.

It's not really like the cpp headers. You would use #import <objc/object.h> (objc encourages using import instead of include, but you can use either one). And they're are other headers you can use, such as one for a list object.
And really all the header does is allow a few new functions and data types, and allow a few new syntactical ways of dealing with objects. I think they are based on the way smalltalk handles objects. Such as

id MyObject; /*id is basically a void pointer that CAN be dereferenced*/
MyObject = new ObjectClass; /*make a new instance of ObjectClass*/
/* could also be written */
ObjectClass *MyObject = new ObjectClass; \* or something like that */
/*to call object methods looks like this */
[MyObject MethodName:Argument]; /*Calls MyObject method MethodName, and passes it Argument ] */
int myNum;
myNum = [MyObject MethodThatReturnsInt:Arg]; /*This calls an object method that returns a value, and I am storing the value in myNum.*/
/*and when you're done you have to free the object*/
[MyObject free]; /* you can even overload the free method */

So, you can see the difference between C++ and ObjC. My apologies to everyone who was bored by that.


I've also heard some talk about Objective-C++ where its Objective-C but also lets you use all of C++ as well. So I guess Mac does permit C++ programming.
You can do all kinds of programming on Mac; C, ObjC, C++, perl, asm, etc.


I guess the question then becomes how different really is Objective-C from ANSI C? If its just a a few headers, it hardly seems worth renaming it.
Beyond what you can use when you include the headers, it's not different than ANSI C. You can include the objc headers and write a plain non objective C program, and it will work just the same as if you didn't include the headers. Maybe not worth the name change. It might be more intuitive to say that I wrote a C program with the Objective C headers. I really don't like C++'s name, because many times I have heard, as I know most everyone else here has, this question, "Isn't C outdated? Why don't you use C++?" It's like people think that when C++ was created that it replaced C, and C should have crawled into a hole and died.

-KEN-
01-25-2005, 01:16 PM
>>Which kinda sounds like Apple will keep it around until everyone is up to date then drop support,

<my-opinion>Mac will never drop carbon</my-opinion>

Yeah. Important applications of theirs like Finder are written in Carbon.

Exile
01-25-2005, 01:48 PM
Whoa, I wasn't trying to prove anyone wrong (I don't have enough expertise to do that), I was just explaining the difference between the two.
I know, I was just saying that my opinions were very shortsighed and sound pretty negative. I knew it and was stating that I expected them to be wrong.


You can do all kinds of programming on Mac; C, ObjC, C++, perl, asm, etc.
I thought they permitted all thee C languages (any better way to say that?) in one source file and the compiler would accept it and generate an executable?


Thanks for all the details on that, I haven't seen much that gives the low down on this stuff. That clears up a lot of the questions I had on Objective-C.

Rouss
01-25-2005, 02:41 PM
I thought they permitted all thee C languages (any better way to say that?) in one source file and the compiler would accept it and generate an executable?

I'm not sure what you mean. I don't think you can write a C program and compile it as C++, unless you include the C headers the way c++ likes it, <cstdio>, <cstring>, etc. And I don't think you can compile a C++ file as C... But I don't really know about this. Maybe somebody else would be better to answer this question than me.

Exile
01-25-2005, 02:48 PM
I'm not sure what you mean. I don't think you can write a C program and compile it as C++, unless you include the C headers the way c++ likes it, <cstdio>, <cstring>, etc. And I don't think you can compile a C++ file as C... But I don't really know about this. Maybe somebody else would be better to answer this question than me.
From what I was reading, the compiler isn't limited to just C and/or Objective-C code. You can mix in C++ as well and it won't get upset.

The Mac compiler (not necessarily the gcc port to OS X, I'm not sure what it will take) will handle C, C++, and Objective-C all in one source file. Naturally you have to include all the required headers and libraries to make it work.

Hope that clears up what I was saying.

Rouss
01-25-2005, 03:13 PM
So you could include <stdio> and <iostream> and write a hybrid c/c++ program? Is this just a Mac thing, or is it true of all versions of gcc? I guess I could test that question myself. I don't write much c++ anyway, so it doesn'y really matter.

Exile
01-25-2005, 03:36 PM
So you could include <stdio> and <iostream> and write a hybrid c/c++ program? Is this just a Mac thing, or is it true of all versions of gcc? I guess I could test that question myself. I don't write much c++ anyway, so it doesn'y really matter.
Thats pretty standard in C++ compilers. Visual studio and gcc will permit that. Those are the only compilers I use so I can't say for sure that all compilers will permit it, but so far as I an tell they all will.

OSDever
05-06-2005, 03:12 PM
Maybe I can help on this one. Cocoa and Objective C are nothing but systems to communicate with a UI made with Interface Builder. You can use C or C++ in your ObjC files, by suffixing your files with .m or .mm. Cocoa was created with fast Mac OS X programming in mind, and does not work in Mac Classic. Carbon is a straight C or C++ file that uses much more code to communicate with the user interface. Carbon requires much more code, but in return there are many more things that you can do with it, and it runs in Mac Classic. You can mix Carbon code into Cocoa code, but you cannot use control the same UI with the two different codes. Carbon probably won't be replaced any time soon. There is a lot more info on the Apple Developer Website.