'GCC message on deprecated global variable usage from a library
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 1:[1]
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
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
