Atma
Atma

Reputation: 29767

Use a C++ file in Cocoa iPhone App

I'm trying to use the functionality of the example iPhone app AQOfflineRenderTest in my app (this app specifically isn't important, I just thought it worth mentioning). I have a C++ file with method called:

    void DoAQOfflineRender(CFURLRef sourceURL, CFURLRef destinationURL)

I'm trying to call this from my Cocoa Objective-C class. The example app I referenced above does this by declaring the method in the header file like so:

    - (IBAction)doSomethingAction:(id)sender;
    @end
    void DoAQOfflineRender(CFURLRef sourceURL, CFURLRef destinationURL);

In the implementation file I call the doSomethingAction which calls renderAudio and executes the same code:

    DoAQOfflineRender(sourceURL, destinationURL);

When I execute I get the error: _DoAQOfflineRender", referenced from: -[myViewController renderAudio] in myViewController.o symbol(s) not found collect2: ld returned 1 exit status

Am I missing a reference to this C++ class? It doesn't specify one in the example app but it runs.

Please help and thanks!

Joe

Upvotes: 0

Views: 574

Answers (3)

Jens Ayton
Jens Ayton

Reputation: 14558

To call a C++ function from a C or Objective-C file, you must declare it with extern "C" linkage in C++.

#if __cplusplus
extern "C" {
#endif

void DoAQOfflineRender(CFURLRef sourceURL, CFURLRef destinationURL);

#if __cplusplus
}
#endif

The reason for this is that C++ functions’ names are modified by encoding their types, so that functions with the same name but different signatures become different symbols. For example, DoAQOfflineRender(CFURLRef, CFURLRef) becomes __Z17DoAQOfflineRenderPK7__CFURLS1_ without extern "C", but _DoAQOfflineRender with it, which is what the C/Objective-C expects.

Upvotes: 0

Amok
Amok

Reputation: 1269

It looks like you're using .m as the extension for you Objective-C source file so the compiler thinks you are referencing a C function. Try changing the extension to .mm.

Upvotes: 0

fbrereto
fbrereto

Reputation: 35925

What you're missing is the implementation of DoAQOfflineRender -- did you forget to link in the library where this routine is defined?

Upvotes: 1

Related Questions