|
| 1 | +# Realm CPP Examples |
| 2 | + |
| 3 | +This project contains the C++ code examples for the Realm C++ SDK and |
| 4 | +their unit and UI tests. |
| 5 | + |
| 6 | +The first time you set up the project, run through **First-time Setup**. |
| 7 | + |
| 8 | +When you're already set up but you have edited source code, start with |
| 9 | +**Build the Project**. |
| 10 | + |
| 11 | +If you just want to run the tests, go to **Run the Tests**. |
| 12 | + |
| 13 | +## First-time Setup |
| 14 | + |
| 15 | +This project uses the Realm CPP SDK as a git submodule. You must ensure |
| 16 | +your machine has access to the version of the submodule used in the project. |
| 17 | +To pull in the submodule and its dependencies, from this `/examples/cpp` |
| 18 | +directory: |
| 19 | + |
| 20 | +```shell |
| 21 | +git submodule update --init --recursive |
| 22 | +``` |
| 23 | + |
| 24 | +The project uses [CMake](https://cmake.org/) to create build files (Makefile, .xcodeproj...) for the |
| 25 | +project. To check if you have CMake installed: |
| 26 | + |
| 27 | +```shell |
| 28 | +cmake |
| 29 | +``` |
| 30 | + |
| 31 | +If you do have CMake installed, you should see the help documentation. If |
| 32 | +you don't, you can install it with brew or by downloading it directly from CMake. |
| 33 | + |
| 34 | +```shell |
| 35 | +brew install cmake |
| 36 | +``` |
| 37 | + |
| 38 | +Run CMake in a gitignored directory, such as build, to generate the build |
| 39 | +configurations that you can then use to compile your app. If this is the |
| 40 | +first time you're setting up the project, you'll need to make the `build` |
| 41 | +directory. |
| 42 | + |
| 43 | +```shell |
| 44 | +# build/ is in .gitignore |
| 45 | +mkdir build |
| 46 | +cd build |
| 47 | +``` |
| 48 | + |
| 49 | +## Build the Project |
| 50 | + |
| 51 | +From `/examples/cpp/build`, run `cmake` to create a Makefile by reading the |
| 52 | +`CMakeLists.txt` in the parent directory. |
| 53 | + |
| 54 | +```shell |
| 55 | +cmake ../ |
| 56 | +``` |
| 57 | + |
| 58 | +You can `ls` to see that a Makefile has been generated. Then, build the app: |
| 59 | + |
| 60 | +```shell |
| 61 | +make |
| 62 | +``` |
| 63 | + |
| 64 | +The first time you run `make`, the process will take some time because it's |
| 65 | +doing a full build. On subsequent runs, `make` does incremental builds, so |
| 66 | +rebuilding will be faster. |
| 67 | + |
| 68 | +When the build is complete, `ls` should reveal an `examples` executable |
| 69 | +at the root of the build directory. |
| 70 | + |
| 71 | +## Run the Tests |
| 72 | + |
| 73 | +To run the tests, from the `build` directory: |
| 74 | + |
| 75 | +```shell |
| 76 | +./examples |
| 77 | +``` |
| 78 | + |
| 79 | +## Update the Realm SDK Version |
| 80 | + |
| 81 | +Enter the submodule directory: |
| 82 | + |
| 83 | +```shell |
| 84 | +cd realm-cpp |
| 85 | +``` |
| 86 | + |
| 87 | +You're now in a remote of `realm-cpp`. Use regular `git` commands to fetch |
| 88 | +the version of the repository that you want to use. |
| 89 | + |
| 90 | +Alternately, if you don't want to get a specific version of the `realm-cpp` |
| 91 | +repo, but just want to update it to the latest commit, you can: |
| 92 | + |
| 93 | +```shell |
| 94 | +git submodule update --remote |
| 95 | +``` |
| 96 | + |
| 97 | +Then, when you `cd` back out of the `realm-cpp` directory and check the |
| 98 | +git status, you should see: |
| 99 | + |
| 100 | +```shell |
| 101 | +modified: realm-cpp (new commits, modified content) |
| 102 | +``` |
| 103 | + |
| 104 | +Add and commit these changes like you would any other changes. This preserves |
| 105 | +the HEAD of the submodule. |
| 106 | + |
| 107 | +To use the updated repository in the CPP examples project: |
| 108 | + |
| 109 | +```shell |
| 110 | +git submodule update --init --recursive |
| 111 | +``` |
| 112 | + |
| 113 | +For best results, delete everything in the `build` folder and then build |
| 114 | +the project again before running the tests. |
| 115 | + |
| 116 | +## Add a New Test File |
| 117 | + |
| 118 | +To add a new test file, create a file with a `.cpp` extension in `examples/cpp/`. |
| 119 | + |
| 120 | +Include the relevant headers. They probably look something like: |
| 121 | + |
| 122 | +```cpp |
| 123 | +#include <catch2/catch_test_macros.hpp> |
| 124 | +#include <chrono> |
| 125 | +#include <string> |
| 126 | +#include <thread> |
| 127 | +#include <cpprealm/sdk.hpp> |
| 128 | +``` |
| 129 | + |
| 130 | +Write the test code. |
| 131 | + |
| 132 | +Open `CMakeLists.txt` and add the new file name to the `add_executable` at ln 20: |
| 133 | + |
| 134 | +```txt |
| 135 | +add_executable(examples examples.cpp my_new_test_file.cpp) |
| 136 | +``` |
| 137 | + |
| 138 | +Run `cmake` and `make` to build the project again, and then run the tests. |
| 139 | + |
| 140 | +## Add a New Example |
| 141 | + |
| 142 | +Find the relevant test case file for the section or category you wish to write |
| 143 | +an example for. |
| 144 | + |
| 145 | +Define a new `TEST_CASE`. This should have a unique string name - as in the |
| 146 | +example below `"add a dog object to a realm"`, and a `"[test]"` tag. |
| 147 | + |
| 148 | +```cpp |
| 149 | +TEST_CASE("add a dog object to a realm", "[test]") { |
| 150 | + auto dog = Dog { .name = "Rex", .age = 1 }; |
| 151 | + |
| 152 | + std::cout << "dog: " << dog << "\n"; |
| 153 | + |
| 154 | + auto realm = realm::open<Person, Dog>(); |
| 155 | + |
| 156 | + realm.write([&realm, &dog] { |
| 157 | + realm.add(dog); |
| 158 | + }); |
| 159 | +} |
| 160 | +``` |
| 161 | +
|
| 162 | +Run `cmake` and `make` to build the project again, and then run the tests. |
| 163 | +
|
| 164 | +## Build and Run for Xcode |
| 165 | +
|
| 166 | +If you want to use Xcode for breakpoints and debugging, you can build |
| 167 | +the project to run it in Xcode. |
| 168 | +
|
| 169 | +From the `/build` directory, run CMake with a `-G Xcode` flag: |
| 170 | +
|
| 171 | +```shell |
| 172 | +cmake ../ -G Xcode |
| 173 | +``` |
| 174 | + |
| 175 | +Note: If you have previously run `CMake` for this project, you may see an |
| 176 | +error: |
| 177 | + |
| 178 | +```shell |
| 179 | +CMake Error: Error: generator : Xcode |
| 180 | +Does not match the generator used previously: Unix Makefiles |
| 181 | +Either remove the CMakeCache.txt file and CMakeFiles directory or choose a different binary directory. |
| 182 | +``` |
| 183 | + |
| 184 | +Run this from a clean build directory, or remove the specified files. On a |
| 185 | +successful build, you should see `examples.xcodeproj` in the `/build` directory. |
| 186 | + |
| 187 | +Open the project in Xcode. |
| 188 | + |
| 189 | +Inside the project, you'll see all the linked files, but the source files |
| 190 | +for the examples are located in: |
| 191 | + |
| 192 | +`examples/Source Files` |
| 193 | + |
| 194 | +To build and run these source files in Xcode, select the `examples` executable |
| 195 | +target and press the `Start` (▶) button. You should see the results and any |
| 196 | +print output in the console. |
0 commit comments