【问题标题】:GCC or make flag to forbid particular standard library functionsGCC 或 make 标志来禁止特定的标准库函数
【发布时间】:2015-11-24 18:05:18
【问题描述】:

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

我很想通过在项目文件(Qt Creator / qmake)中定义一些东西(例如重新定义符号)找到一种方法来禁止使用此功能(可能还有其他功能),以防止新开发人员或经验丰富的错误制造者再介绍一遍。

有什么想法吗?

【问题讨论】:

  • 嗯,有gcc poison
  • 既然要替换它,何不搜索所有文件以了解它的使用情况?
  • @Olaf 搜索和修复当前使用在后来有人添加调用 strtok 的代码时无济于事。 OP 可以创建自己的 strtok,如果调用它会导致中止 - 然后在单元测试期间会发现新添加的 strtok 调用。
  • @FredK:同意。 uint-test应该自动给违反编码标准的人发fire-mail....但是谁敢开除boss呢?
  • 您可以在构建可执行文件后扫描strtok 符号,使用nm 等目标文件分析工具。

标签: c gcc qmake


【解决方案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. 

【讨论】:

  • 使用这个 pragma 的最大问题是强制它包含在每个源文件中,在 #include 语句之后。
  • 这就是为什么我没有发表评论和回答的原因,因为它并不理想,而且 OP 也没有说这是一个可以接受的解决方案。虽然我认为这是 OP 能得到的最好的结果。
猜你喜欢
  • 1970-01-01
  • 2017-03-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-06-19
  • 1970-01-01
  • 1970-01-01
  • 2011-03-24
相关资源
最近更新 更多