Add external C++ libraries to react native Turbo app

586 views Asked by At

I am trying out the new react-native Turbo feature. The calls to the cpp code works as the documentation mentions.

Is there a way to add external C++ libraries like cryptopp. I have my github repo here RNTurbo.

2

There are 2 answers

0
Kirill Novikov On

Try to build your library with https://github.com/callstack/react-native-builder-bob - with interactive CLI you can generate a proper project structure with c++ libraries support for both platforms ios and android

enter image description here

0
Ludescher On

I had the same issue and it took me quite some time to get a working cmake configuration.

If you follow the instructions here you should end up with the following file structure:

CxxTurboModulesGuide
├── android
│   └── app
│       ├── src
│       │   └── main
│       │       └── jni
│       │           ├── CMakeLists.txt
│       │           └── OnLoad.cpp
│       └── build.gradle (updated)
├── ios
│   └── CxxTurboModulesGuide
│       └── AppDelegate.mm (updated)
├── js
│   └── App.tsx|jsx (updated)
└── tm
    ├── CMakeLists.txt
    ├── NativeSampleModule.h
    ├── NativeSampleModule.cpp
    ├── NativeSampleModule.ts|js
    └── TurboModules.podspec

At this point everything should build and work as expected.

And now you can add a library, for example this very simple one.

In the tm directory you create a new folder with the name of the library or you could just clone the desired library into the tm folder.

For the simplicity's sake we use the MathLibrary sample from Microsoft.

Now we and up with the following file structure:

CxxTurboModulesGuide
├── android
│   └── app
│       ├── src
│       │   └── main
│       │       └── jni
│       │           ├── CMakeLists.txt
│       │           └── OnLoad.cpp
│       └── build.gradle (updated)
├── ios
│   └── CxxTurboModulesGuide
│       └── AppDelegate.mm (updated)
├── js
│   └── App.tsx|jsx (updated)
└── tm
    ├── MathLibrary
    │   ├── CMakeLists.txt
    │   ├── MathLibrary.cpp
    │   └── MathLibrary.h
    ├── CMakeLists.txt
    ├── NativeSampleModule.h
    ├── NativeSampleModule.cpp
    ├── NativeSampleModule.ts|js
    └── TurboModules.podspec

The tm/MathLibrary/CMakeLists.txt file looks like this:

cmake_minimum_required(VERSION 3.0.0)
project(MathLibrary VERSION 0.1.0)

set(CMAKE_CXX_STANDARD 17)

add_library(MathLibrary MathLibrary.cpp)

And the main tm/CMakeLists.txt file looks like this:

cmake_minimum_required(VERSION 3.13)

set(CMAKE_VERBOSE_MAKEFILE on)

add_compile_options(
        -fexceptions
        -frtti
        -std=c++17
)

include_directories(${CMAKE_CURRENT_SOURCE_DIR}/MathLibrary) # <=== add new
add_subdirectory(MathLibrary) # <=== add new

file(GLOB tm_SRC CONFIGURE_DEPENDS *.cpp)

add_library(tm STATIC ${tm_SRC})

target_include_directories(tm PUBLIC .)
target_include_directories(react_codegen_AppSpecs PUBLIC .)

target_link_libraries(tm
        MathLibrary # <=== add new
        jsi
        react_nativemodule_core
        react_codegen_AppSpecs
)

And that's it, now you should have all the necessary files in place to build and use c++ libraries.