0

I am trying running c++ file on plugin IOS (not ios app). First I create cpp file

Greeting.cpp

#include "Greeting.hpp"

Greeting::Greeting() {
    greeting = "Hello C++!";
}

std::string Greeting::greet() {
    return greeting;
}

Then I create Greeting.hpp

#ifndef Greeting_hpp
#define Greeting_hpp

#include <stdio.h>
#include <string>

class Greeting {
    std::string greeting;
public:
    Greeting();
    std::string greet();
};
#endif /* Greeting_hpp */

Then I import it into object c file AgoraRtcEnginePlugin.m

#import "Greeting.hpp"

NSString* newTitle = [NSString stringWithCString:greeting.greet().c_str() encoding:[NSString defaultCStringEncoding]];
        result(newTitle);

But when I complile It always throws errors

/agora-flutter-sdk/ios/Classes/Greeting.hpp:13:10: fatal error: 'string' file not found
#include <string>
kaylum
  • 13,833
  • 2
  • 22
  • 31
Bình Trương
  • 380
  • 1
  • 13

1 Answers1

1

The problem you have is that you're including C++ in your Objective-C (AgoraRtcEnginePlugin.m = .m extension is Objective-C) file. Objective-C is a layer on top of C and Objective-C is a strict superset of C.

You can't do this unless you use extern "C", etc. There are questions about this:

You're not forced to create an C API for you class, because there's also Objective-C++ (.mm extension) and you can use C++ directly in these files.

zrzka
  • 20,249
  • 5
  • 47
  • 73