Skip to content

itsfuad/cpp-with-python

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 

Repository files navigation

C++ Library Integration with Python

This project demonstrates how to integrate a C++ shared library with a Python application using ctypes.

Files

  • lib.cpp: Contains the C++ code for the shared library.
  • app.py: Python script that loads and uses the shared library.
  • readme.md: This file.

Building the C++ Library

Windows

To compile the C++ code into a DLL on Windows, use the following command:

g++ -shared -o lib.dll lib.cpp

Linux

To compile the C++ code into a shared object on Linux, use the following command:

g++ -shared -o lib.so lib.cpp

Running the Python Script

Ensure the compiled shared library (lib.dll on Windows or lib.so on Linux) is in the same directory as app.py. Then, run the Python script:

python app.py

Functions

The C++ library provides the following functions:

  • int add(int a, int b): Returns the sum of a and b.
  • int sub(int a, int b): Returns the difference of a and b.
  • int mul(int a, int b): Returns the product of a and b.
  • int divide(int a, int b): Returns the quotient of a and b.

Example Output

1 + 2 = 3
4 * 5 = 20
6 - 3 = 3
10 / 2 = 5

Notes

  • Ensure you have the necessary compiler and Python environment set up.
  • Modify the path to the shared library in app.py if it is not in the same directory.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors