Interfaçage du C#, Python, C, C++, Go et Rust avec LabVIEW

63
Interfaçage du C#, Python, C, C++, Go et Rust avec LabVIEW 1 AlpesView - 10 octobre 2019

Transcript of Interfaçage du C#, Python, C, C++, Go et Rust avec LabVIEW

Page 1: Interfaçage du C#, Python, C, C++, Go et Rust avec LabVIEW

Interfaçage du C#, Python, C, C++, Go et Rust avec LabVIEW

1AlpesView - 10 octobre 2019

Page 2: Interfaçage du C#, Python, C, C++, Go et Rust avec LabVIEW

Sommaire

2

I. Interfaçage de fonction1. C2. C++3. Python4. Go5. Rust

II. Interfaçage d’objet1. Objet2. C#3. Python4. C++ vers Python (avec SWIG ou PyBind11)5. C++

III. Conclusion

IV. Reference

V. Annexe

Page 3: Interfaçage du C#, Python, C, C++, Go et Rust avec LabVIEW

I. Interfaçage de fonction1. C

2. C++

3. Python

4. Go

5. Rust

3

Page 4: Interfaçage du C#, Python, C, C++, Go et Rust avec LabVIEW

Des fonctions C dans LabVIEW

4

Page 5: Interfaçage du C#, Python, C, C++, Go et Rust avec LabVIEW

Fonctions C (1/2)Calc.c

Compilateur C

Calc.o ou Calc.obj

Compilateur C

(Linker/ création de lien)

Calc.dll sur Windows

Calc.so sur Linux

5Fonctions ● C ● C++ ● Python ● Go ● Rust

Page 6: Interfaçage du C#, Python, C, C++, Go et Rust avec LabVIEW

Fonctions C (2/2)

6Fonctions ● C ● C++ ● Python ● Go ● Rust

Page 7: Interfaçage du C#, Python, C, C++, Go et Rust avec LabVIEW

Des fonctions C++ dans LabVIEW

7

Page 8: Interfaçage du C#, Python, C, C++, Go et Rust avec LabVIEW

Fonctions C++ (1/3)

8

Ajout de l’instruction « extern C »

Fonctions ● C ● C++ ● Python ● Go ● Rust

Page 9: Interfaçage du C#, Python, C, C++, Go et Rust avec LabVIEW

Fonctions C++ (2/3)

9

Calc.cpp

Compilateur C++

Calc.o ou Calc.obj

Compilateur C++

(Linker/ création de lien)

Calc.so (sur Linux)

Calc.dll (sur Windows)

Fonctions ● C ● C++ ● Python ● Go ● Rust

Page 10: Interfaçage du C#, Python, C, C++, Go et Rust avec LabVIEW

Fonctions C++ (3/3)

10Fonctions ● C ● C++ ● Python ● Go ● Rust

Page 11: Interfaçage du C#, Python, C, C++, Go et Rust avec LabVIEW

Fonctions Python dans LabVIEW

Rq: à partir de LabVIEW 2018

11

Page 12: Interfaçage du C#, Python, C, C++, Go et Rust avec LabVIEW

Fonctions Python (1/1)

12

Arguments de la fonction

Type de variable en

sortie

Fonctions ● C ● C++ ● Python ● Go ● Rust

Page 13: Interfaçage du C#, Python, C, C++, Go et Rust avec LabVIEW

Go dans LabVIEW

13

Page 14: Interfaçage du C#, Python, C, C++, Go et Rust avec LabVIEW

Go dans LabVIEW (1/1)

14Fonctions ● C ● C++ ● Python ● Go ● Rust

Page 15: Interfaçage du C#, Python, C, C++, Go et Rust avec LabVIEW

Rust dans LabVIEW

15

Page 16: Interfaçage du C#, Python, C, C++, Go et Rust avec LabVIEW

Rust dans LabVIEW (1/1)

16Fonctions ● C ● C++ ● Python ● Go ● Rust

Voir les annexes pour Cargo et Rust

Page 17: Interfaçage du C#, Python, C, C++, Go et Rust avec LabVIEW

II. Interfaçage d’objet1. Objet2. C#3. Python4. C++ vers Python5. C++

17

Page 18: Interfaçage du C#, Python, C, C++, Go et Rust avec LabVIEW

Objet (1/1)

• Une classe Calc avec:• Une méthode addition

• Une méthode multiplication

18

Classe

Calc (a, b)

Méthode

addition

Méthode

mulitplicationObjet ● C# ● Python ● C++ → Python ● C++

Page 19: Interfaçage du C#, Python, C, C++, Go et Rust avec LabVIEW

Objet C# dans LabVIEW

19

