Creating a window using C++?

Does anyone know of any way of creating a window using C++?
I've got Hillegass' book but I'm trying to implement my graphics library on Mac and I'm finding it hard creating a window without Objective-C.

Any pointers?

Macbook, Mac OS X (10.6)

Posted on Dec 7, 2009 8:51 AM

Reply
Question marked as Top-ranking reply

Posted on Dec 7, 2009 12:04 PM

If you're using OpenGL and you need to create a window, you could use GLUT or SDL to create the window. They're both C APIs so you could write your code in C++.

If you need a Mac GUI and are determined to avoid Objective-C, you could use Carbon, but Carbon has several problems.

1. Xcode 3.2 doesn't ship with Carbon project templates so you would have to install Xcode 3.1 to get access to the Carbon project templates.
2. You can't write 64-bit GUI applications with Carbon.
3. More people use Cocoa than Carbon so getting answers to Carbon questions will be difficult.

A third option is to use Cocoa and Objective-C for your GUI and C++ for the rest of your code. By giving your Objective-C code the extension .mm, the compiler will treat the files as Objective-C++, which lets you write programs that use both C++ and Objective-C.
4 replies
Question marked as Top-ranking reply

Dec 7, 2009 12:04 PM in response to RagingAvatar

If you're using OpenGL and you need to create a window, you could use GLUT or SDL to create the window. They're both C APIs so you could write your code in C++.

If you need a Mac GUI and are determined to avoid Objective-C, you could use Carbon, but Carbon has several problems.

1. Xcode 3.2 doesn't ship with Carbon project templates so you would have to install Xcode 3.1 to get access to the Carbon project templates.
2. You can't write 64-bit GUI applications with Carbon.
3. More people use Cocoa than Carbon so getting answers to Carbon questions will be difficult.

A third option is to use Cocoa and Objective-C for your GUI and C++ for the rest of your code. By giving your Objective-C code the extension .mm, the compiler will treat the files as Objective-C++, which lets you write programs that use both C++ and Objective-C.

Dec 7, 2009 3:12 PM in response to Mark Szymczyk

"A third option is to use Cocoa and Objective-C for your GUI and C++ for the rest of your code. By giving your Objective-C code the extension .mm, the compiler will treat the files as Objective-C++, which lets you write programs that use both C++ and Objective-C."

I should've been far more detailed and less lazy in my original question!

This third option is exactly what I'm looking to implement but I'm finding it very hard to find an actual real world example of what I want.

I would ideally like to have a C++ application that calls some Objective-C code in order to create the window and the OpenGL context and return back to my C++ application. I could then keep the Objective-C object around for when I need to make calls to the window context.

Is that even possible?

Dec 7, 2009 5:41 PM in response to RagingAvatar

I have not mixed C++ and Objective-C in a program, but I think it would be easier to create a Cocoa application that makes calls to C++ code than to have a C++ application that makes calls to Cocoa code.

If you read the Objective-C Programming Language document that comes with Apple's developer documentation, there's a section on mixing C++ and Objective-C. The document comes up as the second result of a Google search for Objective C++. Stack Overflow has 20 questions tagged with Objective-C++ that could help.

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

Creating a window using C++?

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.