0

Let's say, in my C executable project, I use a C library to which I have source access - in fact, the .c files of the library get built as part of the build process for my C executable.

Then, let's say in some of the library .c files, let's say exotic.c, there is something like:

static struct exotic_type all_exotic_array[NUM_EXOTIC_ITEMS][8];

... which the author of the library believes should remain hidden (ergo static) - but which, it turns out, I need a reference to in my code; therefore I'd like to make this variable's symbol publicly accessible.

So, in principle I could just erase the static keyword there and recompile - and I'd get the all_exotic_array as global. However, let's say I also do not want to change the source code of the library.

So in this context, I can intervene in the Makefile, when the individual library .c files get compiled. So:

  • Is there a compilation switch, with which I could tell the compiler gcc (or linker ld?), while compiling exotic.c, something like: "please ignore the static storage class specifier of the symbol all_exotic_array, and make it available globally"?
  • If not - is there any other action I could take (maybe after compilation of exotic.c but before linking my executable), to make the symbol all_exotic_array globally available?

I have seen

... which hint that there may be a possibility to do what I want, however I couldn't find exactly what to do in my use case.

sdbbs
  • 4,270
  • 5
  • 32
  • 87
  • 1
    According to [this Q&A](https://stackoverflow.com/questions/37484085/can-gcc-ignore-static-declaration-of-a-function), there is no way to make `gcc` ignore `static`. – Ivan Venkov Jan 25 '23 at 13:39
  • 3
    During the build, copy the original library source file to a temporary file. Alter the temporary file. Compile the temporary file. – Eric Postpischil Jan 25 '23 at 14:04
  • 1
    sdbbs, "but which, it turns out, I need a reference to in my code;" --> Hmmm, one step closer to the precipice. Consider posting the real problem rather than a "how do I do x to solve (unstated) y" question. – chux - Reinstate Monica Jan 25 '23 at 14:49

0 Answers0