1

I am trying to run some C++ code with react-native so I created an Objective-C++ bridge that calls my c++ code. My app compiles just great, and it runs with no errors, but I still cannot get the NativeModules to appear within JS/React

//RTCHelloWorld.mm 
#import "RTCHelloWorld.h"
#include "hello_world.hpp"
#include <iostream>
  
@implementation RTCHelloWorld
RCT_EXPORT_MODULE();
  
RCT_EXPORT_METHOD(say_hello) {
std::cout << example::say_hello() << std::endl;
RCTLogInfo(@"we are saying hello");
}
@end
//RTCHelloWorld.h
#ifndef RNCPP_CODE
#define RNCPP_CODE

#import <React/RCTBridgeModule.h>
#import <React/RCTBridge.h>
#import <React/RCTUIManager.h>
#import <React/RCTEventDispatcher.h>
#import <React/RCTLog.h>
#import <React/RCTUtils.h>
#import <React/UIView+React.h>
#import <React/RCTConvert.h>

#import <Foundation/Foundation.h>

#ifdef __cplusplus
 #import "hello_world.hpp"
#endif

@interface RTCHelloWorld : NSObject <RCTBridgeModule>

@end

#endif

//hello_world.hpp
#ifndef EXAMPLE_H
#define EXAMPLE_H

#include <string>

namespace example {
    extern long multiply(long a, long b);
    extern std::string say_hello();
}

#endif /* EXAMPLE_H */

//hello_world.cpp
#include "hello_world.hpp"
#include <string>

namespace example {
    long multiply(long a, long b) {
      return a * b;
    }

    std::string say_hello(){
      std::string greeting = "Hello World From c++ code";
      return greeting;
    }
}


And here is my very simple JS/React code

//App.js
import React from 'react';
import { View, Text, NativeModules } from 'react-native';

const iOSApp = () => {
  console.log("native modules are", NativeModules)
  return (
      <>
          <View>
              <Text> Hello From ios</Text>    
          </View>
      </>
  );
};
export default iOSApp;

It all compiles well, but for whatever reason nothing is inside of the NativeModules object. It is completely empty. When I console.log the NativeModules object I get the following output, which is empty. Am I not exporting it correctly? Am I not importing it correctly? Xcode compiles with no error so I am stuck.

2020-12-03 12:08:51.711085-0800 RNCpp[8419:225308] [javascript] 'native modules are', {}

I am new to Swift/Objective-C/C++ so I am just copying and pasting from a half dozen examples on the internet and from the React Native docs. So excuse me if I am missing something obvious.

ChannelJuanNews
  • 406
  • 3
  • 11
  • I have the same issue, I was assuming this was an M1 issue - did you ever resolve yours? https://stackoverflow.com/questions/71613550/react-native-nativemodules-not-importing-on-m1-silicon-mac-returning-empty-o – David Pears Mar 25 '22 at 08:04

0 Answers0