Windows10+Cygwin环境下CMake链接双SDK失败求助
Hey there, let's tackle this linker issue step by step—working with two SDKs alongside Cygwin on Windows can get tricky thanks to cross-toolchain quirks, especially as a C++ newbie. Let's break down the root causes and fix them:
First, Understand the Core Problem
The "undefined reference" errors almost certainly stem from library format incompatibility. Most Windows SDKs (like Leap Motion and Ultrahaptics) ship with MSVC-compatible .lib files by default, but Cygwin uses the GNU toolchain (GCC/ld), which requires GNU-format static libraries (.a) or import libraries (.dll.a). MSVC .lib files won't play nicely with Cygwin's linker.
Step-by-Step Fixes
1. Convert SDK Libraries to Cygwin-Compatible Format
If the SDKs don't provide pre-built GNU-format libraries, you'll need to convert their dynamic libraries (.dll) to Cygwin import libraries:
- For each SDK's
.dll(e.g.,Leap.dll,Ultrahaptics.dll):- First, export a
.deffile listing the DLL's symbols. You can do this with MSVC'sdumpbin(run from an MSVC Command Prompt):
Or use Cygwin'sdumpbin /exports C:\Path\To\Leap.dll > Leap.defobjdumpif you prefer:objdump -p /cygdrive/c/Path/To/Leap.dll | grep -A 100 "Export Address Table" > Leap.def - Use Cygwin's
dlltoolto generate a GNU-compatible import library:dlltool -d Leap.def -l libLeap.dll.a - Repeat this for
Ultrahaptics.dlland any dependent DLLs (likelibusb-1.0.dll).
- First, export a
2. Fix Your CMakeLists.txt
Your current CMake config has mixed path styles and inconsistent project/target names. Let's rewrite it to be Cygwin-friendly:
cmake_minimum_required(VERSION 3.8) project(ICMI2017Demo) # Match your target name to avoid confusion set(CMAKE_CXX_STANDARD 11) # --- Ultrahaptics SDK Configuration --- set(ULTRAHAPTICS_ROOT "/cygdrive/c/Program Files/Ultrahaptics") include_directories(${ULTRAHAPTICS_ROOT}/include) # Point to the directory with your converted .dll.a libraries link_directories(${ULTRAHAPTICS_ROOT}/lib/x64) set(ULTRAHAPTICS_LIBS Ultrahaptics usb-1.0) # CMake will look for libUltrahaptics.dll.a etc. # --- Leap Motion SDK Configuration --- set(LEAP_ROOT "/cygdrive/c/PhD/code/libraries/LeapSDK") include_directories(${LEAP_ROOT}/include) link_directories(${LEAP_ROOT}/lib/x64) set(LEAP_LIBS Leap) # --- Source Files --- # Don't forget to include header files used in your project set(SOURCE_FILES main.cpp main.h HapticSurface/LeapMotionDataConversion.hpp ) add_executable(ICMI2017Demo ${SOURCE_FILES}) # Link the converted libraries target_link_libraries(ICMI2017Demo ${ULTRAHAPTICS_LIBS} ${LEAP_LIBS})
3. Verify Runtime Dependencies
Even if compilation succeeds, you'll need to ensure the SDK .dll files are accessible to your executable:
- Copy
Leap.dll,Ultrahaptics.dll, andlibusb-1.0.dllto the same directory asICMI2017Demo.exe - Or add the SDK
bin/x64directories to your WindowsPATHenvironment variable
4. Test Incrementally
To narrow down issues, compile a minimal test program for each SDK separately:
- For Leap Motion: Write a tiny program that initializes the Leap controller and checks for compilation/linking
- For Ultrahaptics: Do the same with a basic Ultrahaptics device setup
This will confirm if one SDK is causing more issues than the other.
Quick Checks to Rule Out Simple Mistakes
- Ensure you're using 64-bit Cygwin (match your SDK and Windows architecture)
- Double-check that all paths in CMake use Cygwin-style syntax (
/cygdrive/c/...instead ofC:/...) - Confirm you've installed full Cygwin development tools (gcc-g++, cmake, make, dlltool, etc.)
内容的提问来源于stack exchange,提问作者Nick

