Documentation
Developing Leap Motion Applications in C++
This article discusses how to set up and compile C++ projects from the command line and popular IDEs.
Topics:
Compilers and libraries
On Windows, you can use the Visual C++ compiler included with Visual Studio 2008, 2010, or 2012. On OS X and Linux, you can use the gcc or clang compiler.
On Windows, the Leap Motion C++ API is provided in the dynamically linked library, Leap.dll (release) or Leapd.dll (debug). Separate libraries are provided for 32-bit, x86 architectures and 64-bit, x64 architectures.
On Mac OS X, the Leap Motion C++ API is provided in the dynamic library, libLeap.dylib. The SDK contains two versions, one linked with libstdc++ and the other with libc++. Both versions are functionally equivalent; choose the one that suits your desired compiler settings. The OS X libraries support both 32-bit and 64-bit architectures.
On Linux, the Leap Motion C++ API is provided in the shared library, libLeap.so. Separate libraries are provided for 32-bit, x86 architectures and 64bit, x64 architectures. The libraries use the libstdc++ standard library.
The Leap Motion dynamic libraries are designed to be loaded from the same directory as your application executable. You are expected to distribute the appropriate Leap Motion library with your application. The Leap Motion libraries are located in the lib folder of the Leap SDK package.
Compiling and linking from the command line
The following examples illustrate how to compile and run Leap-enabled C++ applications from a terminal window or command line. The examples compile and run the Sample.cpp program included in the SDK. The current, working directory must be the SDK Samples folder for the commands to work as written.
On Windows
It is simplest to run the Visual C++ compiler from the Visual Studio command prompt. The Visual Studio command prompt automatically sets up the environment variables to make it easy to run the compiler and linker. The Leap Motion SDK provides different libraries for 32-bit and 64-bit architectures as well as separate libraries for release versus debug configurations (for a total of four combinations). The 32-bit libraries are in the SDK lib\x86 folder; the 64-bit libraries are in lib\x64. The debug versions of the libraries are designated by the letter "d" at the end of the library name: i.e. Leapd.dll instead of Leap.dll.
Using debug versions also requires that msvcp100d.dll and msvcr100d.dll from the Microsoft Visual C++ Runtime Redistributable package are accessible. These libraries are included with the Leap Motion SDK in case you do not already have them installed. The release version of these libraries are installed along with the Leap Motion software so you do not need to include them with the release version of your application when distributing it to end-users.
Debug 32-bit:
mkdir Debug cl /EHsc /Zi /I ..\include Sample.cpp /link ..\lib\x86\Leap.lib /OUT:Debug\Sample.exe /DEBUG xcopy ..\lib\x86\Leapd.dll Debug xcopy ..\lib\x86\msvcp100d.dll Debug xcopy ..\lib\x86\msvcr100d.dll Debug Debug\Sample
Debug 64-bit:
mkdir Debug cl /EHsc /Zi /I ..\include Sample.cpp /link ..\lib\x64\Leapd.lib /OUT:Debug\Sample.exe /DEBUG xcopy ..\lib\x64\Leapd.dll Debug xcopy ..\lib\x64\msvcp100d.dll Debug xcopy ..\lib\x64\msvcr100d.dll Debug Debug\Sample
Release 32-bit:
mkdir Release cl /EHsc /I ..\include Sample.cpp /link ..\lib\x86\Leap.lib /OUT:Release\Sample.exe xcopy ..\lib\x86\Leap.dll Release Release\Sample
Release 64-bit:
mkdir Release cl /EHsc /I ..\include Sample.cpp /link ..\lib\x64\Leap.lib /OUT:Release\Sample.exe xcopy ..\lib\x64\Leap.dll Release Release\Sample
On Mac OS X
With gcc:
mkdir ../build g++ -std=c++98 Sample.cpp -I ../include -o ../build/Sample ../lib/libLeap.dylib cp ../lib/libLeap.dylib ../build ../build/Sample
With clang:
mkdir ../build clang++ -std=c++11 -stdlib=libc++ Sample.cpp -I ../include -o ../build/Sample ../lib/libc++/libLeap.dylib cp ../lib/libc++/libLeap.dylib ../build ../build/Sample
On Linux
With gcc 32-bit:
mkdir ../build g++ -std=c++98 Sample.cpp -I ../include -o ../build/Sample ../lib/x86/libLeap.so cp ../lib/x86/libLeap.so ../build ../build/Sample
With clang 64-bit:
mkdir ../build clang++ -std=c++11 -stdlib=libstdc++ Sample.cpp -I ../include -o ../build/Sample ../lib/x64/libLeap.so cp ../lib/x64/libLeap.so ../build ../build/Sample
Mac OS X: Setting up a C++ project in Xcode
To set up a Leap Motion C++ project in Xcode:
Open or create an Xcode project.
Note that by default, Xcode sets new projects to use c++11, the libc++ standard library, and the Clang flavor of the LLVM compiler. You must use the version of the libLeap.dylib compiled with libc++ when using these defaults.
Add the Leap Motion SDK include files and API library:
Drag Leap.h and LeapMath.h into the project from the SDK include folder.
Drag libLeap.dylib into the project from the SDK lib/libc++ folder. (Make sure to add it to the appropriate project targets.)
Note: When you drag the library into the Xcode project, Xcode adds the parent folder to the library search path. However, it does not remove the folder from the search path if you later remove the library. If you replace the libstdc++ version of libLeap.dylib with the libc++ version, be sure to ammend your library paths under Build Settings, if necessary.
Configure the project to bundle libLeap.dylib with the application:
- Go to the target Build Phases page.
- Add a build phase:
- Click Add Build Phase.
- Select Add Copy Files.
- Open the new phase.
- Set destination to Executables.
- Add libLeap.dylib to the list of files.
- Add your source code...
Mac OS X: Loading the libLeap dynamic library from a different location
If you wish to load the libLeap dynamic library from a different location than your application executable, you can use the OS X install_name_tool to change the loader path. For example, to load the library from the Resources folder of a standard Mac application package instead of the MacOS folder, you could run the following command:
On Mac OS X:
install_name_tool -change @loader_path/libLeap.dylib @executable_path/../Resources/libLeap.dylib Sample
(where Sample is your application binary).
Windows: Setting up a C++ project in Visual Studio
The Leap Motion SDK package includes sample projects for Visual Studio 2008, 20010, and 2012. You can use these projects as a starting point for your Leap Motion projects. This section illustrates how to create a project from scratch. Most of the steps also apply to adding Leap Motion support to an existing project. The example uses Visual Studio 2012.
To add Leap Motion support to a new or existing project:
Important: If you are creating a 64-bit application, use the libraries in the lib\x64 directory of the SDK, not the 32-bit libraries in the lib\x86 directory.
- Open or create a new project of the desired type.
- Make a LEAP_SDK system environment variable to point to your Leap Motion SDK folder. This step is optional, but does simplify creating references to the SDK files in your projects. (As a reminder, you can create and change system environment variables from the Windows System Properties dialog.)
- Open the Project Property Pages using the Project > Properties menu command.
- Set up a Debug configuration:
- Choose the Debug configuration.
- Add the SDK include directory to your project:
Under Configuration Properties, select C/C++ > General.
In the properties pane, add the SDK include directory to the Additional Include Directories field by adding:
$(LEAP_SDK)\include(where LEAP_SDK is the name of the environment variable you created earlier. You can substitute the appropriate file path if you do not want to use an environment variable.)
- Add references to the Leap Motion libraries:
Under Configuration Properties, select Linker > General.
In the properties pane, add the SDK lib\x86 directory (or lib\x64 for 64-bit configurations) to the Additional Library Directories field by adding:
$(LEAP_SDK)\lib\x86Select Linker > Input
Add Leapd.lib to the Additional Dependencies field.
- Add a Post-Build Event to copy the Leap Motion libraries to the project's target executable directory.
Under Configuration Properties, select Build Events > Post-Build Event.
Edit the Command Line field to copy the libraries, adding:
xcopy $(LEAP_SDK)\lib\x86\Leapd.dll "$(TargetDir)" xcopy $(LEAP_SDK)\lib\x86\msvcp100d.dll "$(TargetDir)" xcopy $(LEAP_SDK)\lib\x86\msvcr100d.dll "$(TargetDir)"The msvcp100d.dll and msvcr100d.dll libraries are only needed for debugging configurations. For release configurations, copy only Leap.dll.
- Set up a Release configuration:
- Choose the Release configuration.
- Add the SDK include directory as shown above.
- Add references to the Leap Motion libraries as shown above, except using Leap.lib (not Leapd.lib).
- Add a Post-Build Event to copy Leap.dll to the target executable directory.
- Add your source code...