7

So after searching the web for a while, Ive decided to try here as it seems to be a good forum for discussion. Im trying to create a simple gcc plugin. The program code is attached in the end of this mail, but in plain english it registers the plugin and makes sure that the pragma_init function is called when pragmas are registered. It is here that I use c_register_pragma to intercept some of the pragmas.

I compile it using the example in http://gcc.gnu.org/onlinedocs/gccint/Plugins-building.html#Plugins-building. The compilation and linking works 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? In addition, when including some header files (that will be required later), I get a lot of errors. For example, including "tree.h" yields (amongst 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

Anyone have a clue on what I am doing wrong?

Thank you

artless noise
  • 21,212
  • 6
  • 68
  • 105
llubder
  • 71
  • 1
  • 2
  • Did you consider using http://gcc-melt.org/ that is coding your GCC extension in MELT (a domain specific language to extend GCC) instead of painfully making a GCC plugin in C++ ? – Basile Starynkevitch Apr 11 '13 at 12:09
  • What is the output of `gcc -v` ? – Basile Starynkevitch Apr 11 '13 at 12:11
  • The output of -v (when compiling then plugin) is: collect2 --eh-frame-hdr -m elf_x86_64 -shared -o plugin.so /lib/../lib64/crti.o /gcc-48/lib/gcc/x86_64-unknown-linux-gnu/4.8.0/crtbeginS.o -L/gcc-48/lib/gcc/x86_64-unknown-linux-gnu/4.8.0 -L/gcc-48/lib/gcc/x86_64-unknown-linux-gnu/4.8.0/../../../../lib64 -L/lib/../lib64 -L/usr/lib/../lib64 -L/gcc-48/lib/gcc/x86_64-unknown-linux-gnu/4.8.0/../../.. plugin.o -lgcc --as-needed -lgcc_s --no-as-needed -lc -lgcc --as-needed -lgcc_s --no-as-needed /gcc-48/lib/gcc/x86_64-unknown-linux-gnu/4.8.0/crtendS.o /lib/../lib64/crtn.o – llubder Apr 11 '13 at 12:16
  • I removed some paths since message was too long. I prefer writing my things in C/C++ rather than Melt. Partially because I dont know too much about Melt but also since some of the stuff I am planning to do will be parsed from within the plugin itself. – llubder Apr 11 '13 at 12:18
  • No, run just the command `gcc -v` without any other argument. – Basile Starynkevitch Apr 11 '13 at 12:20

2 Answers2

3

There are two problems here :

The error : "cannot load plugin plugin.so" means that you should add to your LD_LIBRARY_PATH the directory where you store your new shared library plugin.

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

Greg
  • 31
  • 2
2

Which version of GCC are you using, both to compile your plugin, and to use the plugin? Run simply

 gcc -v

without any other program argument to find out!

Did you install the appropriate package for GCC plugin development (on Debian or Ubuntu, it might be gcc-4.7-plugin-dev, but adapt the 4.7 version to your particular version of GCC)?

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

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

You may check how was your GCC built (in C or in C++) by running

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

If that command shows typed C++ manged names, e.g. execute_ipa_pass_list(opt_pass*) instead of just execute_ipa_pass_list your GCC has been compiled with a C++ compiler (probably g++)

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

As I commented, did you consider using MELT (a domain specific language to extend GCC) to extend or customize your gcc compiler?

I suggest downloading the very 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 to change the parsing behavior of your GCC with a plugin. That is not really possible (GCC provides only plugin hooks to add your builtins and pragmas, not to extend the parsed syntax).

Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547
  • COLLECT_GCC=gcc COLLECT_LTO_WRAPPER=/home/rb/Tools/gcc-48/libexec/gcc/x86_64-unknown-linux-gnu/4.8.0/lto-wrapper Target: x86_64-unknown-linux-gnu Configured with: ../gcc-4.8.0/configure --prefix=/home/rb/Tools/gcc-48 --disable-multilib Thread model: posix gcc version 4.8.0 (GCC) – llubder Apr 11 '13 at 12:26
  • Thank you Basile. I did install a plugin-dev package (using FC17) and now the plugin is loaded and outputting the correct message. However, I did revert to my compiler that was pre-install on the system. Do I need to take extra precautions to use GCC4.8? Perhaps Im missing some flags. Nonetheless, it seems to work atleast with prebuild 4.7. Again, Thank you for the support. – llubder Apr 11 '13 at 12:27
  • Out of curiosity, what kind of GCC plugin are you willing to develop? – Basile Starynkevitch Apr 11 '13 at 12:30
  • A plug-in to support parallelization from within the application (think OpenMP, StarPU, OmpSs, OpenStream, XKaapi, ...) that interacts with my run-time system. It is kinda painful to manually transcode applications to use the features I am experimenting with. – llubder Apr 11 '13 at 12:43
  • I would be delighted by much more details, at least by email to me (then mention the URL of your question). Thanks. – Basile Starynkevitch Apr 11 '13 at 12:49