gpt4 book ai didi

c - GCC 或 make 标志以禁止特定的标准库函数

转载 作者:太空狗 更新时间:2023-10-29 15:07:55 24 4
gpt4 key购买 nike

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

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

有什么想法吗?

最佳答案

这就是 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.

关于c - GCC 或 make 标志以禁止特定的标准库函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33900762/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com