/컴퓨터시스템/Linker 에서 Linker - Windows에서 Static Linking, Dynamic Linking 실습 1,2 를 진행해보았다. 

 

그중에, Linker - Windows에서 Static Linking, Dynamic Linking 실습 2 에서는 명시적 호출을 이용하여 dll을 호출하는  방법을 실습하였다. 

 

Python 인터프리터는 C로 작성되었기에, ctypes 모듈을 호출한다면, 파이썬에서 C/C++ 로 작성된 dll 파일의 함수를 사용할 수 있다.  

 

아래는 지난 실습에서 작성한 DynamicLib_Test.dll에 선언 및 정의된 multiply_four(int num)을 PythonApplication1.py에서 사용하는 예제이다. 

 

DynamicLib_Test (C/C++)

 

<header.h>

extern "C" __declspec(dllexport) int multiply_four(int n);

<src.cpp>

#include "header.h"

extern "C" __declspec(dllexport) int multiply_four(int n)
{
  return (4 * n);
}

하면, DynamicLib_Test.dll이 빌드됨. 

 

PythonApplication1.py (Python 3.9.2)

import ctypes 

dynamic_dll = ctypes.WinDLL('DynamicLib_Test.dll')
myfunc = dynamic_dll['multiply_four']
result = myfunc(100)
print(result)

 

+ Recent posts