• SDK
No results for ""


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

Read time: 5 minutes
Last edited: Feb 6, 2020

This reference guide documents basic usage of our 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 SDK GitHub repository. The complete API reference is available here. Additionally you can clone and run a sample 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 / embedded client applications

The LaunchDarkly C / C++ SDK is designed primarily for use in single user desktop and embedded applications. It follows the client-side LaunchDarkly model for single-user contexts (much like our mobile or JavaScript SDKs)-- a network call must be made to change user contexts, so changing users should be infrequent. It is not intended for use in multi-user systems such as web servers. Learn more about our client-side and server-side SDKs. For using LaunchDarkly in C / C++ server-side applications, refer to our C / C++ server-side SDK.

Getting started

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

Unlike other LaunchDarkly SDKs, the C SDK has no installation steps. To get started, clone the GitHub repository or download a release archive from the GitHub Releases page. Two makefiles exist in this repository as a starting point for integrating the SDK into your application. You may need to modify the Makefile to support your specific use case.

The default POSIX Makefile assumes libpthread, and libcurl are installed on the system. Used with the make command it produces two artifacts:

  • A static library libldapi.a
  • A shared library libldapi.so

The Windows Makefile assumes libcurl has been downloaded into the same directory as the SDK source. The dependency will automatically be built for you in this configuration. Used with nmake /f Makefile.win from the Visual Studio Developer Command Prompt it produces:

  • ldapi.dll

You may use either of these artifacts if they are suitable for your environment.

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

1#include "ldapi.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;
2LDConfig *config = LDConfigNew("YOUR_MOBILE_KEY");
3LDUser *user = LDUserNew("YOUR_USER_KEY");
4LDClient *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)
5 if (status == LDStatusInitialized) {
6 printf("Completed LaunchDarkly client initialization");
7 }

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

1initialized = LDClientIsInitialized(client);

Using client, you can check which variation a particular user should receive for a given feature flag.

1show_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 via the configuration object:

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

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


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

1LDUser *user = LDUserNew("aa0ceb");
2LDUserSetFirstName(user, "Jake");
3LDUserSetLastName(user, "Fake");
4LDUserSetCustomAttributesJSON(user, "{\"groups\": [\"Google\", \"Microsoft\"]}");

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 e-mail 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 e-mail 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 e-mail 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-- organizations, groups, 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.
1LDConfig *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:

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

Anonymous users work just like regular users, except that they won't appear on your Users page in LaunchDarkly. You also can't search for anonymous users on your Features page, and you can't search or autocomplete by anonymous user keys. This is actually a good thing-- it keeps anonymous users from polluting your Users page!


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.

1LDConfig *config = LDConfigNew("YOUR_MOBILE_KEY");
3// Initialize a client in offline mode:
4LDConfigSetOffline(config, true);
5LDClient *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.


Note that 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 identify 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 via 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:

1void logger(const char *s) {
2 printf("LD: %s\n", s);
5LDSetLogFunction(LD_LOG_TRACE, logger);