ctypes cannot list the contents of a shared library. A loaded CDLL object only resolves symbols you ask for by name, so to see which functions a library exports you inspect the file with a tool such as nm -D, objdump -T or readelf on Linux, dumpbin /exports on Windows, and nm -gU on macOS. In Python you can then check a single symbol with hasattr(lib, "name") and read its signature from the C header.
The library file also does not store argument or return types. A symbol table gives names only, so the signatures must come from headers or documentation.
A ctypes.CDLL object looks up functions lazily. When you write lib.add, ctypes calls dlsym (or GetProcAddress on Windows) with that name. There is no API in ctypes to ask "which names exist?", and dir(lib) shows only the attributes you have already accessed.
The check works because accessing a missing symbol raises AttributeError, and hasattr turns that into False. It confirms existence only. It tells you nothing about the parameters.
Build a small test library first:
T marks a function in the text section that is exported. The static function hidden is absent because it has internal linkage, so ctypes could not reach it either. Other useful commands:
| Platform | Command | What it shows |
|---|---|---|
| Windows (MSVC tools) | dumpbin /exports mylib.dll | Exported function names and ordinals |
| Windows (MinGW) | objdump -p mylib.dll | Export table in the PE headers |
| macOS | nm -gU libmylib.dylib | Global defined symbols |
| macOS | otool -L libmylib.dylib | Dependent libraries |
On macOS, C symbols carry a leading underscore in nm output, for example _add, while ctypes still uses the name add.
If you want the list inside a script, run the system tool or use a parser library.
This works on Linux with binutils installed. Third-party packages such as pyelftools (ELF files) and pefile (Windows PE files) read the same tables without running external programs.
Symbol tables carry no types, so use the library's header file or documentation. C++ libraries add one more problem: names are mangled, for example _Z3addii. Use nm -DC to demangle them for reading, and ask the author for an extern "C" interface, since ctypes cannot call C++ methods directly.
Once you know the signature, declare it before calling:
Calling a function with the wrong declaration compiles and runs, but it can return garbage or crash, so verify against the header.
No. ctypes does not expose the symbol table. Use nm, objdump, dumpbin or a parser library, then check individual names with hasattr.
Read the header file or documentation. The binary only stores the symbol name, unless the library ships debug information such as DWARF data.
nm?It is probably declared static, inlined, or hidden by visibility flags such as -fvisibility=hidden. Only exported symbols can be loaded by ctypes.
The compiler mangles names to encode namespaces and parameter types. Export plain C functions with extern "C" to get readable names.
ctypes loads symbols by name and cannot list them. Inspect the library with nm -D, objdump -T, readelf, dumpbin /exports or nm -gU, test a name with hasattr, and take signatures from headers. Then set argtypes and restype before calling.