본문 바로가기

교육/Pyhotn 180709-180713

4일차 python 확장 - C 언어

Python 에서 C Library 사용

python 이 32bit 기본설치의 경우 아래의 경로에 python 관련 파일들이 존재함

%LocalAppData%\Programs\Python\Python37-32


C 로 작성된 DLL 을 python 에서 활용하기

DLL 만들기 
  1. Visual Studio Community 설치 (C++ 개발환경)
  2. 빈프로젝트 생성, 프로젝트 이름을 dll 파일명과 동일하게 하기
  3. 파일 생성, 파일명도 dll 파일명과 동일하기 하기
  4. 솔루션 구성 - Release, 솔루션 플랫폼 - 64bit or 84bit 설정하기
  5. 프로젝트 > 속성 > C/C++ 탭 > 추가 포함 디렉터리에 아래 경로 포함
    %LocalAppData%\Programs\Python\Python37-32\include
  6. 프로젝트 > 속성 > 링커 탭 > 추가 라이브러리 디렉토리에 아래 경로 포함
    %LocalAppData%\Programs\Python\Python37-32\libs
  7. 프로젝트 > 속성 > 일반 탭 >
    구성 형식을 동적라이브러리 (.dll) 로 변경
    대상 확장명을 .pyd (python dll)로 변경
  8. ctrl + shift + b 로 빌드하기

python 에서 활용하기

import extest

from ctypes import *


class Test(Structure): # c type 의 structure 만들기

    _fields_ = [('a', c_int), ('b', c_int), ('s', c_char_p)]


obj = Test(10, 20, b'test')


print(extest.strlen('abc'), extest.makeList(1, 2), extest.makeListFromStruct(addressof(obj)))



'교육 > Pyhotn 180709-180713' 카테고리의 다른 글

5일차 정규식  (0) 2018.07.13
4일차 sqlite  (0) 2018.07.12
3일차 library  (0) 2018.07.11
3일차 Class  (0) 2018.07.11
2일차 module  (0) 2018.07.10