将Leap Motion SDK与Cmake链接

时间:2016-05-11 09:57:44

标签: c++ cmake g++ mingw

我正在尝试使用CMake,Qt Creator和MinGW 4.9.2 32bit作为编译器构建一个在Windows 7上使用Myo SDKLeapMotion SDK的项目。

MyoSDK工作正常但我在使用CMake中的LeapMotion SDK时遇到了一些问题。我发现现有的sample CMake file表示Windows部分尚未完成。

这是我的CMakeLists.txt:

cmake_minimum_required (VERSION 2.6)

project (MyoLeapController)

#set(THALMICLABSMYO_ROOT_DIR "C:/myo-sdk-win-0.9.0")

################## MYO

if(WIN32)
    set(THALMICLABSMYO_ROOT_DIR
            "${THALMICLABSMYO_ROOT_DIR}"
            CACHE
            PATH
            "Directory to search for the Thalmic Labs Myo SDK")

    if(CMAKE_SIZEOF_VOID_P MATCHES "8")
        set(_ARCH x86_64)
    else()
        set(_ARCH x86_32)
    endif()

    set(_SDKDIR Windows)

    find_path(THALMICLABSMYO_INCLUDE_DIR
            NAMES myo/libmyo.h
            PATHS "${THALMICLABSMYO_ROOT_DIR}/include")

    include(FindPackageHandleStandardArgs)

    if(CMAKE_SIZEOF_VOID_P MATCHES "8")
        find_library(THALMICLABSMYO_LIBRARY
            NAMES myo64
            PATHS "${THALMICLABSMYO_ROOT_DIR}/lib"
            PATH_SUFFIXES "${_SDKDIR}/${_ARCH}")
    else()
        find_library(THALMICLABSMYO_LIBRARY
            NAMES myo32
            PATHS "${THALMICLABSMYO_ROOT_DIR}/lib"
            PATH_SUFFIXES "${_SDKDIR}/${_ARCH}")
    endif()

    find_package_handle_standard_args(ThalmicLabsMyo
            DEFAULT_MSG
            THALMICLABSMYO_LIBRARY
            THALMICLABSMYO_INCLUDE_DIR
            ${_deps_check})

    if(THALMICLABSMYO_FOUND)
        set(THALMICLABSMYO_LIBRARIES "${THALMICLABSMYO_LIBRARY}")
        set(THALMICLABSMYO_INCLUDE_DIRS "${THALMICLABSMYO_INCLUDE_DIR}")
        mark_as_advanced(THALMICLABSMYO_ROOT_DIR)
    endif()

    mark_as_advanced(THALMICLABSMYO_INCLUDE_DIR THALMICLABSMYO_LIBRARY)
endif()

################## LEAP

IF(LEAP_INCLUDE_DIR AND LEAP_LIBRARY)
    SET(LEAP_FIND_QUIETLY TRUE)
ENDIF(LEAP_INCLUDE_DIR AND LEAP_LIBRARY)

# Set locations to search
IF(UNIX)
    SET(LEAP_INCLUDE_SEARCH_DIRS
        /usr/include
        /usr/local/include
        /opt/leap/include
        /opt/leap_sdk/include
        /opt/include INTERNAL)
    SET(LEAP_LIBRARY_SEARCH_DIRS
        /usr/lib
        /usr/lib64
        /usr/local/lib
        /usr/local/lib64
        /opt/leap/lib
        /opt/leap/lib64
        /opt/leap_sdk/lib
        /opt/leap_sdk/lib64 INTERNAL)
    SET(LEAP_INC_DIR_SUFFIXES PATH_SUFFIXES leap)
ELSE(UNIX)
    #WIN32
    SET(LEAP_INC_DIR_SUFFIXES PATH_SUFFIXES inc)
    SET(LEAP_LIB_DIR_SUFFIXES PATH_SUFFIXES lib)
ENDIF(UNIX)

# Set name of the Leap library to use
IF(APPLE)
    SET(LEAP_LIBRARY_NAME libLeap.dylib)
ELSE(APPLE)
    IF(UNIX)
        SET(LEAP_LIBRARY_NAME libLeap.so)
    ELSE(UNIX)
        # TODO Different libraries are provided for compile and runtime
        SET(LEAP_LIBRARY_NAME libLeap.lib)
    ENDIF(UNIX)
ENDIF(APPLE)

IF(NOT LEAP_FIND_QUIETLY)
    MESSAGE(STATUS "Checking for Leap")
ENDIF(NOT LEAP_FIND_QUIETLY)

# Search for header files
FIND_PATH(LEAP_INCLUDE_DIR Leap.h
    PATHS ${LEAP_INCLUDE_SEARCH_PATHS}
    PATH_SUFFIXES ${LEAP_INC_DIR_SUFFIXES})

# Search for library
FIND_LIBRARY(LEAP_LIBRARY ${LEAP_LIBRARY_NAME}
    PATHS ${LEAP_LIBRARY_SEARCH_DIRS}
    PATH_SUFFIXES ${LEAP_LIB_DIR_SUFFIXES})

