Register FAQ / Rules Forum Spy Search Today's Posts Mark Forums Read
Go Back   MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old May 8, 2008, 07:54 AM   #1
wrldwzrd89
macrumors Demi-God
 
wrldwzrd89's Avatar
 
Join Date: Jun 2003
Location: Solon, OH
Calling functions in other languages (Obj-C <-> C)

I have a book on Objective-C, and I read through it (but haven't had time, until today, to try following the examples). I would like to make a project in XCode that uses a mixture of Objective-C code and plain old C. I understand how to call C functions from Objective-C code, but not calling Objective-C functions from C code. Is this even possible? If it is, how would I go about doing it?

EDIT: If this isn't possible, it's no big deal. I should be able to work around it.
__________________
iMac Intel (Rev H, 27"), 1TB HDD, 16GB RAM, Ubuntu

Last edited by wrldwzrd89; May 8, 2008 at 08:01 AM. Reason: More information
wrldwzrd89 is offline   0 Reply With Quote
Old May 8, 2008, 08:08 AM   #2
iSee
macrumors 68030
 
iSee's Avatar
 
Join Date: Oct 2004
Well, Objective-C is a superset of C. So you can compile your "C" code with the Objective-C compiler and therefore, make Objective-C calls easily.

In fact, I'd say if your C code is calling Objective-C methods, it's not C code at all--it's really Objective-C.
iSee is offline   0 Reply With Quote
Old May 8, 2008, 08:16 AM   #3
wrldwzrd89
Thread Starter
macrumors Demi-God
 
wrldwzrd89's Avatar
 
Join Date: Jun 2003
Location: Solon, OH
Quote:
Originally Posted by iSee View Post
Well, Objective-C is a superset of C. So you can compile your "C" code with the Objective-C compiler and therefore, make Objective-C calls easily.

In fact, I'd say if your C code is calling Objective-C methods, it's not C code at all--it's really Objective-C.
That's good to know. The reason I ask is this: I wish to write a cross-platform application, and I realize that the easiest way to do this is to write the back-end in C and the GUI parts in Objective-C, for the Mac OS X version. The question then becomes one of what language to use for the Windows and Linux versions for the GUI portions. For Linux, of course, it's a no-brainer: C++ is the obvious choice. Windows, though, is more complicated, due to the .NET system Microsoft is encouraging their developers to use.
__________________
iMac Intel (Rev H, 27"), 1TB HDD, 16GB RAM, Ubuntu
wrldwzrd89 is offline   0 Reply With Quote
Old May 8, 2008, 09:28 AM   #4
iSee
macrumors 68030
 
iSee's Avatar
 
Join Date: Oct 2004
In that case, I'd see if I could avoid having any Objective-C in the backend at all. If you do, that means some platform specific stuff is creeping in to what is supposed to be the cross-platform layer.

You'll what to structure it so that primarily the GUI, platform-specific layer (Obj-C on Mac, C++ on linux, C? on Windows) makes calls to the cross-platform C layer but not the other way around.

If/when you do need callbacks from the platform neutral layer to the platform specific layer, it makes sense to consider using C functions and data types for that interface.

If your project is large, consider three layers:
1. GUI - platform specific language
2. PAL - "platform abstraction layer" You'll have one version of this for each platform
3. Cross-platform backend.

The PAL sits between the two other layers. It's only job is to make the connection between layer's 1 and 3. Each version presents the same logical interface to each of the GUI platforms it supports, but each implementation would contain language/platform specific details. It's important that the interface for the GUI-layer be logically identical across platforms even though it is implemented in the platform-specific language. For example, if a function took a rectangle parameter, it might be a CGRect on Mac and a RECT structure on Windows, but it's still representing the same thing. If you don't maintain a logically identical interface for the PAL, it loses its purpose and advantages. If the cross-platform layer does need to make callbacks to the GUI layer, the PAL would also expose an interface for that.

So calls from the GUI to the backend go through the PAL.

Mac: Layer 1 makes Objective-C call to the PAL. The PAL, in turn, makes a C call to the backend, transforming parameters, objects, etc, as needed. Return values are bundled back up a an Objective-C type, if needed.

Linux: Layer 1 makes C++ call to the PAL. The PAL transforms the parameters as needed and makes the same C call to the backend. Return values are bundled back up as a C++ type, if needed.

Win: Layer 1 makes a C# (for example) call to the PAL. The PAL transforms the parameters as needed (probably needs a lot coming from managed C# to unmanaged C) and makes the call to the PAL. Return values are bundled back up as a C# type, as needed.
iSee is offline   0 Reply With Quote
Old May 8, 2008, 09:35 AM   #5
Eraserhead
macrumors G4
 
Eraserhead's Avatar
 
Join Date: Nov 2005
Location: UK
Quote:
Originally Posted by wrldwzrd89 View Post
Windows, though, is more complicated, due to the .NET system Microsoft is encouraging their developers to use.
With regards to .NET the problem is that it is incomplete so not all functions in Win32 are implemented so you may well have to fall back to C++/Win32 anyway. I can't go into technical details of whether this will be an issue for your application however, and depending on what you want to do it may not be an issue.

.NET also isn't being widely used for desktop applications, currently there are very few applications written in .NET currently on Windows, certainly considerably less than there are Cocoa applications for the Mac.

FWIW Adobe Lightroom has used C++/Win32 for Windows and Objective C/Cocoa for Mac.
__________________
If they have to tell you every day they are fair you can bet they arent, if they tell you they are balanced then you should know they are not - Don't Hurt me
Eraserhead is offline   0 Reply With Quote
Old May 8, 2008, 09:39 AM   #6
wrldwzrd89
Thread Starter
macrumors Demi-God
 
wrldwzrd89's Avatar
 
Join Date: Jun 2003
Location: Solon, OH
Quote:
Originally Posted by Eraserhead View Post
With regards to .NET the problem is that it is incomplete so not all functions in Win32 are implemented so you may well have to fall back to C++/Win32 anyway. I can't go into technical details of whether this will be an issue for your application however, and depending on what you want to do it may not be an issue.

.NET also isn't being widely used for desktop applications, currently there are very few applications written in .NET currently on Windows, certainly considerably less than there are Cocoa applications for the Mac.

FWIW Adobe Lightroom has used C++/Win32 for Windows and Objective C/Cocoa for Mac.
Thank you, I did not know this about .NET... in which case I probably WILL use Win32 for the Windows version, simply because I can target more versions of Windows that way.
__________________
iMac Intel (Rev H, 27"), 1TB HDD, 16GB RAM, Ubuntu
wrldwzrd89 is offline   0 Reply With Quote
Old May 8, 2008, 10:36 AM   #7
yeroen
macrumors 6502a
 
yeroen's Avatar
 
Join Date: Mar 2007
Location: Cambridge, MA
For cross-platform C++ application development with native GUI support on each platform, use Qt.

It's a very elegant framework and will save you much strain, effort, and time trying to maintain three different forks of the development tree: OSX/Aqua, Linux/BSD/X11, and Windows.
__________________
all Jarvis, all the time
yeroen is offline   0 Reply With Quote
Old May 8, 2008, 10:48 AM   #8
Catfish_Man
macrumors 68030
 
Catfish_Man's Avatar
 
Join Date: Sep 2001
Location: Portland, OR
Send a message via AIM to Catfish_Man
Quote:
Originally Posted by yeroen View Post
For cross-platform C++ application development with native GUI support on each platform, use Qt.

It's a very elegant framework and will save you much strain, effort, and time trying to maintain three different forks of the development tree: OSX/Aqua, Linux/BSD/X11, and Windows.
Native is a funny word. Yes, it's using CG/Cocoa/whatever under the hood, but native from a user perspective (arguably the main one that matters) means that it *behaves* like a native app as well as looking like one.
Catfish_Man is offline   0 Reply With Quote
Old May 8, 2008, 11:47 AM   #9
Eraserhead
macrumors G4
 
Eraserhead's Avatar
 
Join Date: Nov 2005
Location: UK
I've heard good things about Qt it could well be worth a look.
__________________
If they have to tell you every day they are fair you can bet they arent, if they tell you they are balanced then you should know they are not - Don't Hurt me
Eraserhead is offline   0 Reply With Quote
Old May 8, 2008, 12:08 PM   #10
Cromulent
macrumors 603
 
Cromulent's Avatar
 
Join Date: Oct 2006
Location: The Land of Hope and Glory
Quote:
Originally Posted by Catfish_Man View Post
Native is a funny word. Yes, it's using CG/Cocoa/whatever under the hood, but native from a user perspective (arguably the main one that matters) means that it *behaves* like a native app as well as looking like one.
Surely all that native means is that it is compiled to run on the processor that the computer uses, i.e it is not emulated which is the opposite of a native application.

I know where you are coming from but it is a rather confusing use of a term which up until recently had a very clear cut meaning.
__________________
Neural Advance - Mac OS X, UNIX and Windows Development
Last.fm Profile | Extreme Metal Reviews
MP 4x 2.66Ghz Xeons / 6GB RAM / 640GB + 500GB + 750GB + 1TB HDDs / ATI Radeon 4870 / iPad 3
Cromulent is offline   0 Reply With Quote
Old May 8, 2008, 12:11 PM   #11
Ti_Poussin
macrumors regular
 
Join Date: May 2005
Quote:
Originally Posted by yeroen View Post
For cross-platform C++ application development with native GUI support on each platform, use Qt.

It's a very elegant framework and will save you much strain, effort, and time trying to maintain three different forks of the development tree: OSX/Aqua, Linux/BSD/X11, and Windows.
You may also take a look at wxWidgets, it's a lot like Qt. I personnaly prefer it to Qt, but it's really personnal on that matter.
http://www.wxwidgets.org/
__________________
iMac Core2D 2.16 GHz 20", 250Go SATA/Firewire HD, iPhone 3G, Logitech Z-680 speaker set, MS mouse 5 bouttons
Ti_Poussin is offline   0 Reply With Quote
Old May 8, 2008, 12:12 PM   #12
wrldwzrd89
Thread Starter
macrumors Demi-God
 
wrldwzrd89's Avatar
 
Join Date: Jun 2003
Location: Solon, OH
Quote:
Originally Posted by Ti_Poussin View Post
You may also take a look at wxWidgets, it's a lot like Qt. I personnaly prefer it to Qt, but it's really personnal on that matter.
http://www.wxwidgets.org/
I was not aware that Qt was a cross-platform toolkit. I was aware of wxWidgets but I'm not sure if I want to use it or not, yet.
__________________
iMac Intel (Rev H, 27"), 1TB HDD, 16GB RAM, Ubuntu
wrldwzrd89 is offline   0 Reply With Quote
Old May 8, 2008, 12:19 PM   #13
yeroen
macrumors 6502a
 
yeroen's Avatar
 
Join Date: Mar 2007
Location: Cambridge, MA
Qt used to only emulate the look and feel of the native widget sets, but the latest versions (from Qt4 onwards, I believe) use the native APIs.
__________________
all Jarvis, all the time
yeroen is offline   0 Reply With Quote
Old May 9, 2008, 10:37 AM   #14
wrldwzrd89
Thread Starter
macrumors Demi-God
 
wrldwzrd89's Avatar
 
Join Date: Jun 2003
Location: Solon, OH
Anyone got a link to the Qt homepage? I'm interested in looking into this - if it works the way I hope it does it'll save me a ton of time.
__________________
iMac Intel (Rev H, 27"), 1TB HDD, 16GB RAM, Ubuntu
wrldwzrd89 is offline   0 Reply With Quote
Old May 9, 2008, 10:41 AM   #15
Cromulent
macrumors 603
 
Cromulent's Avatar
 
Join Date: Oct 2006
Location: The Land of Hope and Glory
Quote:
Originally Posted by wrldwzrd89 View Post
Anyone got a link to the Qt homepage? I'm interested in looking into this - if it works the way I hope it does it'll save me a ton of time.
http://www.trolltech.com/products/qt/
__________________
Neural Advance - Mac OS X, UNIX and Windows Development
Last.fm Profile | Extreme Metal Reviews
MP 4x 2.66Ghz Xeons / 6GB RAM / 640GB + 500GB + 750GB + 1TB HDDs / ATI Radeon 4870 / iPad 3
Cromulent is offline   0 Reply With Quote
Old May 9, 2008, 05:13 PM   #16
wrldwzrd89
Thread Starter
macrumors Demi-God
 
wrldwzrd89's Avatar
 
Join Date: Jun 2003
Location: Solon, OH
Well, I looked at both products. Qt doesn't meet my needs very well, but wxWidgets meets them VERY well. I think I'll be using wxWidgets, then.
__________________
iMac Intel (Rev H, 27"), 1TB HDD, 16GB RAM, Ubuntu
wrldwzrd89 is offline   0 Reply With Quote

Reply
MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
thread Thread Starter Forum Replies Last Post
How do sheets call functions in objective-C? wrayal Mac Programming 10 Aug 23, 2008 02:02 PM
Spell checking in other language than system language? Thrash911 Mac OS X 2 Feb 15, 2008 08:55 AM
Crude Way to Send Email in Other Languages tom@bluesky.org Jailbreaks and iOS Hacks 1 Jul 6, 2007 12:56 PM
iLife 4 in other languages Nikko1965 Mac Applications and Mac App Store 1 Feb 4, 2004 04:27 AM


All times are GMT -5. The time now is 09:50 PM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Powered by vBulletin® Version 3.8.6
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

Privacy / DMCA contact / Affiliate and FTC Disclosure
Copyright 2002-2013, MacRumors.com, LLC