2

I'm having problems figuring out how to do the next thing.

I have the following code:

test.cpp

#include <stdio.h>

void
function(void) {printf("Hellow ");}

int main(void) {
    printf("World\n");
    return 0;
}

And I want to transform it into the next one:

#include <stdio.h>

void
function(void) {printf("Hellow ");}

int main(void) {
    function();
    printf("World\n");
    return 0;
}

with a gcc plugin.

The code that doesn't work in my plugin is this one:

...
tree function_fn;
tree function_fn_type;

function_fn_type=build_function_type_list(void_type_node, void_type_node, NULL_TREE);
function_fn = build_fn_decl ("function", function_fn_type);

gimple call = gimple_build_call (funcion_fn, 0);
gsi_insert_before (&gsi, call, GSI_NEW_STMT);
...

Then when I compile test.cpp with the plugin i have the next error message:

/tmp/cc2VRszt.o: In function main': test.cpp:(.text+0x60): Undefined reference tofunction'

Anyone can help me?

artless noise
  • 21,212
  • 6
  • 68
  • 105
Andres Tiraboschi
  • 543
  • 1
  • 7
  • 17

1 Answers1

1

You're building a function declaration, and inserting a call to a function based on the declaration, but unless you've defined that function in another translation unit you link to, it will be unresolved. If you want a plugin to insert a definition in the same translation unit like in your example, this guide for front-end developers would be a good start:

http://www.tldp.org/HOWTO/GCC-Frontend-HOWTO-7.html

  • This does not help, at all. – lllllllllllll Jun 18 '20 at 05:06
  • @lllllllllllll You've provided zero context, so it's hard to say why it doesn't help you, or even if your question is the same. Since it was accepted as an answer, it seems to have helped the OP. You should ask a new question or state your current problem. Anything else is less than helpful/useful. That said, my recollection of this was that there wasn't a function definition in the same translation unit in the original question, which would match their error. –  Jul 06 '20 at 22:01