zoukankan      html  css  js  c++  java
  • how to write a DLL/SO in C/C++ for Python

    http://wolfprojects.altervista.org/articles/dll-in-c-for-python/


    his article explains how to write a DLL/SO in C/C++ for Python

    Note: Cython can also be used to create and wrap C libraries for Python, and might be a good alternative to the approach explained in this article.

    1. Create a new file and write, for example, a function that sums two numbers and returns the result.

      If you are using C call the file test.c and write:

      //test.c
      __declspec(dllexport) int sum(int a, int b) {
          return a + b;
      }

      If you are using C++ call the file test.cpp and write:

      //test.cpp
      #define DLLEXPORT extern "C" __declspec(dllexport)
      
      DLLEXPORT int sum(int a, int b) {
          return a + b;
      }

      If you are using Windows__declspec(dllexport) is necessary to add the export directive to the object file and make the function accessible without using a .def file. If you are using Linux it can be omitted.

      The extern "C" construct prevents the compiler to add decorations on the functions' names in the DLL/SO and it is necessary while using C++.

      Note: in order to make the code more readable and avoid repetitions, it is a good idea to use a #define as shown in the example

    2. The header of the function can be added to test.h (but it's not necessary):

      //test.h
      int sum(int, int);
    3. Compile the program to obtain a DLL/SO.

      If you are using Windows and Visual Studio, create a new Dinamic-Link Library project and include the two files.

      Another way is to use the cl program located in the Visual Studio directory (e.g. C:ProgramsMicrosoft Visual Studio 8VC) with the /LD option:

      >cl /LD test.c
      [...]
      /out:test.dll
      /dll
      /implib:test.lib
      test.obj
         Creating library test.lib and object test.exp
      

      Note: cl use the file extension (.c or .cpp) to know if the source is written in C or C++.

      If you are using Linux, you can use gcc/g++ to compile the program and create the .so:

      gcc -Wall -Wextra -O -ansi -pedantic -shared test.c -o test.so

      Note: the -shared option is responsible to create the .so.

      Note: You can also use Dependency Walker or similar programs to see the list of the exported functions and check if the sum function is there.

    4. Use the ctypes module to access the DLL:

      >>> from ctypes import cdll
      >>> mydll = cdll.LoadLibrary('test.dll')
      >>> mydll<CDLL 'test.dll', handle 10000000 at b92310>

      On Windowscdll.LoadLibrary will search for the DLL in the CWD. On Linux instead, it is necessary to provide the path too:

      >>> from ctypes import cdll
      >>> mydll = cdll.LoadLibrary('/home/wolf/test.so')
      >>> mydll<CDLL '/home/wolf/test.so', handle 9ba7d30 at b7e55d2c>

      Note: ctype module is already included from Python 2.5. If you are using an older version you can download ctypes here.

    5. We can now use mydll to access the sum function:

      >>> mydll.sum<_FuncPtr object at 0x00AF6918>
      >>> mydll.sum(5, 3)8
  • 相关阅读:
    四、分布式账本技术对比
    二、主流区块链技术特点及Hyperledger Fabric V0.6版本特点
    一、Bitcoin比特币与BlockChain区块链技术
    java-信息安全(十七)-*.PFX(*.p12)&个人信息交换文件
    java-信息安全(十五)-单向认证
    java-信息安全(十六)-双向认证
    java-信息安全(十四)-初探SSL
    java-信息安全(十二)-数字签名【Java证书体系实现】
    java-信息安全(十三)-数字签名,代码签名【Java证书体系实现】
    移动端网页 rem css书写
  • 原文地址:https://www.cnblogs.com/threef/p/3353522.html
Copyright © 2011-2022 走看看