我开发了一个跨平台的库,它在套接字通信中合理使用了type-punning。这个库已经在许多项目中使用,其中一些我可能不知道。
不正确地使用这个库可能导致危险的未定义行为。我希望尽我所能确保这个库被正确使用。
除了文档之外,在G++下,我所知道的最好的方法是使用-fstrict_aliasing
和-Wstrict-aliasing
选项。
在GCC下,有没有一种方法可以在源文件级别应用这些选项呢?
换句话说,我想写出类似以下的内容:
MyFancyLib.h
#ifndef MY_FANCY_LIB_H
#define MY_FANCY_LIB_H
#pragma (something that pushes the current compiler options)
#pragma (something to set -fstrict_aliasing and -Wstrict-aliasing)
// ... my stuff ...
#pragma (something to pop the compiler options)
#endif
有没有办法?