This article discusses how to set up and compile C++ projects from the command line and popular IDEs.
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. 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 OS X library supports 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.
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.
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: the 32-bit libraries are in the SDK lib\x86 folder; the 64-bit libraries are in lib\x64.
For 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
For 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
A Makefile is supplied in the SDK samples folder. This makefile is setup to compile the Sample.cpp program, but you can adapt it to build your own programs.
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.
- 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 Leap.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 /yr "$(LEAP_SDK)\lib\x86\Leap.dll" "$(TargetDir)"
- Choose the Release configuration.
- Repeat the setps above.
To set up a Leap Motion project in Xcode:
Open or create an Xcode project.
Add the Leap Motion SDK include files and API library:
Configure the project to bundle libLeap.dylib with the application:
- Go to the target Build Phases page.
- Add a build phase using the Menu command: Editor>Add Build Phase>Add Copy Files Build Phase.
- Open the new phase.
- Set destination to Executables.
- Add libLeap.dylib to the list of files.
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).