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

EDIT ON GITHUB

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

Read time: 2 minutes
Last edited: Dec 04, 2021

Overview

This topic explains how to use the C/C++ client-side SDK, and gives reference information about the SDK's prerequisites and supported features.

All LaunchDarkly SDKs are open source. Clone this SDK from its GitHub repository or read its API reference.

You can also use this SDK to clone and run a sample C application or sample C++ application.

Prerequisites

To use the C/C++ SDK, you must have the following prerequisites:

  • Windows or a POSIX environment (Linux, OSX, BSD)
  • The networking library libcurl
  • (POSIX environments) libpthread
Use this SDK only in desktop and embedded client applications

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

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

Configuring the SDK

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

Installing the SDK

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 expects that libcurl and libpthread (if on POSIX) 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.
  1. Build the C++ wrapper, which is not included in the release binaries. Copy the header and source files and add them to your own build system.
  2. Include the LaunchDarkly SDK headers:
1#include <launchdarkly/api.h>
  1. Create a single shared instance of LDClient.
  2. Specify your mobile key to authorize your application to connect to LaunchDarkly for the associated project and environment:
1unsigned int maxwaitmilliseconds = 10 * 1000;
2struct LDConfig *config = LDConfigNew("YOUR_MOBILE_KEY");
3struct LDUser *user = LDUserNew("YOUR_USER_KEY");
4struct LDClient *client = LDClientInit(config, user, maxwaitmilliseconds);

Your environment's mobile key is available in the Projects tab of your account settings page. Be sure to use a mobile key. Never embed a server-side SDK key into a mobile application.

Fetching flag configuration

Making feature flags available to this SDK

You must make feature flags available to mobile SDKs before the flags can be evaluated. If an SDK tries to evaluate a feature flag that is not available, the user will receive the default value for that flag.

To make a flag available to this SDK, check the SDKs using Mobile key checkbox during flag creation, or on the flag's Settings tab. To make all of a project's flags available to this SDK by default, check the SDKs using Mobile key checkbox in your project Settings.

Here's how to verify the SDK is configured correctly by fetching flag configuration data from LaunchDarkly:

  1. Call LDClientInit to initiate a remote call to the LaunchDarkly service. This fetches the feature flag settings for a given user.

    This call blocks up to the time defined by maxwaitmilliseconds. If you request a feature flag before initialization completes, you receive the default flag value. If you want to wait for client initialization, register a callback:

1void initCallback(LDStatus status)
2{
3 if (status == LDStatusInitialized) {
4 printf("Completed LaunchDarkly client initialization");
5 }
6}
7
8LDSetClientStatusCallback(initCallback);
  1. Use client to check which variation a specific user should receive for a certain feature flag:
1bool show_feature = LDBoolVariation(client, "YOUR_FLAG_KEY", false);
2if (show_feature) {
3 // application code to show the feature
4} else {
5 // the code to run if the feature is off
6}

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

Supported features

This SDK supports the following features: