LaunchDarkly Developer Documentation

Get started in under 30 minutes.
LaunchDarkly provides feature flags as a service for Java · Python · Ruby · Go · Node.js · PHP · .NET. Control feature launches -- who sees what and when -- without multiple code deploys. Easy dashboard for phased rollouts, targeting and segmenting.
Need more help? write us at support@launchdarkly.com

Get Started    Guides

C/C++ SDK Reference

This is BETA software

The C/C++ SDK should not be used in production environments until version 1.0.0 is released.

This reference guide documents all of the methods available in our C client SDK, and explains in detail how these methods work. If you want to dig even deeper, our SDKs are open source-- head to our C SDK GitHub repository to look under the hood.

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.

Getting started

The C / C++ SDK requires a POSIX environment, and assumes that both libcurl and libpthread are installed.

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. You can use the Makefile in this repository as a starting point for integrating this SDK into your application. The default Makefile does produce two artifacts:

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

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

Once integrated, you'll need to import the LaunchDarkly header file:

#include "ldapi.h"

You'll want to create a single, shared instance of LDClient, passing in the client configuration object and a user:

LDConfig *config = LDConfigNew("YOUR_CLIENT_SIDE_KEY");
LDUser *user = LDUserNew("YOUR_USER_KEY");
LDClient *client = LDClientInit(config, user);

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 not block, 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:

void initCallback(int status)
{
  printf("Completed LaunchDarkly client initializationn");
}

LDSetClientStatusCallback(initCallback);

The returned status will be 1 when the client is connected, and 0 when the client becomes disconnected.

Customizing your client

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

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

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

Users

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

LDUser *user = LDUserNew("aa0ceb");
LDUserSetFirstName(user, "Jake");
LDUserSetLastName(user, "Fake");
LDUserSetCustomAttributesJSON(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.

 LDConfig *config = LDConfigNew("YOUR_CLIENT_SIDE_KEY");

// Mark all attributes private
LDConfigSetAllAttributesPrivate(config, true);

Anonymous users

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

LDUser *user = LDUserNew("aa0ceb");
user->anonymous = 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!

All flags

Track

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.

LDConfig *config = LDConfigNew("YOUR_CLIENT_SIDE_KEY");

// Initialize a client in offline mode:
LDConfigSetOffline(config, true);
LDClient *client = LDClientInit(config, user);


// Alternatively, you can switch an already-instantiated client to offline mode:
LDClientSetOffline(client);

Flush

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.

LDClientFlush(client);

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:

LDClientIdentify(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.

Background fetch

Troubleshooting