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

EDIT ON GITHUB

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

Read time: 2 minutes
Last edited: Aug 12, 2022

Overview

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:

ResourceLocation
SDK API documentationSDK API docs
GitHub repositoryc-server-sdk
Sample applicationhello-c-server
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);
LDGlobalInit();

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

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("YOUR_SDK_KEY");
struct LDUser *user = LDUserNew("YOUR_USER_KEY");
struct LDClient *client = LDClientInit(config, maxwaitmilliseconds);
LDClient must be a singleton

It's important to make LDClient a singleton. 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.

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

Here's how:

user = LDUserNew("example-user-key");
LDUserSetName(user, "Sandy");
LDBoolean show_feature = LDBoolVariation(client, user, FEATURE_FLAG_KEY, 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: