zoukankan      html  css  js  c++  java
  • C、C++中如何成功嵌入python

    • 修改lib文件名称,拷贝修改C:Python27libs目录下原来的python27.lib为python27_d.lib

    • 包含头文件在C:Python27include目录下

    • 包含lib文件

    • 调用64位lib库需要修改版本

    最简单的代码:

     1 #include "stdafx.h"
     2 #include "python.h"
     3 
     4 
     5 int _tmain(int argc, _TCHAR* argv[])
     6 {
     7     Py_Initialize();
     8     PyRun_SimpleString("print 'hello world!' ");
     9     Py_Finalize();
    10     getchar();
    11     return 0;
    12 }

    以main.c 调用 hellWorld.py 函数为例进行说明。
           helloWorld.py 的内容很简单只是定义了hello函数然后输出“Hello World”
     
     
    main.c 函数也比较简单,主要是初始化python,以及导入python的模块等
     
    里面的Python_Initialize()主要是初始化python解释器。
          Py_SimpleString("import sys")相当于在python中的import sys语句。
          Py_SimpleString("sys.path.append('./')")是将搜索路径设置为当前目录。
          Py_ImportModule("helloWorld")是利用导入文件函数将helloWorld.py函数导入。
          PyObject_GetAttrString(pModule, "hello")是在pyton引用模块helloWorld.py中查找hello函数。
    接着调用PyEval_CallObject(pFunc, NULL)调用hello函数。
          最后是清理python环境释放资源。

    http://blog.csdn.net/yelbosh/article/details/7495555
    http://blog.csdn.net/taiyang1987912/article/details/44779719
    http://blog.chinaunix.net/uid-20564848-id-73553.html
    http://www.2cto.com/kf/201411/352264.html
    http://www.cnblogs.com/linxr/archive/2011/07/22/2113843.html
  • 相关阅读:
    IDEA运行测试错误Failed to resolve org.junit.platform:junit-platform-launcher
    mysql索引基本原理
    as3.0声音波形系列03_十组合
    as3.0声音波形系列02_六组合
    as3.0声音波形系列01_八组合
    FiltersEffect(效果)
    AS3 TransitionManager 自带特效类
    as3 判断鼠标移动方向
    AS3代码生成xml方法
    求线段的交点
  • 原文地址:https://www.cnblogs.com/hushaojun/p/6063653.html
Copyright © 2011-2022 走看看