0

I need to connect .C/.CPP code that makes use of Python in to Flutter app. Luckily I can use dart:ffi for it. and therefore the but when I'm trying to compile the apk I am getting the following error:

    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.

at first my CMakeLists.txt file looked like this:

cmake_minimum_required(VERSION 3.18)  
project(py)

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

So I edit it to find Python and make sure that he indeed find it and the development version that I have installed :

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
)

Now cmake . prints:

-- 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

Great, now Cmake knows where python is but still returns the same error when trying to create the apk. I tried as well to include the include/python3.8 path for the compiler with the -I flag but that caused a bunch of different issues that make me force outer paths which just felt wrong and ended up with no solution.

I added the project to github so you guys can take a look at it as well: https://github.com/Eidantz/Flutter_python_c

you can find weather.c and another .C file that I tried in /src folder. the CMakeLists.txt is of course in /android/app folder

Thanks in advance guys!

  • You output python-related variables, but do not use them for your library. – Tsyvarev Jul 29 '21 at 20:05
  • I do use it in the other C file but still the same error, did you manage to compile it to so file? – ET_DDE12 Jul 30 '21 at 08:06
  • I mean **CMake variables** like `Python_LIBRARIES` and `Python_INCLUDE_DIRS`: You output them via `message()` command, but do not use them in `target_link_libraries` or `target_include_directories` calls. As currently written in your `CMakeLists.txt`, the library `weather` is NOT linked with Python, and has NO python-related include directories. Obviously, `#include ` cannot be resolved for such library. – Tsyvarev Jul 30 '21 at 08:18

0 Answers0