编译c程序调用Python

在项目原型阶段,使用Python或者shell来验证程序逻辑和方案是非常快捷的开发方法。而且可以在原型完善以后,通过C语言重写方式来提高性能。

不过,在C/C++代码中嵌入Python,最初也遇到一些问题,记录如下。

我在编译嵌入Python的C程序就遇到过无法找到头文件,以及链接程序的错误,这里为了简化,采用了Python 2.7官方文档Embedding Python in Another Application的样例:

#include <Python.h>

int
main(int argc, char *argv[])
{
  Py_SetProgramName(argv[0]);  /* optional but recommended */
  Py_Initialize();
  PyRun_SimpleString("from time import time,ctime\n"
                     "print 'Today is',ctime(time())\n");
  Py_Finalize();
  return 0;
}

我最初尝试设置

CPPFLAGS=-I/usr/include/python2.7
LDFLAGS=-L/usr/lib/python2.7

但是依然失败

  • 设置编译环境

参考 Can't find Python.h already installed python-dev

果然不再报告找不到Python.h头文件了

另外一种解决方法参考 Call Python from within C 使用

但是改成报错

参考 Undefined reference - despite lib being found by linker ,是因为Py_Initialize需要符号表,从 libpython2.7.a加载,则需要传递编译参数 -lpython2.7

不过,依然报错。

https://ubuntuforums.org/showthread.php?t=987124 有一个解决方法

类似 https://bbs.csdn.net/topics/392185678 也是同样解决思路。

修改了环境参数,将

修改成

就可以编译成功

另外,参考 https://ubuntuforums.org/showthread.php?t=987124 的简单代码 (实际上是 https://docs.python.org/2/extending/embedding.html#very-high-level-embedding

参考

Last updated

Was this helpful?