SET(LEAP_INCLUDE_DIR ${LEAP_INCLUDE_DIR} CACHE STRING
    "Directory containing LEAP header files")
SET(LEAP_LIBRARY ${LEAP_LIBRARY} CACHE STRING "Library name of Leap library")

SET(LEAP_INCLUDE_DIRS ${LEAP_INCLUDE_DIR} )
SET(LEAP_LIBRARIES ${LEAP_LIBRARY} )

INCLUDE(FindPackageHandleStandardArgs)
FIND_PACKAGE_HANDLE_STANDARD_ARGS(Leap DEFAULT_MSG LEAP_LIBRARY LEAP_INCLUDE_DIR)

MARK_AS_ADVANCED(LEAP_INCLUDE_DIR LEAP_LIBRARY)

##################

message(${LEAP_LIBRARY})
INCLUDE_DIRECTORIES(${THALMICLABSMYO_INCLUDE_DIR} ${LEAP_INCLUDE_DIRS})
add_executable(MyoLeapController main.cpp)
target_link_libraries(MyoLeapController ${THALMICLABSMYO_LIBRARY} ${LEAP_LIBRARY})

我的CMake输出是:

C:/LeapSDK/lib/x86/Leap.dll
-- Configuring done
-- Generating done
-- Build files have been written to: C:/Users/riecker/Documents/Masterthesis/MyoLeapController-bin

我将找到的Leap库与Myo完全相同,但我得到了一些Linker错误。

我的C ++测试程序:

#include <iostream>
#include <myo/myo.hpp>
#include <Leap.h>

int main (int argc, char *argv[])
{
    //myo::Hub hub("MyoLeapController");
    Leap::Controller controller;

    std::cout << "hi" << std::endl;
    return 0;
}

错误讯息:

[ 50%] Linking CXX executable MyoLeapController.exe
CMakeFiles\MyoLeapController.dir/objects.a(main.cpp.obj):main.cpp:(.text+0x1d): undefined reference to `Leap::Controller::Controller()'
CMakeFiles\MyoLeapController.dir/objects.a(main.cpp.obj):main.cpp:(.text+0x51): undefined reference to `Leap::Controller::~Controller()'
CMakeFiles\MyoLeapController.dir\build.make:98: recipe for target 'MyoLeapController.exe' failed
CMakeFiles\MyoLeapController.dir/objects.a(main.cpp.obj):main.cpp:(.text+0x61): undefined reference to `Leap::Controller::~Controller()'
CMakeFiles\Makefile2:66: recipe for target 'CMakeFiles/MyoLeapController.dir/all' failed
makefile:82: recipe for target 'all' failed
C:/Qt/Tools/mingw492_32/bin/../lib/gcc/i686-w64-mingw32/4.9.2/../../../../i686-w64-mingw32/bin/ld.exe: CMakeFiles\MyoLeapController.dir/objects.a(main.cpp.obj): bad reloc address 0x0 in section `.ctors'
collect2.exe: error: ld returned 1 exit status
mingw32-make[2]: *** [MyoLeapController.exe] Error 1
mingw32-make[1]: *** [CMakeFiles/MyoLeapController.dir/all] Error 2
mingw32-make: *** [all] Error 2
10:56:32: Der Prozess "C:\Qt\Tools\mingw492_32\bin\mingw32-make.exe" wurde mit dem Rückgabewert 2 beendet.
Fehler beim Erstellen/Deployment des Projekts MyoLeapController (Kit: Desktop Qt 5.6.0 MinGW 32bit)
Bei der Ausführung von Schritt "Make"

Leap SDK的包含正在运行,因为它只能在链接期间找到头文件我才会遇到问题。对于包含和链接,我正在使用与正在运行的Myo SDK相同的功能。

有人可以帮助我或对此有所了解吗?谢谢

1 个答案:

答案 0 :(得分:2)

Leap Motion库does not support MinGW,因此您尝试链接的库不兼容。

但是,您可以在this package中尝试使用dll。它来自this thread。它似乎只适用于32位,但这显然是你想要构建的。

从出现错误的构建目录中,首先看看此命令行是否有效,然后您可以继续使其在您的cmake项目中运行:

  

C:\ Qt \ Tools \ mingw492_32 \ bin \ g ++。exe -Wl, - whole-archive   CMakeFiles \ MyoLeapController.dir / objects.a -Wl, - no-whole-archive -o   MyoLeapController.exe -Wl, - out-implib,libMyoLeapController.dll.a   -Wl, - major-image-version,0, - minor-image-version,0 -LC:/myo-sdk-win-0.9.0/lib/ -L pathToTheLeapDll -lLeap -lmyo32 - lkernel32 -luser32 -lgdi32 -lwinspool -lshell32 -lole32 -loleaut32 -luuid -lcomdlg32 -ladvapi32

如果有效,当您尝试使用可执行文件时,请确保您的路径或执行目录中有libLeap.dll