Page 20: Interfaçage du C#, Python, C, C++, Go et Rust avec LabVIEW

Objet C# dans LabVIEW (1/3)

20Objet ● C# ● Python ● C++ → Python ● C++

Page 21: Interfaçage du C#, Python, C, C++, Go et Rust avec LabVIEW

Objet C# dans LabVIEW (2/3)

21

Calc.cs

Compilateur C#

Calc.dll

Objet ● C# ● Python ● C++ → Python ● C++

Page 22: Interfaçage du C#, Python, C, C++, Go et Rust avec LabVIEW

Objet C# dans LabVIEW (3/3)

22Objet ● C# ● Python ● C++ → Python ● C++

Page 23: Interfaçage du C#, Python, C, C++, Go et Rust avec LabVIEW

Objet Python dans LabVIEW

Rq: à partir de LabVIEW 2018

23

Page 24: Interfaçage du C#, Python, C, C++, Go et Rust avec LabVIEW

Objet Python (1/3)

24Objet ● C# ● Python ● C++ → Python ● C++

Page 25: Interfaçage du C#, Python, C, C++, Go et Rust avec LabVIEW

Objet Python (2/3)

classe.py

Ajout d’un script d’adaptation en Python (Wrapper)

Fichier .py

(classe + wrapper)

25Objet ● C# ● Python ● C++ → Python ● C++

Page 26: Interfaçage du C#, Python, C, C++, Go et Rust avec LabVIEW

Objet Python (3/3)

26Objet ● C# ● Python ● C++ → Python ● C++

Page 27: Interfaçage du C#, Python, C, C++, Go et Rust avec LabVIEW

C++ dans Python(avec SWIG ou PyBind11)

27

Page 28: Interfaçage du C#, Python, C, C++, Go et Rust avec LabVIEW

C++ dans Python

• Utilisation de SWIG qui permet d’interfacer du C++ ou du C avec:• Python

• Perl

• C#

• Java

• Ruby

• …

• Utilisation de PyBind11 pour interfacer du C++ vers Python

28Objet ● C# ● Python ● C++ → Python ● C++

Page 29: Interfaçage du C#, Python, C, C++, Go et Rust avec LabVIEW

C++ dans Python

Objet ● C# ● Python ● C++ → Python ● C++29

Page 30: Interfaçage du C#, Python, C, C++, Go et Rust avec LabVIEW

C++ dans Python

SWIG

(Simplified Wrapper and Interface Generator)

30

Page 31: Interfaçage du C#, Python, C, C++, Go et Rust avec LabVIEW

C++ dans Python (1/4)Simplified Wrapper and Interface Generator

31

Calc.i

Calc.h

Calc.cpp Compilateur Compilateur(Linker/Création de lien)

_Calc.so

_Calc.pyd

Calc_wrap.o

Calc.o

Fichier Objet

Calc.py

Wrapper

Rq: *.pyd = *.dll compatible avec la fonction import de Python

Calc_wrap.cxx

Objet ● C# ● Python ● C++ → Python ● C++

Page 32: Interfaçage du C#, Python, C, C++, Go et Rust avec LabVIEW

C++ dans Python (2/4)Simplified Wrapper and Interface Generator

32

Le fichier .i utilisé par SWIG pour générer le wrapper(SWIG générera le fichier Calc_wrap.cxx et Calc.py)

Objet ● C# ● Python ● C++ → Python ● C++

Page 33: Interfaçage du C#, Python, C, C++, Go et Rust avec LabVIEW

C++ dans Python (3/4)Simplified Wrapper and Interface Generator

33

Utilisation de MinGWpour la compilation

Objet ● C# ● Python ● C++ → Python ● C++

Page 34: Interfaçage du C#, Python, C, C++, Go et Rust avec LabVIEW

C++ dans Python (4/4)Simplified Wrapper and Interface Generator

34Rq: Les chemins de python sont pour CentOS 7Objet ● C# ● Python ● C++ → Python ● C++

Page 35: Interfaçage du C#, Python, C, C++, Go et Rust avec LabVIEW

C++ dans Python

Avec PyBind11

35

Page 36: Interfaçage du C#, Python, C, C++, Go et Rust avec LabVIEW

C++ dans Python avec PyBind11 (1/2)

36Objet ● C# ● Python ● C++ → Python ● C++

Page 37: Interfaçage du C#, Python, C, C++, Go et Rust avec LabVIEW

C++ dans Python avec PyBind11 (2/2)

37

Rq: Installer Pybind11 en utilisant:pip install pybind11

g++ -O3 -Wall -shared -std=c++11 -fpic 'python3 -m pybind11 --includes' Calc.cpp -o Calc'python3-config --extension-suffix'

Objet ● C# ● Python ● C++ → Python ● C++

