• SDKS
No results for ""


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

Read time: 2 minutes
Last edited: Jun 18, 2021

This reference guide documents basic usage of our C server-side SDK, and explains in detail how its functions work. If you want to dig even deeper, our SDKs are open source. To learn more, read C/C++ SDK GitHub repository. The online API docs contain the programmatic definitions of every type and method. Additionally you can clone and run a sample application using this SDK.

For use in server-side applications only

This SDK is intended for use in multi-user C/C++ server applications. If you have a C/C++ application and are looking to set up LaunchDarkly in a desktop or embedded application, head to our C/C++ client-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.

Unlike other LaunchDarkly SDKs, the C/C++ SDK has no installation steps. To get started, clone this repository or download a release archive from the GitHub Releases page. You can use CMakeLists.txt in this repository as a starting point for integrating this SDK into your application.

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

1#include "launchdarkly/api.h"

Configure logging, and call the global initialization function. These functions must be called before any other operations are performed. LaunchDarkly provides a predefined convenience logger.

1LDConfigureGlobalLogger(LD_LOG_INFO, LDBasicLogger);

Once the SDK is installed and imported, you'll want to create a single, shared instance of LDClient. You should specify your SDK key here so that your application will be authorized to connect to LaunchDarkly and for your application and environment.

Calling LDClientInit will initiate a remote call to the LaunchDarkly service to fetch feature flags. This call will block up to the time defined by maxwaitmilliseconds. If you request a feature flag before the client has completed initialization, you will receive the default flag value.

1unsigned int maxwaitmilliseconds = 10 * 1000;
2struct LDConfig *config = LDConfigNew("YOUR_SDK_KEY");
3struct LDUser *user = LDUserNew("YOUR_USER_KEY");
4struct LDClient *client = LDClientInit(config, maxwaitmilliseconds);
LDClient must be a singleton

It's important to make this a singleton. The client instance maintains internal state that allows us to serve feature flags without making any remote requests. Be sure that you're not instantiating a new client with every request.

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

1show_feature = LDBoolVariation(client, user, "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

If it is possible for your flag evaluation to be executed before client initializes, you should wrap your call in LDClientIsInitialized(client):

1if (LDClientIsInitialized(client)) {
2 // flag evaluation goes here

Lastly, when your application is about to terminate, shut down client. This ensures that the client releases any resources it is using, and that any pending analytics events are delivered to LaunchDarkly. If your application quits without this shutdown step, you may not see your requests and users on the dashboard, because they are derived from analytics events. This is something you only need to do once.


Supported features

This SDK supports the following features: