[英]How to compile C code on Linux/GCC without deprecated functions being available?
上有老功能,如index
, rindex
它現在已經被取代strchr
和strrchr
。
有沒有辦法配置編譯器或定義所以這些功能不可用?
在以下情況下,它可能會導致混淆警告
index
名稱,例如 - 或者更糟,沒有警告並以非預期的方式使用該功能。 index
的變量,那么當使用-Wshadow
時,較舊的GCC版本(4.x)會發出警告。 看到:
筆記:
index
不應該重新定義,因為庫可能會使用它。 index
的常見情況。 deprecated
屬性標記這些函數,因此與使用棄用函數相關的警告不起作用。 在ISO C模式下使用編譯器。 C標准禁止由於存在非保留字的標識符而使符合程序被破壞。
例如,使用flags -std=c99
。
示例程序:
#include <string.h>
int main()
{
index("abc", 'x');
}
用-std=c11 -Werror
編譯給出:
error: implicit declaration of function 'index' [-Werror=implicit-function-declaration]
您不應該重新定義這些標識符,因為您鏈接的某些庫仍然可能依賴於它們。
如果我理解得很好,你想使用index
作為變量名而不與<strings.h>
包含的index
函數沖突。
在這種情況下,您可以使用預處理器覆蓋:
#include <stdio.h>
#define index(s, c) deprecated_index(s, c)
#include <strings.h>
int index = 5;
int main(void)
{
printf("%d\n", index);
return 0;
}
或者用strchr
覆蓋index
:
#include <stdio.h>
#include <string.h>
#include <strings.h>
#define index(s, c) strchr(s, c)
int main(void)
{
int index = 5;
char *ptr = index("text", 'x');
printf("%s %d\n", ptr, index);
return 0;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.