cl Calc.cpp /EHsc /I C:\Python27\include /LD C:\Python27\libs\python27.libRq: renommer Calc.dll en Calc.pyd

g++ -c -fpic Calc.cpp -I C:\Python27\include -D_hypot=hypotg++ -shared *.o -o Calc.pyd -LC:\Python27\libs -lpython27

Avec Microsoft Visual Studio

Avec g++

Page 38: Interfaçage du C#, Python, C, C++, Go et Rust avec LabVIEW

Objet C++ dans LabVIEW

38

Page 39: Interfaçage du C#, Python, C, C++, Go et Rust avec LabVIEW

Objet C++

39Objet ● C# ● Python ● C++ → Python ● C++

Page 40: Interfaçage du C#, Python, C, C++, Go et Rust avec LabVIEW

Objet C++

• Objet C++ via Python (avec PyBind11)

• Objet C++ via C# (avec SWIG)

• Création d’un wrapper pour interfacer un objet C++ avec LabVIEW

40Objet ● C# ● Python ● C++ → Python ● C++

Page 41: Interfaçage du C#, Python, C, C++, Go et Rust avec LabVIEW

Objet C++

41

Page 42: Interfaçage du C#, Python, C, C++, Go et Rust avec LabVIEW

Objet C++via Python (1/3)

42

• SWIG

• PyBind11Classe

C++Wrapper

en PythonModule Python

LabVIEW

Objet ● C# ● Python ● C++ → Python ● C++

Page 43: Interfaçage du C#, Python, C, C++, Go et Rust avec LabVIEW

Objet C++via Python (2/3)

Classe C++

+

PyBind11

Compilateur C++

Fichier .so ou .dll compatible avec Python

Objet ● C# ● Python ● C++ → Python ● C++43

Page 44: Interfaçage du C#, Python, C, C++, Go et Rust avec LabVIEW

Objet C++via Python (3/3)

Objet ● C# ● Python ● C++ → Python ● C++44

Labview

Wrapper en Python (.py)

Fichier .so/.dll généré par Pybind11

Wrapper

Appelle de la librairie généré par PyBind11

(.dll ou .so)

Page 45: Interfaçage du C#, Python, C, C++, Go et Rust avec LabVIEW

Objet C++

45

Page 46: Interfaçage du C#, Python, C, C++, Go et Rust avec LabVIEW

Wrapperavec SWIG

Classe C++

CompilateurScript C#Librairie

.NETLabVIEW

Objet C++via C# (1/5)

46Objet ● C# ● Python ● C++ → Python ● C++

Page 47: Interfaçage du C#, Python, C, C++, Go et Rust avec LabVIEW

Objet C++via C# (2/5)

47

Calc.i

Calc.h

Calc.cpp

Compilateur C++ Compilateur C++(Linker/Création de lien)

module.dll

Wrapper

Calc_wrap.cxx

Fichier objet

Calc.cs module.cs

modulePINVOKE.cs

CompilateurC#

Calc_csharp.dll

Objet ● C# ● Python ● C++ → Python ● C++

Page 48: Interfaçage du C#, Python, C, C++, Go et Rust avec LabVIEW

Objet C++via C# (3/5)

48

SWIG n’autorise pas que le module est le même nom que la classe en C#

Calc.i

Calc.i pour générer le wrapper avec SWIG

Objet ● C# ● Python ● C++ → Python ● C++

Page 49: Interfaçage du C#, Python, C, C++, Go et Rust avec LabVIEW

Objet C++via C# (4/5)

49

Génération du wrapper avec SWIG

Objet ● C# ● Python ● C++ → Python ● C++

Page 50: Interfaçage du C#, Python, C, C++, Go et Rust avec LabVIEW

Objet C++via C# (5/5)

50Objet ● C# ● Python ● C++ → Python ● C++

Page 51: Interfaçage du C#, Python, C, C++, Go et Rust avec LabVIEW

Objet C++

Création d’un wrapper pour interfacer un objet C++ avec LabVIEW

51

Page 52: Interfaçage du C#, Python, C, C++, Go et Rust avec LabVIEW

Objet C++C++ avec un wrapper (1/3)

52

Calc.h

Calc.cpp

wrapper.h

wrapper.cpp

Objet ● C# ● Python ● C++ → Python ● C++

Page 53: Interfaçage du C#, Python, C, C++, Go et Rust avec LabVIEW

Objet C++C++ avec un wrapper (2/3)

53

• Calc.cpp• wrapper.cpp

Rq: Sur Linux remplacer « Calc.dll » par « Calc.so »Objet ● C# ● Python ● C++ → Python ● C++

Page 54: Interfaçage du C#, Python, C, C++, Go et Rust avec LabVIEW

