user3704133
user3704133

Reputation:

How can I use multiple functions with the same name in other C files if they have the same name and I can't modify the other files?

I have program which generates C files containing a single function void foo(float* n) {}. The files have different code within the function but the name is always the same. I want to create a C file which runs them all in turn but when I try to include more than one I get a redefinition error (from the linker?). I can't change the C files that are produced (otherwise I would just change the function name). The files generated all have different and unique names even though the functions all have common names, and I only need to use the functions one at a time (if there is any sort of freeing operation like #undef for macros).

Upvotes: 4

Views: 868

Answers (1)

Paul R
Paul R

Reputation: 212979

You can use the preprocessor to change the function name for each file at compile-time, e.g.

$ gcc -Wall -Dfoo=foo_1 -c bar1.c
$ gcc -Wall -Dfoo=foo_2 -c bar2.c
$ gcc -Wall -Dfoo=foo_3 -c bar3.c
$ gcc -Wall main.c bar1.o bar2.o bar3.o

This way you can call functions foo_1(), foo_2(), foo_3() from main().

Upvotes: 6

Related Questions