본문 바로가기

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