C/C++ SDK reference (server-side)
Read time: 3 minutes
Last edited: Jul 26, 2023
Overview
This topic documents how to get started with the server-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 a sample application:
Resource | Location |
---|---|
SDK API documentation | SDK API docs |
GitHub repository | c-server-sdk |
Sample application | C (server-side) |
Published module | None |
This SDK is intended for use in multi-user C/C++ server applications. If you have a C/C++ application and want to set up LaunchDarkly in a mobile, desktop, or embedded application, read the client-side C++ SDK reference.
To learn more about LaunchDarkly's different SDK types, read Client-side and server-side SDKs.
Getting started
After you complete the Getting Started process, follow these instructions to start 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.
After cloning the repository, include the LaunchDarkly SDK headers:
#include "launchdarkly/api.h"
Configure logging, and call the global initialization function. You must call these functions before you perform any other operations. LaunchDarkly provides a predefined convenience logger.
Here's how:
LDConfigureGlobalLogger(LD_LOG_INFO, LDBasicLogger);LDGlobalInit();
The C/C++ (server-side) SDK uses an SDK key. Your environment's SDK key is available in the Projects tab of your Account settings page. To learn more about key types, read Keys.
After you install and import the SDK, create a single, shared instance of LDClient
. Specify your SDK key here to authorize your application to connect to a particular environment within LaunchDarkly.
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.
Calling LDClientInit
initiates a remote call to the LaunchDarkly service to fetch feature flags. This call blocks 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.
Here is an example:
unsigned int maxwaitmilliseconds = 10 * 1000;struct LDConfig *config = LDConfigNew("sdk-key-123abc");struct LDUser *user = LDUserNew("user-key-123abc");struct LDClient *client = LDClientInit(config, maxwaitmilliseconds);
You can use client
to check which variation a particular user will receive for a given feature flag.
Here's how:
user = LDUserNew("user-key-123abc");LDUserSetName(user, "Sandy");LDBoolean show_feature = LDBoolVariation(client, user, flag-key-123abc, false, NULL);if (show_feature) {// application code to show the feature} else {// the code to run if the feature is off}
If the SDK might be able to execute your flag evaluation before client
initializes, wrap your call in LDClientIsInitialized(client)
:
if (LDClientIsInitialized(client)) {// flag evaluation goes here}
Shut down the client
Shut down the client when your application terminates. To learn more, read Shutting down.
Supported features
This SDK supports the following features:
- Aliasing users
- Anonymous contexts and users
- Configuration
- Evaluating flags
- Flag evaluation reasons
- Flushing events
- Getting all flags
- Identifying and changing contexts
- Logging configuration
- Offline mode
- Private attributes
- Relay Proxy configuration
- Sending custom events
- Service endpoint configuration
- Shutting down
- Storing data
- Test data sources
- User and context configuration