Cách gọi hàm C từ Python
2 phút đọc27/08/2025
Reading Time: 2 minutes
Bạn muốn tận dụng tốc độ xử lý của ngôn ngữ C nhưng vẫn giữ sự linh hoạt của Python? Bài viết này sẽ hướng dẫn chi tiết cách gọi hàm C từ Python, giúp bạn viết code hiệu quả, tối ưu hiệu suất mà không mất đi sự tiện lợi của Python.
Bạn có thể gọi một hàm C từ Python bằng cách sử dụng module ctypes.
Quy trình này gồm các bước sau:
- Tạo một tệp C (có đuôi .c) với các hàm cần thiết.
- Tạo một tệp shared library (có đuôi .so) bằng trình biên dịch C.
- Trong chương trình Python, tạo một instance của ctypes.CDLL từ tệp shared library.
- Cuối cùng, gọi hàm C theo định dạng:
{CDLL_instance}.{tên_hàm}({các_tham_số_của_hàm}).
Bước 1: Tạo tệp C với các hàm
#include
int square(int i) {
return i * i;
}
Tạo tệp C với các hàm Chúng ta có một hàm C đơn giản sẽ trả về bình phương của một số nguyên. Mã hàm này được lưu trong tệp có tên là my_functions.c.
Bước 2: Tạo tệp Shared Library
Bạn có thể dùng lệnh sau để tạo tệp shared library từ tệp nguồn C.
$ cc -fPIC -shared -o my_functions.so my_functions.c
Bước 3: Gọi hàm C từ chương trình Python
>>> from ctypes import *
>>> so_file = "/Users/pankaj/my_functions.so"
>>> my_functions = CDLL(so_file)
>>>
>>> print(type(my_functions))
>>>
>>> print(my_functions.square(10))
100
>>> print(my_functions.square(8))
64
>>>

Nếu bạn thay đổi tệp chương trình C, bạn sẽ phải tạo lại tệp shared library.
Kết luận
Cách triển khai Python mặc định được viết bằng ngôn ngữ lập trình C và được gọi là CPython. Vì vậy, việc sử dụng các hàm C trong một chương trình Python không phải là điều hiếm gặp. Trong bài hướng dẫn này, chúng ta đã học cách dễ dàng gọi các hàm C trong một chương trình Python.
Về tác giả

Bao TranWeb Developer
I’m passionate about web development and sharing my insights through articles, with over 8 years of experience. I hope these sharings inspire you and help build a strong web development community.
@#@
Tôi đam mê phát triển web và chia sẻ những hiểu biết của mình thông qua các bài viết, với hơn 8 năm kinh nghiệm. Tôi hy vọng những chia sẻ này sẽ truyền cảm hứng cho các bạn và giúp xây dựng một cộng đồng phát triển web mạnh mẽ.
Cập nhật thông tin mới nhấtNhận các thông tin mới nhất về mối đe dọa, báo cáo an ninh mạng từ CyStack về hòm thư điện tử của bạn
{"success":true,"head":"<title>Cách gọi hàm C từ Python - CyStack Tutorial</title>\n<meta name=\"description\" content=\"Tìm hiểu cách gọi hàm C từ Python bằng ctypes và cffi. Hướng dẫn chi tiết giúp tăng tốc chương trình Python với code C hiệu quả\"/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-video-preview:-1, max-image-preview:large\"/>\n<link rel=\"canonical\" href=\"https://blog.cystack.org/tutorial/2025/08/27/cach-goi-ham-c-tu-python/\" />\n<meta property=\"og:locale\" content=\"en_US\" />\n<meta property=\"og:type\" content=\"article\" />\n<meta property=\"og:title\" content=\"Cách gọi hàm C từ Python - CyStack Tutorial\" />\n<meta property=\"og:description\" content=\"Tìm hiểu cách gọi hàm C từ Python bằng ctypes và cffi. Hướng dẫn chi tiết giúp tăng tốc chương trình Python với code C hiệu quả\" />\n<meta property=\"og:url\" content=\"https://blog.cystack.org/tutorial/2025/08/27/cach-goi-ham-c-tu-python/\" />\n<meta property=\"og:site_name\" content=\"CyStack Tutorial\" />\n<meta property=\"article:tag\" content=\"vi\" />\n<meta property=\"article:section\" content=\"Python\" />\n<meta property=\"og:image\" content=\"https://s2.cystack.net/tutorial/27160212/cach-goi-ham-c-tu-python.jpg\" />\n<meta property=\"og:image:secure_url\" content=\"https://s2.cystack.net/tutorial/27160212/cach-goi-ham-c-tu-python.jpg\" />\n<meta property=\"og:image:width\" content=\"1200\" />\n<meta property=\"og:image:height\" content=\"630\" />\n<meta property=\"og:image:alt\" content=\"Cách gọi hàm C từ Python\" />\n<meta property=\"og:image:type\" content=\"image/jpeg\" />\n<meta property=\"article:published_time\" content=\"2025-08-27T16:08:49+07:00\" />\n<meta name=\"twitter:card\" content=\"summary_large_image\" />\n<meta name=\"twitter:title\" content=\"Cách gọi hàm C từ Python - CyStack Tutorial\" />\n<meta name=\"twitter:description\" content=\"Tìm hiểu cách gọi hàm C từ Python bằng ctypes và cffi. Hướng dẫn chi tiết giúp tăng tốc chương trình Python với code C hiệu quả\" />\n<meta name=\"twitter:image\" content=\"https://s2.cystack.net/tutorial/27160212/cach-goi-ham-c-tu-python.jpg\" />\n<meta name=\"twitter:label1\" content=\"Written by\" />\n<meta name=\"twitter:data1\" content=\"Bao Tran\" />\n<meta name=\"twitter:label2\" content=\"Time to read\" />\n<meta name=\"twitter:data2\" content=\"1 minute\" />\n<script type=\"application/ld+json\" class=\"rank-math-schema\">{\"@context\":\"https://schema.org\",\"@graph\":[{\"@type\":[\"Person\",\"Organization\"],\"@id\":\"https://blog.cystack.org/tutorial/#person\",\"name\":\"CyStack Tutorial\"},{\"@type\":\"WebSite\",\"@id\":\"https://blog.cystack.org/tutorial/#website\",\"url\":\"https://blog.cystack.org/tutorial\",\"name\":\"CyStack Tutorial\",\"publisher\":{\"@id\":\"https://blog.cystack.org/tutorial/#person\"},\"inLanguage\":\"en-US\"},{\"@type\":\"ImageObject\",\"@id\":\"https://s2.cystack.net/tutorial/27160212/cach-goi-ham-c-tu-python.jpg\",\"url\":\"https://s2.cystack.net/tutorial/27160212/cach-goi-ham-c-tu-python.jpg\",\"width\":\"1200\",\"height\":\"630\",\"caption\":\"C\\u00e1ch g\\u1ecdi h\\u00e0m C t\\u1eeb Python\",\"inLanguage\":\"en-US\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https://blog.cystack.org/tutorial/2025/08/27/cach-goi-ham-c-tu-python/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":\"1\",\"item\":{\"@id\":\"https://blog.cystack.org/tutorial\",\"name\":\"Home\"}},{\"@type\":\"ListItem\",\"position\":\"2\",\"item\":{\"@id\":\"https://blog.cystack.org/tutorial/category/python/\",\"name\":\"Python\"}},{\"@type\":\"ListItem\",\"position\":\"3\",\"item\":{\"@id\":\"https://blog.cystack.org/tutorial/2025/08/27/cach-goi-ham-c-tu-python/\",\"name\":\"C\\u00e1ch g\\u1ecdi h\\u00e0m C t\\u1eeb Python\"}}]},{\"@type\":\"WebPage\",\"@id\":\"https://blog.cystack.org/tutorial/2025/08/27/cach-goi-ham-c-tu-python/#webpage\",\"url\":\"https://blog.cystack.org/tutorial/2025/08/27/cach-goi-ham-c-tu-python/\",\"name\":\"C\\u00e1ch g\\u1ecdi h\\u00e0m C t\\u1eeb Python - CyStack Tutorial\",\"datePublished\":\"2025-08-27T16:08:49+07:00\",\"dateModified\":\"2025-08-27T16:08:49+07:00\",\"isPartOf\":{\"@id\":\"https://blog.cystack.org/tutorial/#website\"},\"primaryImageOfPage\":{\"@id\":\"https://s2.cystack.net/tutorial/27160212/cach-goi-ham-c-tu-python.jpg\"},\"inLanguage\":\"en-US\",\"breadcrumb\":{\"@id\":\"https://blog.cystack.org/tutorial/2025/08/27/cach-goi-ham-c-tu-python/#breadcrumb\"}},{\"@type\":\"Person\",\"@id\":\"https://blog.cystack.org/tutorial/author/baotran/\",\"name\":\"Bao Tran\",\"url\":\"https://blog.cystack.org/tutorial/author/baotran/\",\"image\":{\"@type\":\"ImageObject\",\"@id\":\"https://secure.gravatar.com/avatar/0cdd33c02ec4a531fcf557b9c1ccc276df0c69031b2382c959bcf335248e840c?s=96&d=mm&r=g\",\"url\":\"https://secure.gravatar.com/avatar/0cdd33c02ec4a531fcf557b9c1ccc276df0c69031b2382c959bcf335248e840c?s=96&d=mm&r=g\",\"caption\":\"Bao Tran\",\"inLanguage\":\"en-US\"}},{\"@type\":\"BlogPosting\",\"headline\":\"C\\u00e1ch g\\u1ecdi h\\u00e0m C t\\u1eeb Python - CyStack Tutorial\",\"keywords\":\"C\\u00e1ch g\\u1ecdi h\\u00e0m C t\\u1eeb Python\",\"datePublished\":\"2025-08-27T16:08:49+07:00\",\"dateModified\":\"2025-08-27T16:08:49+07:00\",\"articleSection\":\"Python\",\"author\":{\"@id\":\"https://blog.cystack.org/tutorial/author/baotran/\",\"name\":\"Bao Tran\"},\"publisher\":{\"@id\":\"https://blog.cystack.org/tutorial/#person\"},\"description\":\"T\\u00ecm hi\\u1ec3u c\\u00e1ch g\\u1ecdi h\\u00e0m C t\\u1eeb Python b\\u1eb1ng ctypes v\\u00e0 cffi. H\\u01b0\\u1edbng d\\u1eabn chi ti\\u1ebft gi\\u00fap t\\u0103ng t\\u1ed1c ch\\u01b0\\u01a1ng tr\\u00ecnh Python v\\u1edbi code C hi\\u1ec7u qu\\u1ea3\",\"name\":\"C\\u00e1ch g\\u1ecdi h\\u00e0m C t\\u1eeb Python - CyStack Tutorial\",\"@id\":\"https://blog.cystack.org/tutorial/2025/08/27/cach-goi-ham-c-tu-python/#richSnippet\",\"isPartOf\":{\"@id\":\"https://blog.cystack.org/tutorial/2025/08/27/cach-goi-ham-c-tu-python/#webpage\"},\"image\":{\"@id\":\"https://s2.cystack.net/tutorial/27160212/cach-goi-ham-c-tu-python.jpg\"},\"inLanguage\":\"en-US\",\"mainEntityOfPage\":{\"@id\":\"https://blog.cystack.org/tutorial/2025/08/27/cach-goi-ham-c-tu-python/#webpage\"}}]}</script>\n"}