• SDKS
No results for ""


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

Read time: 6 minutes
Last edited: May 14, 2021

This reference guide documents basic usage of our C / C++ client-side SDK, and explains in detail how features work. If you want to dig even deeper, our SDKs are open source. To learn more, read C/C++ SDK GitHub repository. The complete API reference is available here. Additionally you can clone and run a sample C application or a sample C++ application using this SDK.

The C / C++ SDK supports Windows and POSIX environments (Linux, OSX, BSD). The networking library libcurl is a universal dependency.

For use in desktop and embedded client applications only

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

To learn more, read client-side and server-side SDKs.

Getting started

Building on top of our Getting Started guide, the following steps will get you started with using the LaunchDarkly SDK in your C application.

The C/C++ SDK has no installation steps. To get started, clone the GitHub repository or download a release archive from the GitHub Releases page. This project utilizes cmake for cross platform builds. The build system expects libpthread (if on POSIX), and libcurl are installed on the system. The cmake configuration exports the target ldclientapi. If you are not utilizing cmake in your own project, and you cannot use our artifacts, you should cmake install the SDK to a directory of your choice. This will copy the required headers, and binaries equivalent to our release bundles.

The C++ wrapper is not included in the release binaries and needs to be built separately. Copy the header and source file and add them to your own build system.

Once ready, your first step should be to include the LaunchDarkly SDK headers.

1#include <launchdarkly/api.h>

Once the SDK is installed and imported, you'll want to create a single, shared instance of LDClient. You should specify your mobile key here so that your application will be authorized to connect to LaunchDarkly and for your application 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);
Use a mobile key

Be sure to use a mobile key from your Environments page. Never embed a server-side SDK key into an embedded or desktop application.

Calling LDClientInit will initiate a remote call to the LaunchDarkly service to fetch the feature flag settings for the specified user. This call will block up to the time defined by maxwaitmilliseconds, however. If you request a feature flag before the client has completed initialization, you will receive the default flag value. To wait for client initialization, you can register a callback:

1void initCallback(LDStatus status)
3 if (status == LDStatusInitialized) {
4 printf("Completed LaunchDarkly client initialization");
5 }

Alternatively, you can conditionally gate your LDClient usage if the client is not yet initialized.

1bool initialized = LDClientIsInitialized(client);

Using client, you can check which variation a particular user should receive for a given 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

Customizing your client

You can also pass other custom parameters to the client with the configuration object:

1struct LDConfig *config = LDConfigNew("YOUR_MOBILE_KEY");
2LDConfigSetEventsCapacity(config, 1000);
3LDConfigSetEventsFlushIntervalMillis(config, 30000);

Here, we've customized the event queue capacity and flush interval parameters.

To learn more about the specific configuration options that are available in this SDK, read the SDK's API docs.


Feature flag targeting and rollouts are all determined by the user you pass to your client. Here's an example:

1struct LDJSON *attributes, *groups;
3groups = LDNewArray();
4LDArrayPush(groups, LDNewText("Google"));
5LDArrayPush(groups, LDNewText("Microsoft"));
7attributes = LDNewObject();
8LDObjectSetKey("groups", groups);
10struct LDUser *user = LDUserNew("aa0ceb");
11LDUserSetFirstName(user, "Jake");
12LDUserSetLastName(user, "Fake");
13LDUserSetCustomAttributesJSON(user, attributes);

Let's walk through this snippet. The first argument to the builder is the user's key. In this case we've used the hash "aa0ceb". The user key is the only mandatory user attribute. The key should also uniquely identify each user. You can use a primary key, an email address, or a hash, as long as the same user always has the same key. We recommend using a hash if possible.

All of the other attributes (like firstName, email, and the custom attributes) are optional. The attributes you specify will automatically appear on our dashboard, meaning that you can start segmenting and targeting users with these attributes.

In addition to built-in attributes like names and email addresses, you can pass us any of your own user data by passing custom attributes

A note on types

Most of our built-in attributes (like names and email addresses) expect string values. Custom attributes values can be strings, booleans (like true or false), numbers, or lists of strings, booleans or numbers. If you enter a custom value on our dashboard that looks like a number or a boolean, it'll be interpreted that way.

Custom attributes are one of the most powerful features of LaunchDarkly. They let you target users according to any data that you want to send to us, including organizations, groups, and account plans. Anything you pass to us becomes available instantly on our dashboard.

