> ## Documentation Index
> Fetch the complete documentation index at: https://launchdarkly-preview.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

# C++ SDK reference (server-side)

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

> For the complete documentation index, see [llms.txt](/llms.txt). For the full documentation snapshot, see [llms-full.txt](/llms-full.txt).

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

<Note title="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:
</Note>

| Resource | Location |
| - | - |
| SDK API documentation | [SDK API docs](https://launchdarkly.github.io/cpp-sdks/libs/server-sdk/docs/html) |
| Supported SDK Versions | [C++ server SDK](../concepts/supported-versions#c-server-sdk) |
| GitHub repository | [cpp-sdks](https://github.com/launchdarkly/cpp-sdks) |
| Sample application | [C++ (server-side) (native)](https://github.com/launchdarkly/cpp-sdks/tree/main/examples/hello-cpp-server) ,  [C++ (server-side) (C binding)](https://github.com/launchdarkly/cpp-sdks/tree/main/examples/hello-c-server) |
| Published module | None |

## 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:

* [`tl/expected`](https://github.com/TartanLlama/expected)
* [`djarek/certify`](https://github.com/djarek/certify.git)

If you are planning to run the C++ SDK test suite, you will also need the following:

* [`nlohmann/json`](https://github.com/nlohmann/json)
* [`googletest`](https://github.com/google/googletest)

You do not need to run the test suite in order to use the SDK.

<Warning title="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](/sdk/client-side/c-c--).

  To learn more about LaunchDarkly's different SDK types, read [Choosing an SDK type](/sdk/concepts/client-side-server-side).
</Warning>

## Get started

<Note title="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.
</Note>

After you complete the [Getting started process](/home/getting-started), 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.

### Incorporate the SDK

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

#### Incorporate the SDK using cmake

To incorporate the SDK using `cmake`:

1. Clone the [GitHub repository](https://github.com/launchdarkly/cpp-sdks) as a subdirectory of your project.
2. Update your project's `CMakeLists.txt` to include the SDK repository:

<CodeGroup>
  ```bash title="Using add_subdirectory" theme={null}
         add_subdirectory(cpp-sdks)
  ```
</CodeGroup>

3. Link your project's target against the `launchdarkly::server` target:

<CodeGroup>
  ```bash title="Linking your target" theme={null}
         target_link_libraries(your-target PRIVATE launchdarkly::server)
  ```

  ```bash title="Linking your target, if you are using Redis" theme={null}
         # If you are using Redis for a persistent feature store,
         # ensure that the LD_BUILD_REDIS_SUPPORT option is ON.

         target_link_libraries(your-target PRIVATE launchdarkly::server_redis_source)
  ```
</CodeGroup>

#### Incorporate 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](https://github.com/launchdarkly/cpp-sdks/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](https://github.com/launchdarkly/cpp-sdks) and install the headers using `cmake`:

<CodeGroup>
  ```bash title="Install headers" theme={null}
         cmake --build .
         cmake --install .
  ```
</CodeGroup>

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

#### Include the Launch

Darkly headers

To include the LaunchDarkly SDK headers:

<CodeGroup>
  ```c title="C++ SDK v3 (native)" theme={null}
      #include <launchdarkly/server_side/client.hpp>
  ```

  ```c title="C++ SDK v3 (C binding)" theme={null}
      #include <launchdarkly/server_side/bindings/c/sdk.h>
  ```
</CodeGroup>

The C wrapper is included in the release binaries.

<Accordion title="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](https://github.com/launchdarkly/c-server-sdk) or download a release archive from the [GitHub Releases](https://github.com/launchdarkly/c-server-sdk/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.
</Accordion>

After cloning the repository, include the LaunchDarkly SDK headers:

<CodeGroup>
  ```cpp title="C++ SDK v3.0 (native)" theme={null}
      #include <launchdarkly/server_side/client.hpp>
  ```

  ```c title="C++ SDK v3.0 (C binding)" theme={null}
      #include <launchdarkly/server_side/bindings/c/sdk.h>
  ```

  ```c title="C SDK v2.x" theme={null}
      #include <launchdarkly/api.h>
  ```
</CodeGroup>

### About 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:

<CodeGroup>
  ```cpp title="Using launchdarkly namespace" theme={null}
      using namespace launchdarkly; # omitted in examples; assumed to be present
      auto config_builder = server_side::ConfigBuilder("YOUR_SDK_KEY");
      auto config = config_builder.Build();
  ```

  ```cpp title="Using launchdarkly::server_side namespace" theme={null}
      using namespace launchdarkly::server_side;
      auto config_builder = ConfigBuilder("YOUR_SDK_KEY");
      auto config = config_builder.Build();
  ```

  using namespace launchdarkly::server\_side;
  using namespace launchdarkly::server\_side::config::builders;

  auto config\_builder = ConfigBuilder("YOUR\_SDK\_KEY");
  config\_builder.Logging().Logging(
  LoggingBuilder::BasicLogging().Level(LogLevel::kDebug) // LoggingBuilder is in scope
  );

  ```cpp title="No namespace" theme={null}
      auto config_builder = launchdarkly::server_side::ConfigBuilder("YOUR_SDK_KEY");
      auto config = config_builder.Build();
  ```
</CodeGroup>

### Initialize the client

<Note title="The C++ (server-side) SDK uses an SDK key">
  The C++ (server-side) SDK uses an SDK key. Keys are specific to each project and environment. They are available from **Project settings**, on the **Environments** list. To learn more about key types, read [Keys](/sdk/concepts/client-side-server-side#keys).
</Note>

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:

<CodeGroup>
  ```cpp title="C++ SDK v3.0 (native)" theme={null}
      auto config_builder = server_side::ConfigBuilder("YOUR_SDK_KEY");
      auto config = config_builder.Build();
      if (!config) {
         /* an error occurred, config is not valid */
      }

      server_side::Client client(*config);
  ```

  ```c title="C++ SDK v3.0 (C binding)" theme={null}
      LDClientConfigBuilder builder = LDClientConfigBuilder_New("YOUR_SDK_KEY");

      LDClientConfig config;
      LDStatus status = LDClientConfigBuilder_Build(builder, &config);

      if (!LDStatus_Ok(status)) {
           /* an error occurred, config is not valid */
      }

      LDClientSDK client = LDClientSDK_New(config);
  ```

  ```c title="C SDK v2.x" theme={null}
      unsigned int maxwaitmilliseconds = 10 * 1000;
      struct LDConfig *config = LDConfigNew("YOUR_SDK_KEY");
      /* blocks on initialization */
      struct LDClient *client = LDClientInit(config, maxwaitmilliseconds);
  ```
</CodeGroup>

To learn more about the specific configuration options available for this SDK, read [`ConfigBuilder`](https://launchdarkly.github.io/cpp-sdks/libs/server-sdk/docs/html/classlaunchdarkly_1_1config_1_1shared_1_1builders_1_1ConfigBuilder.html).

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:

<CodeGroup>
  ```cpp title="C++ SDK v3.0 (native)" theme={null}
      client.StartAsync().wait_for(std::chrono::seconds(10));
  ```

  ```c title="C++ SDK v3.0 (C binding)" theme={null}
      unsigned int maxwait = 10 * 1000; /* 10 seconds */
      LDServerSDK_Start(client, maxwait, NULL);
  ```

  ```c title="C SDK v2.x" theme={null}
      /* In v2.x, the LDClientInit() call automatically blocks on initialization */
  ```
</CodeGroup>

To initialize asynchronously:

<CodeGroup>
  ```cpp title="C++ SDK v3.0 (native)" theme={null}
      client.StartAsync();
  ```

  ```c title="C++ SDK v3.0 (C binding)" theme={null}
      LDServerSDK_Start(client, LD_NONBLOCKING, NULL);
  ```
</CodeGroup>

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

<CodeGroup>
  ```cpp title="C++ SDK v3.0 (native)" theme={null}
      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. */
      }
  ```

  ```c title="C++ SDK v3.0 (C binding)" theme={null}
      LDServerSDK client = LDServerSDK_New(config);

      bool initialized_successfully;
      if (LDServerSDK_Start(client, maxwait, &initialized_successfully)) {
        /* The client's attempt to initialize succeeded or failed in the specified amount of time. */
        if (initialized_successfully) {
          /* Initialization succeeded. */
        else {
          /* Initialization failed. */
        }
      } else {
         /* The specified timeout was reached, but the client is still initializing. */
      }
  ```
</CodeGroup>

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.

### Evaluate a context

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

<CodeGroup>
  ```cpp title="C++ SDK v3.0 (native)" theme={null}
      auto context = ContextBuilder().Kind("user", "example-user-key").Name("Sandy").Build();

      bool show_feature = client.BoolVariation(context, "example-flag-key", false);

      if (show_feature) {
          // application code to show the feature
      } else {
          // the code to run if the feature is off
      }
  ```

  ```c title="C++ SDK v3.0 (C binding)" theme={null}
      LDContextBuilder context_builder = LDContextBuilder_New();
      LDContextBuilder_AddKind(context_builder, "user", "example-user-key");
      LDContextBuilder_Attributes_SetName(context_builder, "user", "Sandy");
      LDContext context = LDContextBuilder_Build(context_builder);

      bool show_feature = LDServerSDK_BoolVariation(client, context, "example-flag-key", false);

      if (show_feature) {
          // application code to show the feature
      } else {
          // the code to run if the feature is off
      }
  ```

  ```c title="C SDK v2.x" theme={null}
      user = LDUserNew("example-user-key");
      LDUserSetName(user, "Sandy");

      LDBoolean show_feature = LDBoolVariation(client, user, "example-flag-key", false, NULL);
      if (show_feature) {
          // application code to show the feature
      } else {
          // the code to run if the feature is off
      }
  ```
</CodeGroup>

## 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](/sdk/features/shutdown#c-server-side).

## Supported features

This SDK supports the following features:

* [Anonymous contexts and users](/sdk/features/anonymous#c-server-side)
* [Configuration](/sdk/features/config#c-server-side), including
  * [Application metadata configuration](/sdk/features/app-config#c-server-side)
  * [Service endpoint configuration](/sdk/features/service-endpoint-configuration/server-side#c-server-side)
* [Context configuration](/sdk/features/context-config/server-side#c-server-side)
* [Evaluating flags](/sdk/features/evaluating#c-server-side)
* [Flag evaluation reasons](/sdk/features/evaluation-reasons/server-side#c-server-side)
* [Flushing events](/sdk/features/flush#c-server-side)
* [Getting all flags](/sdk/features/all-flags#c-server-side)
* [Identifying and changing contexts](/sdk/features/identify#c-server-side)
* [Logging configuration](/sdk/features/logging#c-server-side)
* [Monitoring SDK status](/sdk/features/monitoring#c-server-side)
* [Offline mode](/sdk/features/offline-mode#c-server-side)
* [Private attributes](/sdk/features/private-attributes/server-side#c-server-side)
* [Reading flags from a file](/sdk/features/flags-from-files#c-server-side)
* [Relay Proxy configuration](/sdk/features/relay-proxy-configuration)
  * [Using proxy mode](/sdk/features/relay-proxy-configuration/proxy-mode#c-server-side)
  * [Using daemon mode](/sdk/features/relay-proxy-configuration/daemon-mode#c-server-side)
* [Sending custom events](/sdk/features/events#c-server-side)
* [Shutting down](/sdk/features/shutdown#c-server-side)
* [Storing data](/sdk/features/storing-data#c-server-side)
* [Test data sources](/sdk/features/test-data-sources#c-server-side)
