Skip to content

Tutorial, tips, and tricks for wrapping C/C++ projects into Python using ctypes.

License

Notifications You must be signed in to change notification settings

gajanan-choudhary/ctypes-tutorial

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

13 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

ctypes-tutorial

Tutorial, tips, and tricks for wrapping C/C++ projects into Python using ctypes.

Table of contents

General info

This repository serves as an example on how to smartly wrap a C project into Python using ctypes, the in-built foreign-function Python library. The motivation of this work is to modernize C software and libraries by wrapping them into Python.

Setup

Dependencies

  • Bash
  • C compiler -- has been tested on gcc.
  • Python 2.7 -- for now; support for other versions of Python may be added later.

Usage

CMake is used to build this project. Run the following commands from your terminal to build the library and the Python interface:

mkdir build
cd build
cmake -DMAKE_SHARED_LIBRARY=ON ..
make
cd ..

Then run python from the root directory, import the pybinarytree package and use it. For example:

import pybinarytree as pbt
tree = pbt.Tree()
tree.insertVal([-1, 2, 0, 6, 1, 221, -3, 5, 8, 7])
tree.showOrdered()

Features

List of features that are ready:

  • Demonstrates examples of how an entire project can be wrapped into ctypes smartly
  • Demonstrates how to handle preprocessor-directive-dependent code (#ifdef ...)
  • Demonstrates pitfalls of wrapping C code using ctypes

Status

Project is: in progress.

License

This repository is is unlicensed. See the LICENSE file in the main/root directory of this repository, or visit https://unlicense.org for details.

This repository uses and builds upon another code by the same author (Gajanan Choudhary), named binarytree. That code is also unlicensed.

Contributor

Acknowledgments

  • ctypes documentation
  • Stackoverflow

About

Tutorial, tips, and tricks for wrapping C/C++ projects into Python using ctypes.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published