Skip to content
This repository has been archived by the owner on Jul 9, 2024. It is now read-only.

Latest commit

 

History

History
193 lines (143 loc) · 7.18 KB

File metadata and controls

193 lines (143 loc) · 7.18 KB
path title description tags published
/sdk/server-side/c-c--
C/C++ SDK reference (server-side)
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.
c
c++
sdk
server-side
true

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:

Resource Location SDK API documentation [SDK API docs](https://launchdarkly.github.io/c-server-sdk) GitHub repository [c-server-sdk](https://github.com/launchdarkly/c-server-sdk) Sample application [C (server-side)](https://github.com/launchdarkly/hello-c-server) Published module None
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++ 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();
The C/C++ (server-side) SDK uses an SDK key

The C/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 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: