ctypes.cdll is a loader object in Python's standard ctypes module. It loads shared libraries (.so on Linux, .dylib on macOS, .dll on Windows) whose functions use the C cdecl calling convention, and it lets you call those functions directly from Python without writing an extension module. It works on every platform, unlike ctypes.windll, which exists only on Windows and uses the stdcall convention.
Each library you load is a ctypes.CDLL instance. Functions are looked up as attributes on it, and you describe their argument and return types so Python converts values correctly.
ctypes.cdll is an instance of ctypes.LibraryLoader configured to create CDLL objects. Two spellings do the same thing:
Direct construction with ctypes.CDLL is the form most code uses today, because it accepts extra arguments such as mode, use_errno and use_last_error. Attribute access such as ctypes.cdll.msvcrt works only when the attribute name matches a library the operating system can find, so it is mostly a Windows convenience.
| Loader | Calling convention | Platforms |
|---|---|---|
ctypes.cdll / ctypes.CDLL | cdecl | Windows, Linux, macOS |
ctypes.windll / ctypes.WinDLL | stdcall | Windows only |
ctypes.oledll / ctypes.OleDLL | stdcall, returns HRESULT | Windows only |
ctypes.pydll / ctypes.PyDLL | cdecl, keeps the GIL held | All |
On 64-bit Windows there is only one calling convention in practice, so the cdecl versus stdcall choice matters mainly for 32-bit builds.
Create a small C library:
Build it as a shared library:
On Windows, the function must be exported (for example with __declspec(dllexport)) or listed in a .def file, otherwise Python cannot find it. Then call it:
Using an absolute path avoids picking up the wrong library from the search path. Setting argtypes and restype is not optional in practice: without them ctypes assumes every argument and the return value are C int, which silently truncates 64-bit pointers and double values.
You can call system libraries too. On Unix-like systems, CDLL(None) gives you the symbols already loaded into the current process, which includes the C library:
ctypes.util.find_library("c") returns a name or path the loader can open, or None when nothing matches, so check the result before using it. Arguments passed as c_char_p must be bytes, not str.
ctypes.cdll libraries use the same type system as the rest of ctypes. Use ctypes.byref() or ctypes.POINTER() for output parameters, ctypes.Structure for C structs and ctypes.CFUNCTYPE() for callbacks:
Keep a reference to any callback object for as long as C code can call it, otherwise Python garbage-collects it and the program crashes.
OSError: cannot open shared object file: the file is missing, is not on the loader search path, or one of its own dependencies is missing. Use an absolute path.AttributeError: function not found: the name is wrong, is mangled (C++ code needs extern "C"), or is not exported on Windows.argtypes and restype.OSError: [WinError 193] or similar: the Python interpreter and the library have different bitness (32-bit versus 64-bit).PATH are no longer searched for dependent DLLs. Call os.add_dll_directory() for the folder that holds them.For anything larger than a few functions, consider cffi, Cython or a binding generator such as pybind11, which check types at build time.
cdll loads libraries that use the cdecl calling convention and works on all platforms. windll uses stdcall, which most classic Win32 API functions use, and exists only on Windows.
There is no supported unload call. The library stays loaded until the process exits, or you can use the platform loader directly (for example dlclose through ctypes) if you must, though that is risky.
CDLL releases the GIL while the C function runs, so other Python threads can run. The C code itself must be thread safe if you call it from several threads. Use PyDLL only for functions that call back into the Python C API.
Use ctypes for a few simple functions in an existing library. Use cffi, Cython, pybind11 or the C API when you need type checking, speed for many small calls or a maintained wrapper.
ctypes.cdll loads cdecl shared libraries on any platform and returns CDLL objects whose functions you call straight from Python. Always load by absolute path or find_library, declare argtypes and restype for every function, pass bytes for C strings, and keep callbacks referenced. Use windll only for stdcall Windows APIs.