GCC或制作标志以禁止特定的标准库函数

时间:2015-11-24 18:05:19

标签: c gcc qmake

我正在研究一个多线程项目,由于使用了不是线程安全的库函数“strtok()”而遭受了许多错误。

我喜欢通过在项目文件(Qt Creator / qmake)中定义某些东西(例如重新定义符号)来找到禁止使用此函数(以及可能还有其他函数)的方法,以保留新的开发人员或经验丰富的bug再次介绍它。

有什么想法吗?

1 个答案:

答案 0 :(得分:1)

这是GCC手册中关于取消使用某些库函数的说法:

#pragma GCC poison
Sometimes, there is an identifier that you want to remove completely
from your program, and make sure that it never creeps back in. 
To enforce this, you can poison the identifier with this pragma. 
#pragma GCC poison is followed by a list of identifiers to poison. 
If any of those identifiers appears anywhere in the source after the directive, 
it is a hard error. For example,

          #pragma GCC poison printf sprintf fprintf
          sprintf(some_string, "hello");


will produce an error.

If a poisoned identifier appears 
as part of the expansion of a macro which was defined 
before the identifier was poisoned, it will not cause an error. 
This lets you poison an identifier 
without worrying about system headers defining macros that use it.

For example,

          #define strrchr rindex
          #pragma GCC poison rindex
          strrchr(some_string, 'h');


will not produce an error.