Skip to content

RobertoLorenzoAguilar/UsingCorCplusplusFunctions-inPython

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

54 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Using C or C++ Functions in Python

How you can call functions in c or c++ since python language?

although exists some different ways, I will show you how you can call your functions through dlls

The first step is generate the dll file.

In my case, I will reproduce a little one example into the windows operating system.

I take advantage of the Visual Studio C/C++ IDE and Compiler for Windows.

image

if you want only you can install only the MSBuild on the command line - C++. without the necessity to install the Visual Studio IDE complete https://docs.microsoft.com/en-us/cpp/build/building-on-the-command-line?view=msvc-160.

But in my case, because I have some problems I decided to isolate the problem first started to compile one "Hello World" in the IDE.

note: before I had a lot of problems with https://www.msys2.org/ who provide a collection of tools and libraries of Linux to run in windows, but really I don't tried with Window Subsystem Linux sounds good well, possible I will make a test in the future.

prerequisites

you need have install the:

          -python3 ("my version is python 3, I didnt try with version 2")
          -python install pip -----> this is the python package manager
          -python3 -m pip install invoke -----> we need this tool to invoke  enviroment vcvars32.bat

please dont forget set the system enviroments variables.

image

As of now you can interact with the board.

The next step will be to try to create a DLL to embedded in other kind of project in a Graphical user interface. this is my main.cpp

image

this is my main.h remember expouse the param -D Expouse_Functions

image

here you can see in my method the way to create the .dll

image

you should open the project folder and run in cmd.

invoke clean
invoke build-main

remember always clean

Here I started to test the DLL in python, is simple string comparing. image

If you have this problem, it's because you built the project in other architecture different from what you using in your python interpreter, please change in the tasks.py the line:

 -path = f'"{path}vcvars32.bat" x86'  # Enter the VS venv 32 bits
 -path = f'"{path}vcvars32.bat" x64'  # Enter the VS venv 64 bits

image

Well, the one of important thing that I recommended you is to install the next program, which will help you can see if your function is accessible, it is necessary when you want to call the function of your dll.

image

TODO: Pending by load.

1.- the references.
2.- I need Upload this code how to create a DLL
3.- try to recrate the functionality using a python library in place of dll.
4.- Create GUI

About

How you can call functions in c or c++ since python language

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published