Cmake 编译失败找不到 python.h (Flutter, dart:ffi)

我需要将使用 Python 的 .C/.CPP 代码连接到 Flutter 应用程序中。 幸运的是我可以使用 dart:ffi 。因此 但是当我尝试编译 apk 时,出现以下错误:

    launching lib/main.dart on AOSP on IA Emulator in debug mode...
    lib/main.dart:1
    
    FAILURE: Build failed with an exception.
    
    * What went wrong:
    Execution failed for task ':app:externalNativeBuildDebug'.
    > Build command failed.
      Error while executing process /home/*usr_name*/Android/Sdk/cmake/3.18.1/bin/ninja with arguments {-C /home/*usr_name*/flutter/final4/android/app/.cxx/cmake/debug/x86_64 weather}
      ninja: Entering directory `/home/*usr_name*/flutter/final4/android/app/.cxx/cmake/debug/x86_64'
      [1/2] Building C object CMakeFiles/weather.dir/home/*usr_name*/flutter/final4/src/weather.c.o
      FAILED: CMakeFiles/weather.dir/home/*usr_name*/flutter/final4/src/weather.c.o 
 /home/*usr_name*/Android/Sdk/ndk/22.1.7171670/toolchains/llvm/prebuilt/linux-x86_64/bin/clang --target=x86_64-none-linux-android21 --gcc-toolchain=/home/*usr_name*/Android/Sdk/ndk/22.1.7171670/toolchains/llvm/prebuilt/linux-x86_64 --sysroot=/home/*usr_name*/Android/Sdk/ndk/22.1.7171670/toolchains/llvm/prebuilt/linux-x86_64/sysroot -Dweather_EXPORTS  -g -DANDROID -fdata-sections -ffunction-sections -funwind-tables -fstack-protector-strong -no-canonical-prefixes -D_FORTIFY_SOURCE=2 -Wformat -Werror=format-security  -O0 -fno-limit-debug-info  -fPIC -MD -MT CMakeFiles/weather.dir/home/*usr_name*/flutter/final4/src/weather.c.o -MF CMakeFiles/weather.dir/home/*usr_name*/flutter/final4/src/weather.c.o.d -o CMakeFiles/weather.dir/home/*usr_name*/flutter/final4/src/weather.c.o -c /home/*usr_name*/flutter/final4/src/weather.c
  /home/*usr_name*/flutter/final4/src/weather.c:43:10: fatal error: 'Python.h' file not found
  #include <Python.h>
           ^~~~~~~~~~
  1 error generated.
  ninja: build stopped: subcommand failed.

起初我的 CMakeLists.txt 文件是这样的:

cmake_minimum_required(VERSION 3.18)  
project(py)

add_library( 
  weather
  SHARED
  ../../src/weather.c
)

所以我编辑它以找到 Python 并确保他确实找到了它以及我安装的开发版本

cmake_minimum_required(VERSION 3.18)  
project(py)

find_package(Python COMPONENTS Interpreter Development)

message("Python_FOUND:${Python_FOUND}")
message("Python_VERSION:${Python_VERSION}")
message("Python_Development_FOUND:${Python_Development_FOUND}")
message("Python_libraRIES:${Python_libraRIES}")
message("Python_libraRY_DIRS:${Python_libraRY_DIRS}")
message("Python_INCLUDE_DIRS:${Python_INCLUDE_DIRS}")
message("Python_LINK_OPTIONS:${Python_LINK_OPTIONS}")
message("Python_NumPy_FOUND:${Python_NumPy_FOUND}")


add_library( 
  weather
  SHARED
  ../../src/weather.c
)

现在 cmake . 打印:

-- The C compiler identification is GNU 9.3.0
-- The CXX compiler identification is GNU 9.3.0
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working C compiler: /usr/bin/cc - skipped
-- Detecting C compile features
-- Detecting C compile features - done
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: /usr/bin/c++ - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Found Python: /usr/bin/python3.8 (found version "3.8.10") found components: Interpreter Development Development.Module Development.Embed 
Python_FOUND:TRUE
Python_VERSION:3.8.10
Python_Development_FOUND:TRUE
Python_libraRIES:/usr/lib/x86_64-linux-gnu/libpython3.8.so
Python_libraRY_DIRS:/usr/lib/x86_64-linux-gnu
Python_INCLUDE_DIRS:/usr/include/python3.8
Python_LINK_OPTIONS:
Python_NumPy_FOUND:FALSE
-- Configuring done
-- Generating done
-- Build files have been written to: /home/*usr_name*/flutter/final4/android/app

太好了,现在 Cmake 知道 python 在哪里,但在尝试创建 apk 时仍然返回相同的错误。 我也尝试使用 -I 标志为编译器包含 include/python3.8 路径,但这导致了一系列不同的问题,使我强制使用外部路径,这些路径只是感觉不对,最终没有解决方案。

我把这个项目添加到了github,所以你们也可以看看: https://github.com/Eidantz/Flutter_python_c

您可以在 /src 文件夹中找到 weather.c 和另一个我尝试过的 .C 文件。 CMakeLists.txt 当然在 /android/app 文件夹中

提前谢谢各位!

hanhjj 回答:Cmake 编译失败找不到 python.h (Flutter, dart:ffi)

暂时没有好的解决方案,如果你有好的解决方案,请发邮件至:iooj@foxmail.com
本文链接:https://www.f2er.com/14772.html

大家都在问