shle2821
shle2821

Reputation: 1886

How to interact with C++ build within Xcode from Objective-C

I've never tried this before so any and all comments or suggestions will be very appreciated. The big picture is that I'm trying to use C++ files within an iOS app.

The whole project started with C++ files. Not Objective-C (or Swift). C++ codes worked by themselves.

I was able to create a working build using cmake -G Xcode ..

To run this build properly, I need to pass adjust "Arguments Passed On Launch" under edit scheme, and it runs perfectly.

Now comes the questions.

1) How do I link schemes so that it can while on an iOS app, I can initiate this C++ build?

2) Is there a way to programmatically send "Arguments Passed On Launch"?

3) Right now, C++ build uses cout to show the results in the main.cpp file. How do I generate output so that it can be sent to an Objective-C file?

Any small or big feedbacks or suggestions will be very appreciated. Thank you.

Upvotes: 0

Views: 123

Answers (1)

Richard Hodges
Richard Hodges

Reputation: 69892

The bad news: Objective C does not understand c++ header files.

The Good News: But Objective-C++ does

Assuming you want to keep the entire project as objective-C, then the trick is to provide objective-c++ wrappers around the c++ objects which have objective-c interfaces.

example:

wrapper.h

@interface MyWrapper : NSObject
- (void) something;
@end

wrapper.mm

#import "wrapper.h"
#include "cpp-class.hpp"

@implementation WyWrapper
{
    // private c++ data here
    std::unique_ptr<cpp_object> myobj_;
    // objects here unfortunately need to be default-constructible
}

- (id) init
{
    // because they are initialised here
    myobj_ = std::make_unique<cpp_object>();
}

- (void) something
{
    myobj_->do_something();
}
@end

Upvotes: 1

Related Questions