c
cairo
This manual documents the API used by C and C++ programmers who want to write extension modules that use pycairo.
To compile a Python extension using Pycairo you need to know where Pycairo and cairo are located and what flags to pass to the compiler and linker.
Variant:
Only available since version 1.16.0.
While Pycairo installs a pkg-config file, in case of virtualenvs, installation to the user directory or when using wheels/eggs, pkg-config will not be able to locate the .pc file. The
get_include
function should work in all cases, as long as Pycairo is in your Python search path.- Compiler Flags:
python -c "import cairo; print(cairo.get_include())"
pkg-config --cflags cairo
- Linker Flags:
pkg-config --libs cairo
Variant:
This works with older versions, but with the limitations mentioned above. Use it as a fallback if you want to support older versions or if your module does not require virtualenv/pip support.
- Compiler Flags:
pkg-config --cflags pycairo
orpkg-config --cflags py3cairo
- Linker Flags:
pkg-config --libs pycairo
orpkg-config --libs py3cairo
Edit the client module file to add the following lines:
/* All function, type and macro definitions needed to use the Pycairo/C API
* are included in your code by the following line
*/
#include "pycairo.h"
/* define a variable for the C API */
static Pycairo_CAPI_t *Pycairo_CAPI;
/* import pycairo - add to the init<module> function */
Pycairo_IMPORT;
Example showing how to import the pycairo API:
#include "py3cairo.h"
PyMODINIT_FUNC
PyInit_client(void)
{
PyObject *m;
m = PyModule_Create(&clientmodule);
if (m == NULL)
return NULL;
if (import_cairo() < 0)
return NULL;
/* additional initialization can happen here */
return m;
}
These are only listed here so they can be referenced in the documentation.
See https://www.cairographics.org/manual/ for details.