Private user attributes

You can optionally configure the C SDK to treat some or all user attributes as private user attributes. Private user attributes can be used for targeting purposes, but are removed from the user data sent back to LaunchDarkly.

In the C SDK there are two ways to define private attributes for the LaunchDarkly client:

  • When creating the LDConfig object, you can set the allAttributesPrivate value to true. When you do this, all user attributes (except the key) for the user are removed before the user is sent to LaunchDarkly.
  • When creating the LDConfig object, you can configure a map of privateAttributeNames. If any user has a custom or built-in attribute named in this list, it will be removed before the user is sent to LaunchDarkly.
1struct LDConfig *config = LDConfigNew("YOUR_MOBILE_KEY");
3// Mark all attributes private
4LDConfigSetAllAttributesPrivate(config, true);

Anonymous users

You can also distinguish logged-in users from anonymous users in the SDK, as follows:

1struct LDUser *user = LDUserNew("aa0ceb");
2LDUserSetAnonymous(user, true);

We recommend using the same user key for every initialization and then replacing that with the actual user key when you know who the user is. This way LaunchDarkly counts the initialization user key only once against your MAU, instead of every time you initialize.

Anonymous users work just like regular users, except that they won't appear on your Users page in LaunchDarkly. This keeps anonymous users from polluting your Users page.

You can't search for anonymous users on your Features page, and you can't search or autocomplete by anonymous user keys. If you use the same user key for every anonymous user, you also can't use percentage rollouts or Experimentation with anonymous users.

Aliased users

There are situations in which multiple LaunchDarkly users can represent one person. For example, this can happen when a person initially logs into an application. The person might be represented by an anonymous user before they log in, and a different user after they log in. In that case, that one person would be identified by two different users as denoted by different user keys.

The SDK can associate these two LaunchDarkly users by sending an alias event.

The SDK automatically sends an alias event when LDClientIdentify is called with a known user if the previous user was anonymous. You can disable this behavior if necessary. To learn more, read Customizing your client.

You can manually tell the SDK to send an alias event with the LDClientAlias function.

1LDClientAlias(client, newUser, previousUser);


The track method allows you to record custom events in your application with LaunchDarkly:

1LDClientTrack(client, "YOUR_EVENT_KEY");

Offline mode

In some situations, you might want to stop making remote calls to LaunchDarkly and switch to the last known values for your feature flags. Offline mode lets you do this easily.

1struct LDConfig *config = LDConfigNew("YOUR_MOBILE_KEY");
3// Initialize a client in offline mode:
4LDConfigSetOffline(config, true);
5struct LDClient *client = LDClientInit(config, user, 0);
7// Alternatively, you can switch an already-instantiated client to offline mode:
8LDClientSetOffline(client, true);


Internally, the LaunchDarkly SDK keeps an event buffer for analytics events. These are flushed periodically in a background thread. In some situations (for example, if you're testing out the SDK in a simulator), you may want to manually call flush to process events immediately.


The flush interval is configurable. If you need to change the interval, you can do so via the configuration.

Changing the user context

If your app is used by multiple users on a single device, you may want to change users and have separate flag settings for each user. To achieve this, the SDK supports switching between different user contexts.

You can use the LDClientIdentify method to switch user contexts:

1LDClientIdentify(client, newUser);

The 'identify' call will load any saved flag values for the new user and immediately trigger an update of the latest flags from LaunchDarkly. Since this method re-fetches flag settings for the new user, it should not be called at high frequency. The intended use case for switching user contexts is the login / logout flow.

Real-time updates

LaunchDarkly manages all flags for a user context in real-time by polling flags based on a real-time event stream. When a flag is modified from the LaunchDarkly dashboard, the flag values for the current user will update almost immediately.

To accomplish real-time updates, LaunchDarkly broadcasts an event stream that is listened to by the C SDK. Whenever an event is performed on the dashboard, the C SDK is notified of the updated flag settings in real-time.

Shutting down

To fully uninitialize the C SDK resources you must use close. The operation will block until all resources have been freed. It is not safe to use any API methods after this process is initiated.



If you are having trouble debugging the SDK consider registering a log handler, and increasing the verbosity level:

1LDConfigureGlobalLogger(LD_LOG_TRACE, LDBasicLogger);