使用Android NDK工具链构建时CMake无法找到PyTorch相关库与包
Let me break down why this is happening and how to fix it—this is a common gotcha with cross-compiling for Android using CMake, especially when working with prebuilt libraries like PyTorch.
Why This Happens
When you switch to the Android NDK toolchain, CEnable enables cross-compilation mode, which changes how it resolves paths and finds packages/libraries. Even if you specify absolute paths in find_package or find_library, CMake might ignore them because of default settings that restrict searches to the Android sysroot (the root filesystem for your target Android platform).
Specifically, these two variables are likely causing the problem:
CMAKE_FIND_ROOT_PATH_MODE_PACKAGE: Defaults toONLYin Android toolchains, meaning CMake will only look for packages inside the sysroot, ignoring your manually specifiedPATHS.CMAKE_FIND_ROOT_PATH_MODE_LIBRARY: Same logic applies here—find_librarywill only search the sysroot unless told otherwise.
Another possible culprit is mismatched ABIs: if your PyTorch prebuilt libraries are for a different architecture (e.g., x86_64) than your target Android ABI (e.g., arm64-v8a), CMake will reject them even if the files exist.
Fixes to Try
1. Relax CMake's Path Search Restrictions
Add these lines after loading the Android toolchain in your CMakeLists.txt, before calling find_package(Torch):
# Allow CMake to search both sysroot and custom paths for packages/libraries set(CMAKE_FIND_ROOT_PATH_MODE_PACKAGE BOTH) set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY BOTH) set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE BOTH) # Optional, but helps with header resolution
This tells CMake to check both the Android sysroot and the absolute paths you specify in TorchConfig.cmake.
2. Verify ABI Compatibility
Double-check that your PyTorch prebuilt libraries match your target Android ABI:
- In your CMakeLists.txt, confirm you've set the correct ABI:
set(ANDROID_ABI arm64-v8a) # Or whatever ABI you're targeting (e.g., armeabi-v7a) - Use the
filecommand to inspect the library architecture:
The output should mention your target ABI (e.g.,file /path/to/your/pytorch/lib/libc10.soARM aarch64for arm64-v8a). If it doesn't, download the correct PyTorch prebuilt libraries for your ABI from the official PyTorch distribution.
3. Manually Specify Caffe2 and c10 Paths
If the first fix doesn't work, bypass the problematic lines in TorchConfig.cmake by manually configuring Caffe2 and c10 before calling find_package(Torch):
# Manually set Caffe2 configuration path set(Caffe2_DIR "/absolute/path/to/pytorch/lib/cmake/Caffe2") find_package(Caffe2 REQUIRED NO_DEFAULT_PATH) # Manually configure the c10 shared library set(C10_LIBRARY "/absolute/path/to/pytorch/lib/libc10.so") add_library(c10 SHARED IMPORTED) set_target_properties(c10 PROPERTIES IMPORTED_LOCATION ${C10_LIBRARY} INTERFACE_INCLUDE_DIRECTORIES "/absolute/path/to/pytorch/include" ) # Now load PyTorch with explicit paths set(Torch_DIR "/absolute/path/to/pytorch/lib/cmake/Torch") find_package(Torch REQUIRED NO_DEFAULT_PATH)
This skips the auto-detection in TorchConfig.cmake and forces CMake to use the exact paths you know are valid.
4. Patch TorchConfig.cmake for Android
Occasionally, TorchConfig.cmake might have logic that doesn't account for Android cross-compilation. You can edit the problematic lines to explicitly add NO_DEFAULT_PATH to ensure CMake only uses the specified paths:
# Modify line 40 in TorchConfig.cmake find_package(Caffe2 REQUIRED PATHS ${CMAKE_CURRENT_LIST_DIR}/../Caffe2 NO_DEFAULT_PATH) # Modify line 47 in TorchConfig.cmake find_library(C10_LIBRARY c10 PATHS "${TORCH_INSTALL_PREFIX}/lib" NO_DEFAULT_PATH)
This prevents CMake from falling back to sysroot searches for these specific calls.
内容的提问来源于stack exchange,提问作者LIU Qingyuan

