No results for ""
EXPAND ALL
  • Home
  • API docs

GIVE DOCS FEEDBACK

C++ SDK reference (server-side)

Read time: 8 minutes
Last edited: Mar 04, 2024
Version 3 of the C++ (server-side) SDK replaces users with contexts

A context is a generalized way of referring to the people, services, machines, or other resources that encounter feature flags in your product. Contexts replace another data object in LaunchDarkly: "users."

Code samples on this page are from the two most recent SDK versions where they differ. To learn more about upgrading, read C++ (server-side) SDK 2.x to 3.0 migration guide and Best practices for upgrading users to contexts.

Overview

This topic documents how to get started with the server-side C++ SDK, and links to reference information on all of the supported features.

SDK quick links

LaunchDarkly's SDKs are open source. In addition to this reference guide, we provide source, API reference documentation, and a sample application:

ResourceLocation
SDK API documentationSDK API docs
GitHub repositorycpp-sdks
Sample applicationC++ (server-side) (native)
C++ (server-side) (C binding)
Published moduleNone

Prerequisites and dependencies

To use the C++ SDK, you must have the following prerequisites installed on your build machine:

  • Windows or a POSIX environment (Linux, OSX, BSD)
  • cmake, version 3.19 or above
  • boost, version 1.81 or above
  • openssl, version 1.1 or above
  • libpthread, if you are using a POSIX environment

To 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.

For use in server-side applications only

This SDK is intended for use in multi-user C++ server applications. If you have a C++ application and want to set up LaunchDarkly in a mobile, desktop, or embedded application, read the client-side C++ SDK reference.

To learn more about LaunchDarkly's different SDK types, read Client-side, server-side, and edge SDKs.

Getting started

Version 3 of the C++ (server-side) SDK is a native C++ library

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.

After you complete the Getting Started process, follow these instructions to start using the LaunchDarkly SDK in your C application.

The following sections explain how to install and configure the SDK, and then to verify its connection to LaunchDarkly by fetching flag configuration information.

Incorporating the SDK

You can incorporate the SDK by building from source using cmake, or by using pre-built artifacts. Then, include the LaunchDarkly headers.

Incorporating the SDK using cmake



To incorporate the SDK using cmake:

  1. Clone the GitHub repository as a subdirectory of your project.
  2. Update your project's CMakeLists.txt to include the SDK repository:
    add_subdirectory(cpp-sdks)
  3. Link your project's target against the launchdarkly::server target:
    target_link_libraries(your-target PRIVATE launchdarkly::server)

Incorporating the SDK using prebuilt artifacts



The C++ (server-side) SDK releases include 64-bit static and dynamic libraries for Linux, Mac, and Windows.

To incorporate the SDK using prebuilt artifacts:

  1. Download the correct release for your platform from the GitHub Releases page.
  2. Ensure the SDK's headers are installed on the build system. One way to do this is to clone the GitHub repository and install the headers using cmake:
    cmake --build .
    cmake --install .

You can now reference the installed headers and link against the prebuilt libraries.

Including the LaunchDarkly headers



To include the LaunchDarkly SDK headers:

#include <launchdarkly/server_side/client.hpp>

The C wrapper is included in the release binaries.

Expand for how to install the SDK if you are using v2.x

Here's how to install the SDK:

  1. Clone the GitHub repository or download a release archive from the GitHub Releases page.
  2. Install the SDK locally.
  • If you use cmake, the build system will expect that boost and openssl exist on the system. The cmake configuration exports the target ldclientapi.
  • If you don't use 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.

After cloning the repository, include the LaunchDarkly SDK headers:

#include <launchdarkly/server_side/client.hpp>

Understanding the SDK namespaces

SDK components common to the C++ (server-side) SDK v3.0 and the C++ (client-side) SDK v3.0 exist within the top-level launchdarkly namespace. Server-side components exist within launchdarkly::server_side.

Individual server-side config builders, for example the LoggingBuilder, are within the launchdarkly::server_side::config::builders namespace. To reduce verbosity of configuration code, you may bring this namespace into scope.

To keep the examples in our documentation concise, we assume symbols in the top-level launchdarkly namespace are visible. You can bring launchdarkly, launchdarkly::server_side, launchdarkly::server_side::config::builders, or all of these into scope, or you can refer to SDK components by their fully-qualified names.

For example:

using namespace launchdarkly; # omitted in examples; assumed to be present
auto config_builder = server_side::ConfigBuilder("sdk-key-123abc");
auto config = config_builder.Build();

Initializing the client and fetching flag configuration

The C++ (server-side) SDK uses an SDK key

The C++ (server-side) SDK uses an SDK key. Your environment's SDK key is available in the Projects tab of your Account settings page. To learn more about key types, read Keys.

After you install and import the SDK, create a single, shared instance of Client. To create a client instance, you need your environment's SDK key to authorize your application to connect to a particular environment within LaunchDarkly.

Here's how to configure the SDK key and create the client:

auto config_builder = server_side::ConfigBuilder("sdk-key-123abc");
auto config = config_builder.Build();
if (!config) {
/* an error occurred, config is not valid */
}
server_side::Client client(*config);

To learn more about the specific configuration options available for this SDK, read ConfigBuilder.

Next, construct the client and call StartAsync to initiate a request to the LaunchDarkly service and fetch the feature flag settings.

Because the client initializes asynchronously, you may choose to block until the client is ready.

To block on initialization:

client.StartAsync().wait_for(std::chrono::seconds(10));

To initialize asynchronously:

client.StartAsync();

Whether you block on initialization or initialize asynchronously, you can also examine the result to determine if initialization succeeded. Here's how:

server_side::Client client(*config);
auto start_result = client.StartAsync();
if (auto const status = start_result.wait_for(maxwait); status == std::future_status::ready) {
/* The client's attempt to initialize succeeded or failed in the specified amount of time. */
if (start_result.get()) {
/* Initialization succeeded. */
} else {
/* Initialization failed. */
}
} else {
/* The specified timeout was reached, but the client is still initializing. */
}

If you block on initialization, the initialization call blocks up to the time maxwait. If you request a feature flag before initialization completes, you will receive the fallback value you defined in your variation call.

Now you can check which variation a specific context should receive for a given feature flag. Here's how:

auto context = ContextBuilder().Kind("user", "user-key-123abc").Name("Sandy").Build();
bool show_feature = client.BoolVariation(context, "flag-key-123abc", false);
if (show_feature) {
// application code to show the feature
} else {
// the code to run if the feature is off
}

Shut down the client

Lastly, shut down the client when your application terminates.

In the C++ SDK v3.0, the SDK will be automatically closed. If you are using the C binding, or if you are working with earlier versions of the SDK, you must specifically close the client. To learn more, read Shutting down.

Supported features

This SDK supports the following features: