GCC杂注在源文件中添加/删除编译器选项 [英] GCC pragma to add/remove compiler options in a source file

查看:165
本文介绍了GCC杂注在源文件中添加/删除编译器选项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开发了一个跨平台的库,在套接字通信中公平地使用类型双关键字。这个库已经在许多项目中使用,其中一些项目我可能不知道。

I have developed a cross-platform library which makes fair use of type-punning in socket communications. This library is already being used in a number of projects, some of which I may not be aware of.

错误地使用这个库会导致危险的未定义行为。我想尽我所能确保这个库的使用正确。

Using this library incorrectly can result in dangerously Undefined Behavior. I would like to ensure to the best of my ability that this library is being used properly.

除了当然的文档,在G ++下我知道的最好方法是要做到这一点,就是使用 -fstrict_aliasing -Wstrict-aliasing 选项。

Aside from documentation of course, under G++ the best way I'm aware of to do that is to use the -fstrict_aliasing and -Wstrict-aliasing options.

在GCC下有一种方法可以在源文件级应用这些选项吗?

Is there a way under GCC to apply these options at a source file level?

换句话说,我想写一些类似如下:

MyFancyLib.h



In other words, I'd like to write something like the following:

#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

有没有办法?

Is there a way?

推荐答案

是一个只有头文件的库,我认为是处理这个问题的唯一方法是解决严格的走样违规问题。如果违规发生在您定义的类型之间,则可以使用涉及联合的常用技巧或 may_alias type属性。如果您的图书馆使用预定义的 sockaddr 类型,这可能很困难。

If your library is a header-only library, I think the only way to deal with this is to fix the strict aliasing violations. If the violations occur between types you define, you can use the usual tricks involving unions, or the may_alias type attribute. If your library uses the predefined sockaddr types, this could be difficult.

这篇关于GCC杂注在源文件中添加/删除编译器选项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