C++ on Mac

Joeyt

Registered
I am trying to program in C++ on my iBook (I am taking AP CS classes at my High School where they are teaching C++), however I am having some problems compiling & coding. Here are my questions:

1) Is C++ what people normaly program in on OS X?
2) What are you supposed to use to write C++ code with? Project Builder?
3) When compiling and linking, how come the executable is so big? A simple "Hello World!" program is 100k! Is there a way I can shrink it?
4) How do I do GUI programming w/ C++?
5) Can someone explain what Cocoa is? Is it a language, or an API?

Thanks in advance,

Joey
 
1) C++ is the access language to MacOS X's Carbon API, also Embedded C++ is the language used to access Apple's IOKit framework for driver development.

2) You can use any text editor to write your code. An Integrated Development Environment like Project builder just provides you with a one-stop-shop sort of shell for quick access and use of most of the development tools you'll need to use. You could just as easily write all your code in BBEdit and compile it from the commandline or from one of BBEdits built in scripts.

3) The executable is big for probably two reasons. The default target type is debug so a bunch of extra information is included into the code which makes it easier to debug. Another reason is that being in a novice programming course you're probably linking to large class libraries like iostream, linking those libraries into your code will increase their size. There are a slew of compiler directives you can use to minimize that effect, but you probably ought not to worry about that stuff for now.

4) To do GUI application development for MacOS X using C++ you'd have to use the Carbon API or load TrollTech's MacOS X Qt 3.0 API onto the system and use Qt if you so desired. My recommendation would be to stick to Carbon unless your intention is to be able to make one code base that can effectively run on MacOS X, Linux, and Windows with only minor modifications. I'm not sure why you'd want to be doing GUI programming at this point now, most UI API's and Frameworks are overly complex for anything you'll need to do in your course work. Java's JFC and Swing are about the simplest to use only because the abstraction from complex referencing and allocation problems that the language provides you with lets you just kind of go at it.

5) Cocoa is a large and robust object oriented development API which houses dozens of frameworks. It's access languages are ObjectiveC and Java. ObjectiveC is the original native language for using the Cocoa API, which is an inherited trait from the API's days at NeXT under the names NeXTstep and OpenStep. Since ObjC doesn't allow for global namespaces most of the classes in Cocoa are prefixed with the letters "NS", which stood for NeXTstep. However developer reluctance to be forced into learning a fringe, although very powerfulo and intuitive, language such as ObjectiveC was what forced Apple's hand to include a bridge system to allow Java to be used for Cocoa development.

-Nathan
 
Wow, thanks naschbac!

Ok more questions :)

If I have to use Cocoa (cause it looks like every one else who is programming on Mac OS X is using it) I am gonna use Objective-C (I would use Java but I don't wanna take that performance hit). Where would be the best place where I could learn Objective-C? A book? Tutorial?

Thanks again,

Joey
 
Well first I'd like to say that there's not much of a performance hit when using Java to access Cocoa. Apple's Java VM is pretty good and the only major performance hit you'd likely notice is that the load time for your app may take longer because the system will have to wait for the Java VM to load. Subsequent launches of Java programs should experience this load delay until you either log out or restart as the VM should stay resident.

Second you don't have to use Cocoa, most people use it because it's the world's most advanced and intuitive development API when coupled with Project Builder and Interface Builder. It's really quite phenomenal how easy it is to turn quick application prototypes into fully functional applications using these tools. You could just as well use other languages/API's like C+ & Carbon, C++ & Qt3, Java & JFC, AppleScript & AS Studio, or even RealBASIC. It just depends on what you want to do and what other constraints you decide to place on your project.

Third if you want to do Mac OS X programming there's a couple books I could suggest, one you'll have to buy (but it's well worth it) and the other you can download from Apple.

Here's the links:

Cocoa Programming for Mac OS X

Object-Oriented Programming and the Objective-C Language

The first one is a good book to get the basics about building Cocoa applications with ObjC from a good author that writes with a great teaching voice.

The second one is a PDF from Apple which I recommend you look at to understand the basics of the ObjC language. It's essentially a manual on the ObjC language but it's reasonably well written and the first three chapters will give you a very good base for further learning.

-Nathan
 
Check your hard drive for these two files:

/Developer/Documentation/Cocoa/JavaTutorial/javatutorial.pdf
/Developer/Documentation/Cocoa/ObjCTutorial/objctutorial.pdf

Both of them will walk you through building a simple application in Java and Objective C respectively. These should help you get the necessary basic familiarity with Interface Builder, and give you a simple idea of how Cocoa programming works.

As for the performance hit of using Java, it's nowhere near as much as with something like Swing or AWT, since java libraries aren't being used to display the GUI. I think that's what causes most of the java bottlenecks on OS X - bridging the GUI rendering. I made a little Cocoa desk calculator in Java, and you'd never tell from looking at it that it's not C.
 
OK this is a bit off topic but its kind of under the same category of programming on OS X.

I poked around the developer.apple.com site and I am wondering about developing Cocoa-Java apps. Creating a new project in PB I noticed that you have to use Interface Builder to well make a UI :) I am wondering is there anyway to develop Cocoa-Java apps from scratch (like going into Terminal, using vi/pico writing an app, and running the java/c commands)?? I like doing things the hard way (I hate using wizards and GUI apps to make other GUI apps).

Thanks in advance,

Joey
 
I think it can be done by instantiating all the necessary classes (NSWindow, etc.) and setting them up with the proper drawing coordinates, but frankly, I don't even want to try. It's hellish enough writing AWT apps from scratch! :)
 
Originally posted by Joeyt
OK this is a bit off topic but its kind of under the same category of programming on OS X.

I poked around the developer.apple.com site and I am wondering about developing Cocoa-Java apps. Creating a new project in PB I noticed that you have to use Interface Builder to well make a UI :) I am wondering is there anyway to develop Cocoa-Java apps from scratch (like going into Terminal, using vi/pico writing an app, and running the java/c commands)?? I like doing things the hard way (I hate using wizards and GUI apps to make other GUI apps).

Thanks in advance,

Joey
Just FYI, there is a rather large performance hit when using Cocoa-Java. This is because every message you send from Java is sent over a Java to Obj-C "bridge", which means repeated calls in a loop can be very slow. Here's a good article as to why using Cocoa-Java isn't a great idea: http://www.bignerdranch.com/Resources/Java.html

You can crate a GUI from scratch in Cocoa/Obj-C, but it's completely pointless. Just don't bother. To do that, you would have to override main(), create an autorelease pool, connect to the window server, establish the main run loop, create a window, create a menu bar, create menu items, give each item a target/action, then create buttons and other elements, give appropriate elements target./actions and any other attributes.

Interface Builder isn't just a "GUI wizard"; it has existed from the very beginning of NeXTStep and is one of the elements that makes it great. Unlike other applications, it doesn't just create code to create the elements, it actually represents all GUI elements in a binary file which is unarchived at runtime. In addition to GUI elements, it contains information about classes in your application, the targets and actions of elements, and numerous other things. It also makes for easy localization. So, please, just use IB :).
 
ok ok.

Where can I find a REALLY good book that teaches Obj-C AND Cocoa (in that order :) )? I have some experience with C++/Java (I have a feeling I should have learned C instead of C++, everyone seems to praise C alot more than C++).

Thanks in advance,

Joey
 
Back
Top