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
| 번호 | 제목 | 날짜 | 조회 수 |
|---|---|---|---|
| HOT | [TIP] Jedis로 원격지 Redis접속에 실패할때 | 2017.09.27 | 10881 |
| HOT | [TIP] 앱 실행시 관리자 권한을 얻도록 Visual Studio에서 설정하기 | 2017.01.22 | 10502 |
| » | [TIP] 파이선에서 C 코드 호출하기 (How to call C function from Python) [1] | 2018.08.14 | 10121 |
| 13 | [LINK]코드와 한글 [Code and Hangul] | 2004.04.27 | 730 |
| 12 | [TIP] mfc42u.lib 링크 에러시... | 2004.04.07 | 880 |
| 11 | Visual C++ 단축키 정리 [퍼옴] | 2004.01.27 | 2415 |
| 10 | [TIP]링크 클릭시 나는 사운드 없애기 | 2003.10.24 | 1102 |
| 9 | [TIP]마우스 오른쪽 버튼 막은 사이트서 페이지 소스 보기 | 2003.08.07 | 983 |
| 8 | [TIP]레지스트리 관련 팁들 (2) | 2003.07.25 | 833 |
| 7 | [TIP]Active-x 관련 팁들 | 2004.01.15 | 749 |
| 6 | [TIP] Linux 부팅시 x window 혹은 콘솔이 뜨도록 변경 | 2003.04.04 | 840 |
| 5 | [TIP] NT, LINUX 멀티 부팅 하기 | 2003.04.04 | 872 |
| 4 | [TIP] 윈도우의 콘솔창(cmd)에서 TAB키 활용 | 2003.02.20 | 1699 |
| 3 | IE 스킨 바꾸기 가이드라인 [1] | 2000.08.14 | 1395 |
| 2 | Windows 95 단축키와 오류 고치기 | 1999.06.06 | 1041 |
| 1 | 좋은 Tool 많이 다룰수록 좋쥐요. | 1999.06.05 | 1242 |
https://pgi-jcns.fz-juelich.de/portal/pages/using-c-from-python.html