嵌入式python:extern PyRun_SimpleString,预期标识符

时间:2014-05-17 00:57:22

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

我试图用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

1 个答案:

答案 0 :(得分:2)

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

在这种情况下它是

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

不是旧的宏变体

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

使用它基本上为你想要的Python API函数定义一个extern函数;在这种情况下,要运行一个简单的字符串,必须使用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);