C++ SDK reference (client-side)
C++ SDK reference (client-side)
C++ SDK reference (client-side)
This topic documents how to get started with the client-side C++ SDK, and links to reference information on all of the supported features.
LaunchDarkly’s SDKs are open source. In addition to this reference guide, we provide source, API reference documentation, and sample applications:
To use the C++ SDK, you must have the following prerequisites installed on your build machine:
cmake, version 3.19 or aboveboost, version 1.81 or aboveopenssl, version 1.1 or abovelibpthread, if you are using a POSIX environmentTo build the C++ SDK, you must have the following dependencies. These are automatically fetched by cmake during the build process:
If you are planning to run the C++ SDK test suite, you will also need the following:
You do not need to run the test suite in order to use the SDK.
This SDK is intended for use in single-user mobile, desktop, and embedded applications. If you have a C/C++ application and want to set up LaunchDarkly on the server-side, read the server-side C/C++ SDK reference.
To learn more about LaunchDarkly’s different SDK types, read Choosing an SDK type.
Previous versions of this SDK were written in C, with a C++ wrapper available. In version 3.0 and higher, this SDK is written in C++, with a C wrapper available. The code samples below show all options, where applicable.
The following sections explain how to install and configure the SDK, and then to verify its connection to LaunchDarkly by fetching flag configuration information for a specific context.
After you complete the Get started process, follow these instructions to start using the LaunchDarkly C++ SDK:
You can incorporate the SDK by building from source using cmake, or by using pre-built artifacts. Then, include the LaunchDarkly headers.
To incorporate the SDK using cmake:
CMakeLists.txt to include the SDK repository:
launchdarkly::client target:
The C++ (client-side) SDK releases include 64-bit static and dynamic libraries for Linux, Mac, and Windows.
To incorporate the SDK using prebuilt artifacts:
cmake:
You can now reference the installed headers and link against the prebuilt libraries.
Here’s how to install the SDK:
cmake, the build system will expect that boost and openssl exist on the system. The cmake configuration exports the target ldclientapi.cmake and you cannot use LaunchDarkly’s artifacts, use cmake install to install the SDK in directory you choose. This copies the required headers, and binaries equivalent to LaunchDarkly’s release bundles.To include the LaunchDarkly SDK headers:
The C wrapper is included in the release binaries.
SDK components common to the C++ (client-side) SDK v3.0 and the C++ (server-side) SDK v3.0 exist within the top-level launchdarkly namespace. Client-side components exist within launchdarkly::client_side.
To keep the examples in our documentation concise, we assume symbols in the top-level launchdarkly namespace are visible. You can bring launchdarkly, launchdarkly::client_side, or both into scope, or you can refer to SDK components by their fully-qualified names.
For example:
After you install the SDK, initialize a single shared Client. To create a client instance, you need your environment’s mobile key and the context for which you want to evaluate flags. The mobile key authorizes your application to connect to a particular environment within LaunchDarkly.
The C++ (client-side) SDK uses a mobile key. Keys are specific to each project and environment. They are available on the SDK keys page under Settings. To learn more about key types, read Keys.
Mobile keys are not secret and you can expose them in your client-side code without risk. However, never embed a server-side SDK key into a client-side application.
Here’s how to configure the mobile key and define the context:
To learn more about the specific configuration options available in this SDK, read ConfigBuilder.
Next, construct the client and call StartAsync to initiate a remote call to the LaunchDarkly service and fetch the feature flag settings for a given context. The StartAsync method returns a future. We strongly recommend using StartAsync with a method that takes a timeout, such as wait_for.
To block on initialization for a specific amount of time:
You can also initialize the client asynchronously:
If you request a feature flag before initialization completes, you will receive the fallback value you defined in your variation call. Whether you block on initialization or initialize asynchronously, you can examine the result to determine if initialization succeeded.
Here’s how:
You may also choose to block until the client is ready by using StartAsync with wait rather than with wait_for. However, we strongly discourage this. If you block indefinitely, your application will hang if the client cannot connect to LaunchDarkly. To learn more, read StartAsync. If you do choose to block indefinitely for client initialization, you can listen to status updates. To learn more, read Monitoring SDK status.
It’s important to make Client a singleton for each LaunchDarkly project. The client instance maintains internal state that allows LaunchDarkly to serve feature flags without making any remote requests. Do not instantiate a new client with every request.
If you have multiple LaunchDarkly projects, you can create one Client for each. In this situation, the clients operate independently. For example, they do not share a single connection to LaunchDarkly.
After you create the client, you can use it to check which variation a particular context will receive for a given feature flag.
Here’s how:
You must make feature flags available to mobile SDKs before the SDK can evaluate those flags. If an SDK tries to evaluate a feature flag that is not available, the context will receive the fallback value for that flag.
To make a flag available to this SDK, check the SDKs using Mobile key checkbox during flag creation, or toggle on the option in the flag’s right sidebar. To make all of a project’s flags available to this SDK by default, check the SDKs using Mobile key checkbox on your project’s Flag settings page.
Shut down the client when your application terminates. To learn more, read Shutting down.
This SDK supports the following features: