Previous: Plugins tracking, Up: Plugins


23.10 Building GCC plugins

If plugins are enabled, GCC installs the headers needed to build a plugin (somewhere in the installation tree, e.g. under /usr/local). In particular a plugin/include directory is installed, containing all the header files needed to build plugins.

On most systems, you can query this plugin directory by invoking gcc -print-file-name=plugin (replace if needed gcc with the appropriate program path).

Inside plugins, this plugin directory name can be queried by calling default_plugin_dir_name ().

Plugins may know, when they are compiled, the GCC version for which plugin-version.h is provided. The constant macros GCCPLUGIN_VERSION_MAJOR, GCCPLUGIN_VERSION_MINOR, GCCPLUGIN_VERSION_PATCHLEVEL, GCCPLUGIN_VERSION are integer numbers, so a plugin could ensure it is built for GCC 4.7 with

     #if GCCPLUGIN_VERSION != 4007
     #error this GCC plugin is for GCC 4.7
     #endif

The following GNU Makefile excerpt shows how to build a simple plugin:

     GCC=gcc
     PLUGIN_SOURCE_FILES= plugin1.c plugin2.c
     PLUGIN_OBJECT_FILES= $(patsubst %.c,%.o,$(PLUGIN_SOURCE_FILES))
     GCCPLUGINS_DIR:= $(shell $(GCC) -print-file-name=plugin)
     CFLAGS+= -I$(GCCPLUGINS_DIR)/include -fPIC -O2
     
     plugin.so: $(PLUGIN_OBJECT_FILES)
        $(GCC) -shared $^ -o $@

A single source file plugin may be built with gcc -I`gcc -print-file-name=plugin`/include -fPIC -shared -O2 plugin.c -o plugin.so, using backquote shell syntax to query the plugin directory.

When a plugin needs to use gengtype, be sure that both gengtype and gtype.state have the same version as the GCC for which the plugin is built.