Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Software - Troubleshooting and Discussion > Developer Center > C question

C question
Thread Tools
Junior Member
Join Date: Nov 2000
Status: Offline
Reply With Quote
May 11, 2001, 07:30 PM
 
If I want to use simple C functions in a Obj-C/cocoa app how do I go about doing that?

Can I just write the functions in a .c file and import them with #import?

-Max
     
Dedicated MacNNer
Join Date: Jan 2001
Location: Virginia, US
Status: Offline
Reply With Quote
May 11, 2001, 08:25 PM
 
Yep. You can use #include too; it works like normal. #import automatically protects against multiple inclusions; that's the only different from #include.
     
Fresh-Faced Recruit
Join Date: Apr 2001
Status: Offline
Reply With Quote
May 11, 2001, 09:06 PM
 
You could import the .c file, but most common programming practice is to put the function prototypes in a .h file, and then include it in both the .c file and whatever else you plan on using the functions in. The linker will take care of the rest in the build phase.

Also note that in most standard C programs, #import is not used, but instead they do something like this:
Code:
[my_func.h] #ifndef MY_FUNC_H #define MY_FUNC_H /* code goes here */ #endif /* MY_FUNC_H */
This also protects from multiple inclusions. Keep this in mind if you write any code you plan on running on OS's other than OS X, because other compilers will either die or issue a warning for the use of #import statements. Read Apple's compiler documentation in the /Developer folder for more details.
     
Addicted to MacNN
Join Date: Mar 2000
Location: London, UK
Status: Offline
Reply With Quote
May 12, 2001, 05:20 AM
 
Well, in a general answer to your first question, you can use any C code in an Objective-C Cocoa app. Objective-C is a superset of C, so any valid C code is also valid Obj-C code.
     
Senior User
Join Date: Feb 2001
Location: Rochester, uk
Status: Offline
Reply With Quote
May 13, 2001, 04:10 AM
 
Yep, you can use any C code you want to. Just make sure that any functions you write are outside of the @interface...@end and @implementation...@end sections, and it'll work fine.

In my current project, i've got one file with a pile of C string functions, strings.m, and a file strings.h that various classes import as needed. It works perfectly.

All words are lies. Including these ones.
     
Xeo
Moderator Emeritus
Join Date: Mar 2001
Location: Austin, MN, USA
Status: Offline
Reply With Quote
May 20, 2001, 09:52 AM
 
I have a .c file from a person in one of my classes. When compiled, it is a text based, interactive program.

How would I go about giving this a GUI? I don't know any C yet, but I have done a few tutorials for Project builder.

I was thinking I could have a push button that would start it, and the rest of the app would just run inside a text field. Is this possible?


------------------
     
Admin Emeritus
Join Date: Oct 2000
Location: Boston, MA
Status: Offline
Reply With Quote
May 20, 2001, 11:25 AM
 
Yup, it's very possible Xeo. That happens with stuff like OpenUp and other Gooey programs. Of course, modeling a true interface around the app is your best bet...

What kind of program is it?
"Against stupidity, the gods themselves contend in vain" (Schiller)
     
Xeo
Moderator Emeritus
Join Date: Mar 2001
Location: Austin, MN, USA
Status: Offline
Reply With Quote
May 20, 2001, 12:21 PM
 
It's a cryptanalysis project. Basically, once you compile it, and run it, it opens to a blank screen. You have to hit ? (question mark) to get a small menu which tells you the kind of things that can be done.

I have no idea how to give it a GUI. I didn't write it and even in Terminal it's crap. (actually, I couldn't get it compiled correctly on this either, I had to use my network's Unix server via telnet.)

It's probably best if I let the program die. I was just curious if it was possible.

Thanks.

------------------
     
Addicted to MacNN
Join Date: Mar 2000
Location: London, UK
Status: Offline
Reply With Quote
May 21, 2001, 01:47 AM
 
No, I don't think you can give programs like that a GUI very easily... Unless you were to rip out the interface and chuck the backend into your project
     
   
Thread Tools
Forum Links
Forum 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
Trackbacks are On
Pingbacks are On
Refbacks are On
Top
Privacy Policy
All times are GMT -5. The time now is 10:03 PM.
All contents of these forums © 1995-2011 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.7 © 2000-2011, Jelsoft Enterprises Ltd., Content Relevant URLs by vBSEO 3.3.2