Objet C++C++ avec un wrapper (3/3)

54Objet ● C# ● Python ● C++ → Python ● C++

Page 55: Interfaçage du C#, Python, C, C++, Go et Rust avec LabVIEW

Conclusion (1/2)

55

LabVIEW

.NET Node

Python Node

Call Library Node

Fonctions

Python

Bibliothèque C

Classe

C++

Wrapperen C++

WrapperSWIG

Module

Python

Wrapper en PythonClasse

Python

Bibliothèque

.NET

Bibliothèque de fonctions C++

« avec extern C »

Cython

Nuitka…

Fonctions en Rust, Go,…

Code C#

Page 56: Interfaçage du C#, Python, C, C++, Go et Rust avec LabVIEW

Conclusion (2/2)

56

C++

C

Python

Go

Rust

C#

WrapperC++

WrapperPython

Call Library Node

Call Library Node

Python Node

.NETNode

Call Library Node

Call Library Node

C++

C++

WrapperSWIG C#

WrapperPyBind11

Python Node

.NETNode

WrapperPython

Page 57: Interfaçage du C#, Python, C, C++, Go et Rust avec LabVIEW

Référence

57

• SWIG: http://www.swig.org/

• PyBind11: • https://github.com/pybind/pybind11• https://pybind11.readthedocs.io/en/master/

• Wrapper C/C++: https://nachtimwald.com/2017/08/18/wrapping-c-objects-in-c/

• Python avec C/C++: https://docs.python.org/fr/3/extending/building.html

• Python et ctypes: https://deptinfo-ensip.univ-poitiers.fr/ENS/doku/doku.php/stu:progc:ctypes

• Go:• https://medium.com/learning-the-go-programming-language/calling-go-functions-from-other-languages-4c7d8bcc69bf• https://golang.org/

• Rust: https://doc.rust-lang.org/1.2.0/book/rust-inside-other-languages.html

• Microsoft Visual Studio: • https://docs.microsoft.com/en-us/cpp/build/reference/compiler-options-listed-by-category?view=vs-2019• https://docs.microsoft.com/fr-fr/cpp/build/walkthrough-compiling-a-native-cpp-program-on-the-command-line?view=vs-2019• https://docs.microsoft.com/fr-fr/cpp/build/walkthrough-compile-a-c-program-on-the-command-line?view=vs-2019

Page 58: Interfaçage du C#, Python, C, C++, Go et Rust avec LabVIEW

AnnexeInstaller g++ et gcc sur Windows (1/2)Aller sur http://mingw-w64.org/doku.php/download, télécharger et installer la version MingW-W64-builds

Annexe ● g++/gcc ● Rust ● C#58

Page 59: Interfaçage du C#, Python, C, C++, Go et Rust avec LabVIEW

AnnexeInstaller g++ et gcc sur Windows (2/2)Modifier la variable d’environnement « Path » pour pointer vers les dossiers « bin » de MinGW: C:\Program Files (x86)\mingw-w64\i686-8.1.0-posix-dwarf-rt_v6-rev0\mingw32\bin (par exemple pour moi)

Annexe ● g++/gcc ● Rust ● C#59

Page 60: Interfaçage du C#, Python, C, C++, Go et Rust avec LabVIEW

AnnexeInstallation de Rust sur Windows (1/1)Aller sur https://www.rust-lang.org/tools/install

Annexe ● g++/gcc ● Rust ● C#60

Page 61: Interfaçage du C#, Python, C, C++, Go et Rust avec LabVIEW

AnnexeRust, Cargo et librairie (1/1)Créer un projet Cargo → https://doc.rust-lang.org/book/ch01-03-hello-cargo.html et https://doc.rust-lang.org/cargo/commands/cargo-new.html

Annexe ● g++/gcc ● Rust ● C#61

lib.rs

Fichier de configuration

Page 62: Interfaçage du C#, Python, C, C++, Go et Rust avec LabVIEW

AnnexeC# sur Windows (1/2)• Pour pouvoir compiler du C#, il faut installer Visual Studio :

https://visualstudio.microsoft.com/fr/ , ici on a utilisé la version « community »

• Une fois installé, ouvrir Visual Studio Installer, et installer « Développement .NET Desktop »

Annexe ● g++/gcc ● Rust ● C#62

Page 63: Interfaçage du C#, Python, C, C++, Go et Rust avec LabVIEW

AnnexeC# sur Windows (2/2)Si vous voulez utiliser les lignes de commande pour compiler du C#, il faut chercher les « prompt » dans le menu démarrer, et choisir la version qui vous convient (32bits ou 64 bits)

Annexe ● g++/gcc ● Rust ● C#63