• Home
  • Integrations
  • SDKs
  • Guides
  • API docs
    No results for ""


    C/C++ SDK reference (server-side)

    Read time: 2 minutes
    Last edited: Mar 16, 2023


    This topic documents how to get started with the server-side C/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:

    SDK API documentationSDK API docs
    GitHub repositoryc-server-sdk
    Sample applicationC (server-side)
    Published moduleNone
    For use in server-side applications only

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

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

    Getting started

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

    Unlike other LaunchDarkly SDKs, the C/C++ SDK has no installation steps. To get started, clone this repository or download a release archive from the GitHub Releases page. You can use CMakeLists.txt in this repository as a starting point for integrating this SDK into your application.

    After cloning the repository, include the LaunchDarkly SDK headers:

    #include "launchdarkly/api.h"

    Configure logging, and call the global initialization function. You must call these functions before you perform any other operations. LaunchDarkly provides a predefined convenience logger.

    Here's how:

    LDConfigureGlobalLogger(LD_LOG_INFO, LDBasicLogger);

    After you install and import the SDK, create a single, shared instance of LDClient. Specify your SDK key here to authorize your application to connect to a particular environment within LaunchDarkly.

    LDClient must be a singleton

    It's important to make LDClient 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 LDClient for each. In this situation, the clients operate independently. For example, they do not share a single connection to LaunchDarkly.

    Calling LDClientInit initiates a remote call to the LaunchDarkly service to fetch feature flags. This call blocks up to the time defined by maxwaitmilliseconds. If you request a feature flag before the client has completed initialization, you will receive the default flag value.

    Here is an example:

    unsigned int maxwaitmilliseconds = 10 * 1000;
    struct LDConfig *config = LDConfigNew("sdk-key-123abc");
    struct LDUser *user = LDUserNew("user-key-123abc");
    struct LDClient *client = LDClientInit(config, maxwaitmilliseconds);

    You can use client to check which variation a particular user will receive for a given feature flag.

    Here's how:

    user = LDUserNew("user-key-123abc");
    LDUserSetName(user, "Sandy");
    LDBoolean show_feature = LDBoolVariation(client, user, flag-key-123abc, false, NULL);
    if (show_feature) {
    // application code to show the feature
    } else {
    // the code to run if the feature is off

    If the SDK might be able to execute your flag evaluation before client initializes, wrap your call in LDClientIsInitialized(client):

    if (LDClientIsInitialized(client)) {
    // flag evaluation goes here

    Shutting down

    Lastly, shut down the client when your application terminates. To learn more, read Shutting down.

    Supported features

    This SDK supports the following features: