Issue
I have a C static library with global variables. My goal is to print a message at compile time to the user whenever global variables from the library are used in its program.
I tried to mark variables as __attribute__((deprecated))
. But I need the user to be able to build even if -Werror
is set.
Therefore I tried to add #pragma GCC diagnostic warning "-Wdeprecated-declarations"
, but it only seems active within the library, not if a user link with the library.
Solution
You could employ linker instead as explained in e.g. ninjalj's blog.
Here's a short example:
$ cat myvar.c
int myvar = 0;
static const char myvar_warning[] __attribute__((section(".gnu.warning.myvar"))) =
"myvar is deprecated";
$ cat main.c
extern int myvar;
int main() {
return myvar;
}
$ gcc main.c myvar.c
/tmp/cc2uM5Vx.o: In function `main':
tmp.c:(.text+0x6): warning: myvar is deprecated
Answered By - yugr Answer Checked By - David Goodson (WPSolving Volunteer)