Android平台C++平台中嵌入Python的错误嵌入

2024-04-19 13:07:52 发布

您现在位置:Python中文网/ 问答频道 /正文

我希望在C++ Qt qmake项目中嵌入Python,该项目必须能够在多种平台上工作,如Linux、Android和Windows

到目前为止,我已经通过执行以下操作在Linux部分取得了成功:

.pro文件中,我包含了这样的Python C

# Python
unix {

    LIBS += -L 3rdparty/python3.9 -lpython3.9
    INCLUDEPATH += 3rdparty/python3.9
    DEPENDPATH += 3rdparty/python3.9
}

有关更多信息,请参见.pro文件中的“unix”包括Linux和Android

接下来,在我的cpp文件中,我像这样导入了Python头

extern "C" {
//For Python integration and avoiding name conflict with reserved Qt keywords slots, emit, and so on
#define IGNORE_CRYPT_H
#pragma push_macro("slots")
#undef slots
#include <Python.h>
#pragma pop_macro("slots")
}

为了避免包含crypt.h,我对Python.h文件做了如下轻微修改

#ifndef IGNORE_CRYPT_H
#include <crypt.h>
#endif
<>最后,我可以像C++这样执行一个简单的python打印脚本
Py_Initialize();
std::string code = "print(\"hello there\")";
PyRun_SimpleString(code.c_str());
Py_Finalize();

现在,最后,当我为我的主机操作系统Linux编译这个时,它工作得非常好,我可以看到“hello there”被输出到控制台

然而,当我尝试为Android编译这个时,问题就开始了。针对arm64-v8a和armeabi-v7a架构的Android编译失败,出现以下错误

/home/user/Android/Sdk/ndk/21.3.6528147/toolchains/llvm/prebuilt/linux-x86_64/bin/../lib/gcc/aarch64-linux-android/4.9.x/../../../../aarch64-linux-android/bin/ld: cannot find -lpython3.9
clang++: error: linker command failed with exit code 1 (use -v to see invocation)
make[1]: *** [Makefile.Arm64-v8a:320: libMemento_arm64-v8a.so] Error 1
make[1]: Leaving directory '/tmp/memento-android-release'
make: *** [Makefile:75: arm64-v8a-all] Error 2
make: *** Waiting for unfinished jobs....
In file included from /path/to/source/code/backend.cpp:26:
In file included from /path/to/source/code/3rdparty/python3.9/Python.h:65:
/path/to/source/code/3rdparty/python3.9/pyport.h:741:2: error: "LONG_BIT definition appears wrong for platform (bad gcc/glibc config?)."
#error "LONG_BIT definition appears wrong for platform (bad gcc/glibc config?)."
 ^
1 error generated.
make[1]: *** [Makefile.Armeabi-v7a:1664: armeabi-v7a/backend.o] Error 1
make[1]: Leaving directory '/tmp/memento-android-release'
make: *** [Makefile:47: armeabi-v7a-all] Error 2
13:32:57: The process "/home/user/Android/Sdk/ndk/21.3.6528147/prebuilt/linux-x86_64/bin/make" exited with code 2.
Error while building/deploying project memento (kit: Android Qt 5.15.2 (android) Clang Multi-Abi)
When executing step "Make"

基本上,有两个错误我不知道如何解决-它找不到lpython3.9,“LONG_BIT”的定义是错误的。有没有关于如何解决这个问题并为Android编译这个项目的想法


Tags: 文件tomakelinuxcodeerrorqtmakefile