how to write a DLL/SO in C/C++ for Python
2013-10-06 12:43
627 查看
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.
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:
If you are using C++ call the file test.cpp and write:
If you are using Windows,
The
Note: in order to make the code more readable and avoid repetitions, it is a good idea to use a
The header of the function can be added to test.h (but it's not necessary):
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:\Programs\Microsoft Visual Studio 8\VC) with the /LD option:
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:
Note: the
Note: You can also use Dependency Walker or similar programs to see the list of the exported functions and check if the
Use the
On Windows,
Note: ctype module is already included from Python 2.5. If you are using an older version you can download ctypes here.
We can now use mydll to access the sum function:
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.
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
#defineas shown in the example
The header of the function can be added to test.h (but it's not necessary):
//test.h int sum(int, int);
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:\Programs\Microsoft Visual Studio 8\VC) 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
-sharedoption 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
sumfunction is there.
Use the
ctypesmodule to access the DLL:
>>> from ctypes import cdll >>> mydll = cdll.LoadLibrary('test.dll') >>> mydll<CDLL 'test.dll', handle 10000000 at b92310>
On Windows,
cdll.LoadLibrarywill 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.
We can now use mydll to access the sum function:
>>> mydll.sum<_FuncPtr object at 0x00AF6918> >>> mydll.sum(5, 3)8
相关文章推荐
- How to check for NAN / INF / IND in C++
- How to debug unmanaged C++ dll in C#
- how to write it in C++?
- How to compile a C/C++ DLL for 64 bit with Visual Studio?
- How to Use the TimeDistributed Layer for Long Short-Term Memory Networks in Python 译文
- A good blog about how to write an Hadoop MapReduce program in Python
- How to Use the Dynamic Link Library in C++ Linux (C++调用Delphi写的.so文件)
- how to locate dll in native c++ world / dotnet world?
- How&nbsp;to&nbsp;write/use&nbsp;DLL&nbsp;in&nbsp;Linux&nbsp;sy…
- How can I force Python's file.write() to use the same newline format in Windows as in Linux (“\r\n”
- how to call c++ function in python coding in linux?
- 《How to Use the TimeDistributed Layer for Long Short-Term Memory Networks in Python》学习笔记
- How to write own add-in for SSMS 2012 (Final release version)
- How to separate the implementation and definition for template function in c++
- How to write Lossfunction in PythonCaffe
- How to load the specified mscordacwks.dll for managed debugging when multiple .NET runtime are loaded in one process
- [Python]How to handle the exception in Python?
- How to create a DLL library in C and then use it with C#
- What is the best way to implement a heartbeat in C++ to check for socket connectivity?
- C++--How to configure Visual Studio for compiling drivers