zoukankan      html  css  js  c++  java
  • python c扩展

    improt xxx时,python 会在一些路径中寻找:http://docs.python.org/2/tutorial/modules.html


    When a module named spam is imported, the interpreter first searches for a built-in module with that name. If not found, it then searches for a file named spam.py in a list of directories given by the variable sys.pathsys.path is initialized from these locations:

    • the directory containing the input script (or the current directory).
    • PYTHONPATH (a list of directory names, with the same syntax as the shell variable PATH).
    • the installation-dependent default.

    After initialization, Python programs can modify sys.path. The directory containing the script being run is placed at the beginning of the search path, ahead of the standard library path. This means that scripts in that directory will be loaded instead of modules of the same name in the library directory. This is an error unless the replacement is intended. See sectionStandard Modules for more information.

    在python shell下

    import sys

    sys.path

    可以查看


    python模块3种形式 如模块名字为helloworld:

    helloworld.py 

    helloworld.pyc  

    helloworld.pyd(windows下的dll文件 只不过导出符号里有 inithelloworld 入口函数,用以初始化模块)

    helloworld.so(unix下共享库,同window的dll)


    写一个hello.c:

    #include "Python.h"
    
    static PyObject* helloworld(PyObject* self)
    {
    return Py_BuildValue("s", "Hello, Python extensions!!");
    }
    
    static char helloworld_docs[] =
    "helloworld( ): Any message you want to put here!!
    ";
    
    static PyMethodDef helloworld_funcs[] = {
    {"helloworld", (PyCFunction)helloworld,
    METH_NOARGS, helloworld_docs},
    {NULL}
    };
    
    __declspec(dllexport) void inithelloworld(void)
    {
    Py_InitModule3("helloworld", helloworld_funcs,
    "Extension module example!");
    
    }

    我们省去添加include路径的麻烦,直接把hello.c 放到 python 安装目录的 include 目录下

    注意到 pthon27 的编译环境是vs2008

    而python32 的编译环境是vs2010

    这里用的是27,打开vs2008的命令提示控制台,切到 hello.c目录:

    F:Win7-appPython27include>cl.exe /c hello.c
    用于 80x86 的 Microsoft (R) 32 位 C/C++ 优化编译器 15.00.21022.08 版
    版权所有(C) Microsoft Corporation。保留所有权利。
    
    hello.c
    
    F:Win7-appPython27include>ls | grep hell
    hello.c
    hello.obj
    
    F:Win7-appPython27include>link /dll hello.obj
    Microsoft (R) Incremental Linker Version 9.00.21022.08
    Copyright (C) Microsoft Corporation. All rights reserved.
    
    LINK : fatal error LNK1104: 无法打开文件“python27.lib”

    注意  /c 生成 只生成目标文件hello.obj

    link时出错: 没有指定库路径 ,要链接 python27.lib

    于是 

    F:Win7-appPython27include>link /dll hello.obj /OUT:helloworld.dll/LIBPATH:../libs
    Microsoft (R) Incremental Linker Version 9.00.21022.08
    Copyright (C) Microsoft Corporation.  All rights reserved.
    
       正在创建库 helloworld.lib 和对象 helloworld.exp
    
    F:Win7-appPython27include>ls | grep hello
    hello.c
    hello.obj
    helloworld.dll
    helloworld.exp
    helloworld.lib

    生成helloword.dll了,我们所helloworld.dll 后缀改为 .pyd 现在可以用这个 python库文件了:

    Python 2.7.5 (default, May 15 2013, 22:43:36) [MSC v.1500 32 bit (Intel)] on win32
    Type "help", "copyright", "credits" or "license" for more information.
    >>> import helloworld
    >>> dir(helloworld)
    ['__doc__', '__file__', '__name__', '__package__', 'helloworld']
    >>> helloworld.helloworld()
    'Hello, Python extensions!!'
    >>>

    python为我们提供了 模块 distutilies.core 模块 构建安装 扩展模块:

    http://www.cnblogs.com/threef/p/3290324.html

    hello.c同目录下:写一个 setup.py

    然后运行 python setup.py install

    平台无关地构造安装扩展模块:

    from distutils.core import setup,Extension
    setup(name='helloworld', version='1.0',  
          ext_modules=[Extension('helloworld',['hello.c'])])
  • 相关阅读:
    DM8168通过GPMC接口与FPGA高速数据通信实现
    2016年 我在浙大计算机学院在职研究生学习经历
    CCS Font 知识整理总结
    Hexo 博客部署到 GitHub
    树莓派配置 USB 无线网卡
    树莓派搭建 Hexo 博客(二)
    树莓派搭建 Hexo 博客(一)
    树莓派初次使用的基本配置.md
    语法测试cnblogs使用Markdown
    硬件工程师-面试笔记0305
  • 原文地址:https://www.cnblogs.com/threef/p/3354757.html
Copyright © 2011-2022 走看看