Reputation: 6362
I'd like to use some C++ code in an Xcode project but I don't understand either language well enough yet to know how to proceed. I've been at the Obj-C for a while and have an app on the app store, but still learning...
The code I want to use has two files with the same name and .h
and .c
extensions. I would think they correspond to .h
and .m
files in Obj-C, but they lack the @interface
and @implementation
structure I'm familiar with. And there's a main.c
that I don't know what to do with. It looks like it's the main program - should I try to pull its code out into my primary viewController
?
Maybe a link to a good tutorial? Maybe this question's too vague...
FYI - the code I want to use is for calculating sunrise and sunset times, and is located at: http://www.risacher.org/sunwait/
Thanks!
EDIT:
Thanks for the suggestions - I will probably have to do some more learning before I get this. But I made some progress...
In main.c
(seems weird to have a file called that...) there is a function(?) like this:
int mainFunction(int argc, char *argv[])
{
// a bunch of function-y stuff
}
It was called main
but I changed it to mainFunction
to get rid of an error. Now it compiles and I can call it but the compiler warns me thus: warning: implicit declaration of function 'mainFunction'
and it crashes after I call it.
Upvotes: 2
Views: 3688
Reputation: 86651
First of all, if your files have a .c
extension, they are almost certainly C and not C++. All you have to do is add them to the project and target and include the relevant .h
file in the Objective-C where you call the C functions.
Now the fact that you used to have a function called main()
tells us that you had a stand alone program. Every stand alone program has to have a function called main()
, it's the entry point for the program. Your Objective-C application will have a function main()
already which is why you were getting an error. You can take the approach of renaming the duplicate and calling it but there are a number of pitfalls with that approach.
As you found out, you need a function prototype to stop the implicit declaration warning. This should be
int mainFunction(int argc, char* argv[]);
and should be in a header that you include in the .m file where you want to call it.
In C, certain assumptions are made about the parameters. argc
is the number of char*
s in argv. argv[0]
is conventionally the name of the program as invoked on the command line. So argc
must be at least 1 and argv[0]
must point to a string. The remaining char*
s in argv point to the command line parameters.
I suggest you work through the basics of C to gain an understanding of it. You can probably learn enough to progress your project in a day or two. The classic text is The C Programming Language. It's still one of the best IMHO.
Upvotes: 1
Reputation: 95449
Now it compiles and I can call it but the compiler warns me thus: warning: implicit declaration of function 'mainFunction' and it crashes after I call it.
This is because, where you use this function, you do not forward-declare or include the declaration of the function. In your Objective-C code, you should add the following bit of code:
int mainFunction(int argc, char *argv[]);
Also note that the ".c" file extension is C code, not C++. Since Objective-C is a superset of C, you could just as easily change the extension to ".m", and you'd still get the same error since you need this forward declaration. I would strongly recommend familiarizing yourself with both C and C++ code before venturing into the world of Objective-C. If you wish to forgoe learning C++, then at the very least, you should strengthen your C knowledge before going into Objective-C, as the C fundamentals apply.
Upvotes: 2
Reputation: 424
There are not many tutorials about this unfortunately. I think this has been asked a couple of times in stackoverflow, so searching here will give you some hints. The first pitfall you would want to avoid is, you need to use the ".mm" extension for your obj-c files to enable the c++ extension.
One last pointer is, look at opensource projects e.g. chrome to see how they mix up obj-c and C++. Google search is also your friend :).
Upvotes: 0
Reputation: 61331
Rename sources from .m to .mm, then they become Objective C++. You can instantiate and call C++ classes from Objective C code and vice versa. You cannot, though, derive ObjC classes from C++ classes and vice versa. Also, mind the calling conventions - the global functions in .m files are extern "C"
as far as .mm/.cpp files are concerned.
Linking together .mm and .cpp works fine, too.
Upvotes: 1
Reputation: 15588
There is a chapter in the Objective-C 2.0 guide here: http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/ObjectiveC/Articles/ocCPlusPlus.html#//apple_ref/doc/uid/TP30001163-CH10-SW1
Basically you can mix C++ and objective-C, but there are a couple of pitfalls. It sounds like you may need to learn more about C++ in general before you explore the nuances of objective-C++
Upvotes: 1