Getting started with GCC plugins

So, after searching the Internet for a while, I decided to give it a try here as it seems to be a good forum for discussion. I am trying to create a simple gcc plugin. The program code is attached at the end of this letter, but in plain English, it registers the plug-in and ensures that the pragma_init function is called when pragmas are registered. This is where I use c_register_pragma to intercept some pragmas.

I will compile it using the example at http://gcc.gnu.org/onlinedocs/gccint/Plugins-building.html#Plugins-building . Compilation and layout work fine. However, when I load the plug-in, I get:

gcc -c -fplugin=plugin.so test.c -o test.o cc1: error: cannot load plugin plugin.so plugin.so: undefined symbol: warning 

What am I doing wrong? Also, when I include some header files (which will be needed later), I get a lot of errors. For example, including "tree.h" gives (among 50 other errors):

 /machmode.h:262:1: error: unknown type name 'class' class bit_field_mode_iterator ^ /machmode.h:263:1: error: expected '=', ',', ';', 'asm' or '__attribute__' before '{' token { ^ /plugin/include/tree.h:27:0, from conftest.c:63: /vec.h:220:8: error: field 'register_overhead' declared as a function 

Does anyone know what I'm doing wrong?

thanks

+7
source share
2 answers

There are two problems here:

Error: "Unable to load plugin plugin.so" means that you must add to your LD_LIBRARY_PATH the directory in which you store the new plug-in with the shared library.

Hundreds of errors you got with all the files in include are resolved on my computer if you compile g ++ instead of gcc (not sure to understand why you thought)

+3
source

What version of GCC are you using, both to compile your plugin and to use the plugin? Just skip

  gcc -v 

without any other program argument to find out!

Has the appropriate package for developing GCC plugins been installed (on Debian or Ubuntu it can be gcc-4.7-plugin-dev , but adapt version 4.7 to your specific version of GCC)?

Have you installed all the dependencies needed to create GCC (on Debian or Ubuntu, apt-get build-dep gcc-4.7 gcc-4.7-plugin-dev )?

Recent versions of GCC (especially many GCC 4.7 shipped by distributions and all GCC 4.8) are compiled by the C ++ compiler, not the C compiler.

You can check how your GCC was created (in C or C ++) by running

 nm -D -C $(gcc -print-file-name=cc1) 

If this command shows C ++ typed names, for example, execute_ipa_pass_list(opt_pass*) instead of just execute_ipa_pass_list your GCC was compiled using the C ++ compiler (possibly g++ )

Thus, you may need to use g++ (not gcc ) to compile your GCC plugin.

As I commented, have you considered using MELT (a domain specific language for the GCC extension) to extend or customize your gcc compiler?

I suggest downloading the latest http://gcc-melt.org/melt-plugin-snapshot.tar.bz2 since I will release the next MELT in a few weeks for GCC 4.7 and 4.8

And don't expect parsing behavior to change in your GCC with the plugin. This is actually not possible (GCC provides only plugins to add your inline and pragmatic elements, and not to extend the parsing syntax).

+2
source

All Articles