【问题标题】:Embedded python: extern PyRun_SimpleString, Expected identifier嵌入式python:extern PyRun_SimpleString,预期标识符
【发布时间】:2014-05-17 00:57:21
【问题描述】:

我正在尝试使用 C++ 中的嵌入式解释器运行一些 python 命令(我正在使用 Marmalade 中间件并编译为 arm;这是编译到库的 python 2.6.4 的移植版本;我正在链接到库并加载到移植的python头文件中)

[此处完全移植的代码:https://github.com/marmalade/python]

我的问题是我不知道如何正确导入函数,我不断收到 PyRun_SimpleString 的“类型说明符”错误

我应该将其指定为 void 而不是 int 还是与 PyAPI_FUNC(int) 有其他关系?

PythonTest.cpp(使用 libpython_d.a)

#include "Python.h"

extern int PyRun_SimpleString(const char*);
extern void Py_Initialize(void);
extern void Py_Finalize(void);

int main() {
while(true){
Py_Initialize();

PyRun_SimpleString("print('Python Print test')");

Py_Finalize();
}
return 0;
}

error:
PythonTest.cpp(3): error : expected identifier before '__null' (col 48)
PythonTest.cpp(3): error : expected ',' or '...' before '__null' (col 48)
IntelliSense: expected a type specifier PythonTest.cpp 3

这些是引用 PyRun_SimpleString 函数和一些相关依赖项的摘录。 [在 libpython_.a 中编译;头目录已链接]

pythonrun.h

#define PyRun_SimpleString(s) PyRun_SimpleStringFlags(s, NULL)
...
...
PyAPI_FUNC(int) PyRun_SimpleStringFlags(const char *, PyCompilerFlags *);

pythonrun.c

#undef PyRun_SimpleString
PyAPI_FUNC(int)
PyRun_SimpleString(const char *s)
{
    return PyRun_SimpleStringFlags(s, NULL);
}

pyport.h

#ifndef PyAPI_FUNC
#   define PyAPI_FUNC(RTYPE) RTYPE

【问题讨论】:

标签: python c++ visual-studio types marmalade


【解决方案1】:

我想通了。当您从库中导入函数时,您必须使用 PyAPI 函数

在这种情况下是

PyAPI_FUNC(int) PyRun_SimpleStringFlags(const char *, PyCompilerFlags *);

不是旧的宏变体

#define PyRun_SimpleString(s) PyRun_SimpleStringFlags(s, NULL)

要使用它,基本上为您想要的 Python API 函数定义一个外部函数;在这种情况下,要运行一个简单的字符串,您必须使用 simpleStringFlags,将 arg1 设置为您的 Python 字符串,将 arg2 设置为 NULL。 例如:

PyRun_SimpleStringFlags("print('Hello form python library')",NULL);

这是一个橘子酱的例子:

PyAppTest.mkb

#!/usr/bin/env mkb

librarys
{
    <arm_shared>
    "../pythonMaster,python"
}

files
{
    (source)
    PyAppTest.cpp
    (header)
    PyAppTest.h
}

includepaths
{

    ../pythonMaster/
    ../pythonMaster/upstream/Python
    ../pythonMaster/upstream/Include
}

PyAppTest.cpp

#include "Python.h"
#include "../header/PyAppTest.h"
#include "s3eDebug.h"
#include "s3eDevice.h"
#include "s3eSurface.h"



void Py_Initialize();
void Py_Finalize();
int PyRun_SimpleStringFlags(const char *, PyCompilerFlags *);
char *Py_GetProgramFullPath(void);
const char *Py_GetVersion(void);
const char *Py_GetPlatform(void);
const char *Py_GetCopyright(void);
const char *Py_GetCompiler(void);
const char *Py_GetBuildInfo(void);

// Main entry point for the application
int main() {

    Py_Initialize();
    const char *fullPath= Py_GetProgramFullPath();
    const char *pyVer=Py_GetVersion();
    const char *pyPlat=Py_GetPlatform();
    const char *pyCopy=Py_GetCopyright();
    const char *pyCompiler=Py_GetCompiler();
    const char *pyBuild=Py_GetBuildInfo();
    PyRun_SimpleStringFlags("print('hello, this is python')",NULL);
    Py_Initialize();
    Py_Finalize();
     while (!s3eDeviceCheckQuitRequest())
    {
        // Fill background blue
        s3eSurfaceClear(0, 0, 255);

        // Print a line of debug text to the screen at top left (0,0)
        // Starting the text with the ` (backtick) char followed by 'x' and a hex value
        // determines the colour of the text.
s3eDebugPrint(10,20, fullPath, 0);
s3eDebugPrint(10,50, pyVer, 0);
s3eDebugPrint(10,100,  pyPlat, 0);
s3eDebugPrint(10,150, pyCopy, 0);
s3eDebugPrint(10,250, pyCompiler, 0);
s3eDebugPrint(10,350, pyBuild, 0);




        // Flip the surface buffer to screen
        s3eSurfaceShow();

        // Sleep for 0ms to allow the OS to process events etc.
        s3eDeviceYield(0);
    }
    // Return
    return 0;
}

PyAppTest.h

extern int PyRun_SimpleStringFlags(const char *, PyCompilerFlags *);


extern void Py_Initialize(void);
extern void Py_Finalize(void);
extern char *Py_GetProgramFullPath(void);

extern const char *Py_GetVersion(void);
extern const char *Py_GetPlatform(void);
extern const char *Py_GetCopyright(void);
extern const char *Py_GetCompiler(void);
extern const char *Py_GetBuildInfo(void);

【讨论】:

    猜你喜欢
    • 2018-03-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-07-06
    • 2023-03-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多