메뉴 건너뛰기

개발도구


1. 호출할 C 코드를 test.c 파일이라는 이름으로 작성합니다. (Write C callee functions like belows)

int foo(int a)
{
    return a + 5;
}

char *bar(char *c) {
    return c;
}

2. test.c 파일을 빌드합니다. (Type build command like below)

gcc -o test.so -shared -fPIC test.c


3. 파이선 코드 caller.py 를 작성합니다. (Write Python caller code like belows)

import ctypes

test_c_codes = ctypes.cdll.LoadLibrary("test.so")
foo_c_func = test_c_codes.foo
bar_c_func = test_c_codes.bar
bar_c_func.restype = ctypes.c_char_p

print foo_c_func(10)
print bar_c_func("airpage")


4. 라이브러리 경로를 가르키는 환경변수를 설정합니다 (Set environment variable to so file's location)

$ set LD_LIBRARY_PATH = $LD_LIBRARY_PATH:./


5. caller.py 를 실행해 봅니다. (Type execution command like below and check the result)

$ python caller.py

15
airpage







번호 제목 날짜 조회 수
공지 [TIP] Jedis로 원격지 Redis접속에 실패할때 2017.09.27 7038
공지 [TIP] 앱 실행시 관리자 권한을 얻도록 Visual Studio에서 설정하기 2017.01.22 6556
» [TIP] 파이선에서 C 코드 호출하기 (How to call C function from Python) [1] 2018.08.14 6421
53 [LINK] distcc로 컴파일 시간 줄이기 (번역) 2009.02.04 1413
52 [TIP] 윈도우 시작시 내문서가 항상 열려 있을때... 2008.12.07 1375
51 [LINK] Process별로 사용중인 Port 보기 유틸리티 2008.11.21 1210
50 [TIP] IE 6 혹은 7에서 스크립트 실행 차단 블록 해제하기 [1] 2008.06.16 2704
49 [TIP] Excel Data 에서 Carriage return 제거하기 2008.02.21 1352
48 [TIP] Visual C 에서 C1083 에러 관련... 2008.02.20 1173
47 [TIP] Windows 에서 ARM 시뮬레이터 사용하기 2007.12.16 1480
46 [TIP] Make 빌드시 출력메세지 기록하기 2008.02.20 1215
45 [TIP] Command Shell 에서 Registry 다루는 방법 2007.10.04 1641
44 [TIP] Clock skew detected. Your build may be incompleted. 2007.10.03 3785
43 [TIP] MCP 파일을 Makefile 파일로 변환하는 유틸 2007.08.15 835
42 [TIP] LoadLibrary 가 dll 을 탐색하는 순서 2007.08.07 1418
41 [TIP] kernel panic: VFS: unable to mount root fs on ... 2007.04.18 1094
40 [TIP] DSW를 새로운 VC6에서 띄우기 2007.01.06 1123
39 HTML Help 파일 만드는 툴(Microsoft HTML Help) 2006.06.16 993
38 [TIP] Linux Bootloader 복구 방법 2005.09.29 698
37 pal/gal 관련 질문드릴께요...^^ [1] 2005.07.09 616
36 [LINK] ARM Simulator (VM-arm) 2005.06.13 801
35 [LINK] GTK 크로스 컴파일 방법 및 기타등등~ 2005.05.06 701
34 GBIOChecker [2] 2005.04.20 571
위로