C/C++ SDK reference (client-side)
Read time: 2 minutes
Last edited: May 12, 2023
Overview
This topic documents how to get started with the client-side C/C++ SDK, and links to reference information on all of the supported features.
LaunchDarkly's SDKs are open source. In addition to this reference guide, we provide source, API reference documentation, and sample applications:
Resource | Location |
---|---|
SDK API documentation | SDK API docs |
GitHub repository | c-client-sdk |
Sample applications | C (client-side) C++ (client-side) |
Published module | None |
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
libpthread
, if you are using a POSIX environment
This SDK is intended for use in single-user mobile, desktop, and embedded applications. If you have a C/C++ application and want to set up LaunchDarkly on the server-side, read the server-side C/C++ 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:
- Clone the GitHub repository or download a release archive from the GitHub Releases page.
- Install the SDK locally.
- If you use
cmake
, the build system will expect thatlibcurl
, andlibpthread
if you are on POSIX, exist on the system. Thecmake
configuration exports the targetldclientapi
. - If you don't use
cmake
and you cannot use LaunchDarkly's artifacts, usecmake install
to install the SDK in directory you choose. This copies the required headers, and binaries equivalent to LaunchDarkly's release bundles.
- 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.
- Include the LaunchDarkly SDK headers:
#include <launchdarkly/api.h>
- Create a single shared instance of
LDClient
. - Specify your mobile key to authorize your application to connect to a particular environment within LaunchDarkly:
unsigned int maxwaitmilliseconds = 10 * 1000;struct LDConfig *config = LDConfigNew("mobile-key-123abc");struct LDUser *user = LDUserNew("user-key-123abc");struct LDClient *client = LDClientInit(config, user, maxwaitmilliseconds);
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.
Your environment's mobile key is available in the Projects tab of your account settings page.
Mobile keys are not secret and you can expose them in your client-side code without risk. However, never embed a server-side SDK key into a client-side application.
Fetching flag configuration
You must make feature flags available to mobile SDKs before the SDK can evaluate those flags. If an SDK tries to evaluate a feature flag that is not available, the user will receive the fallback 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:
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 will receive the default flag value. If you want to wait for client initialization, register a callback:
void initCallback(LDStatus status){if (status == LDStatusInitialized) {printf("Completed LaunchDarkly client initialization");}}LDSetClientStatusCallback(initCallback);
- Use
client
to check which variation a specific user should receive for a certain feature flag:
bool show_feature = LDBoolVariation(client, "flag-key-123abc", false);if (show_feature) {// Application code to show the feature} else {// The code to run if the feature is off}
Shutting down
Shut down the client when your application terminates. To learn more, read Shutting down.
Supported features
This SDK supports the following features: