Join us at MongoDB.local London on 7 May to unlock new possibilities for your data. Use WEB50 to save 50%.
Register now >
Docs Menu
Docs Home
/ /
SDK de Dispositivo Atlas

Instalar el SDK de C++

El SDK de Atlas Device para C++ permite a las aplicaciones cliente escritas en C++ acceder a los datos almacenados en los dispositivos y sincronizar datos con Atlas. Esta página detalla cómo instalar el SDK de C++ en tu Proyecto y empezar a usarlo.

  • Estándar mínimo de C++: C++17.

  • Para desarrollo en macOS: Xcode 11.x o posterior.

  • For development on Windows: Microsoft Visual C++ (MSVC).

  • De lo contrario, recomendamos git y CMake.

Tip

Atlas Device SDK and Realm

El SDK utiliza la base de datos Realm Core para la persistencia de los datos del dispositivo. Al instalar el SDK de C++, los nombres de los paquetes reflejan la nomenclatura de Realm.

Al desarrollar con Xcode, puedes usar Swift Package Manager (SPM) para instalar realm-cpp.

1

In Xcode, select File > Add Packages....

2

Copy and paste the following into the search/input box.

https://github.com/realm/realm-cpp
3

Bajo Package ProductSeleccione realm-cpp-sdk. En Add to Target, seleccione el destino al que desea agregar el SDK. Por ejemplo, el destino podría ser el ejecutable principal de su aplicación. Haga clic en Add Package.

Puede utilizar CMake con el módulo FetchContent para administrar el SDK y sus dependencias en su proyecto C++.

Crea o modifica tu CMakeLists.txt en el directorio raíz de tu proyecto:

  1. Agrega Include(FetchContent) para incluir el módulo FetchContent en la compilación de tu proyecto.

  2. Utilice FetchContent_Declare para localizar la dependencia del SDK y especificar la etiqueta de versión que desea utilizar.

  3. Utilice el comando FetchContent_MakeAvailable() para verificar si se han completado las dependencias nombradas y, si no es así, completarlas.

  4. Finalmente, target_link_libraries() vincula la dependencia del SDK a su ejecutable de destino.

To get the most recent version tag, refer to the releases on GitHub: realm/realm-cpp.

Set the minimum C++ standard to 17 with set(CMAKE_CXX_STANDARD 17).

In a Windows install, add the required compiler flags listed below.

cmake_minimum_required(VERSION 3.15)
project(MyDeviceSDKCppProject)
# Minimum C++ standard
set(CMAKE_CXX_STANDARD 17)
# In a Windows install, set these compiler flags:
if(MSVC)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /Zc:preprocessor /bigobj")
endif()
# Include the FetchContent module so you can download the C++ SDK
Include(FetchContent)
# Declare the version of the C++ SDK you want to download
FetchContent_Declare(
cpprealm
GIT_REPOSITORY https://github.com/realm/realm-cpp.git
GIT_TAG v1.0.0
)
# The MakeAvailable command ensures the named dependencies have been populated
FetchContent_MakeAvailable(cpprealm)
# Create an executable target called myApp with the source file main.cpp
add_executable(myApp main.cpp)
target_link_libraries(myApp PRIVATE cpprealm)

Ejecute CMake en un directorio gitignored, como build, para generar las configuraciones de compilación que luego puede usar para compilar su aplicación:

# build/ is in .gitignore
mkdir build
cd build
cmake .. # Create Makefile by reading the CMakeLists.txt in the parent directory (../)
make # Actually build the app

You can use CMake to generate more than simple Makefiles by using the -G flag. See the CMake documentation for more information.

Make the C++ SDK available in your code by including the cpprealm/sdk.hpp header in the translation unit where you want to use it:

#include <cpprealm/sdk.hpp>

The C++ SDK supports building Android apps. To build an Android app:

  • Add <uses-permission android:name="android.permission.INTERNET" /> to your AndroidManifest.xml

  • Agregue el subdirectorio del SDK de C++ a CMakeLists.txt de su biblioteca nativa y vincúlelo como una biblioteca de destino:

    set(CMAKE_CXX_STANDARD 17)
    add_subdirectory("realm-cpp")
    ...
    target_link_libraries(
    # Specifies the target library.
    myapplication
    # make sure to link the C++ SDK.
    cpprealm
    )
  • Ensure that the git submodules are initialized inside of the realm-cpp folder before building.

  • When instantiating the database or the SDK App, you must pass the filesDir.path as the path parameter in the respective constructor or database open template.

Para ver un ejemplo de cómo usar el C++ SDK en una aplicación Android, consulta la aplicación Android RealmExample en el repositorio de GitHub realm-cpp.

Específicamente, consulte los archivos MainActivity.kt y native-lib.cpp en la aplicación de ejemplo de Android para obtener ejemplos de código.

Next

Bienvenido a la Docs de Atlas Device SDK

En esta página