- mongodb - 在 MongoDB mapreduce 中,如何展平值对象?
- javascript - 对象传播与 Object.assign
- html - 输入类型 ="submit"Vs 按钮标签它们可以互换吗?
- sql - 使用 MongoDB 而不是 MS SQL Server 的优缺点
我们最近为一个项目启用了 -Wall
。它在 GCC 为 4.7 或更高版本(或 Clang)时启用,因为我们可以使用 GCC diagnostic
管理提升警告的输出。我们希望通过源代码管理它们,而不是通过命令行参数。 (我们不想污染命令行,或者要求图书馆用户重新发现需要什么)。
在 GCC 4.8 和 5.1 下,我们正在捕获在 -Wunused-variable
、-Wunused-value
、-的 GCC 诊断 block 中禁用的警告- Wunused-function
和 -Wunknown-pragmas
。两个 GCC 都接受 -fopenmp
,并且都定义了 _OPENMP
来响应它,所以我很确定我们永远不会看到 -Wunknown-pragmas
code> 以响应 #prgam omp ...
(它被禁用,但它不是未知)。
g++ -DNDEBUG -g2 -O3 -Wall -march=native -pipe -c nbtheory.cpp
nbtheory.cpp:655:0: warning: ignoring #pragma omp parallel [-Wunknown-pragmas]
#pragma omp parallel
^
nbtheory.cpp:656:0: warning: ignoring #pragma omp sections [-Wunknown-pragmas]
#pragma omp sections
^
...
在这种特殊情况下,file nbtheroy.cpp
有以下防护措施来帮助管理该警告(仅显示相关部分,但您可以看到 the GitHub link 中的所有内容):
// Defines GCC_DIAGNOSTIC_AWARE if GCC 4.7 or above.
#include <misc.h>
...
#if GCC_DIAGNOSTIC_AWARE
# pragma GCC diagnostic ignored "-Wunknown-pragmas"
#endif
...
Integer ModularRoot(const Integer &a, const Integer &dp, const Integer &dq,
const Integer &p, const Integer &q, const Integer &u)
{
Integer p2, q2;
#pragma omp parallel
#pragma omp sections
{
#pragma omp section
p2 = ModularExponentiation((a % p), dp, p);
#pragma omp section
q2 = ModularExponentiation((a % q), dq, q);
}
return CRT(p2, p, q2, q, u);
}
...
因为文件是*.cpp
(它实际上是翻译单元),我们不执行#pragma GCC diagnostic push
开头,#pragma GCC diagnostic pop
结尾。 (但是,我们对包含的头文件执行此操作)。 (我们也尝试过这样做,但没有帮助)。
这里是 GCC_DIAGNOSTIC_AWARE
(来自 misc.h
):
// Used to suppress some warnings in some header and implementation files.
// Some platforms, like CentOS and OpenBSD, use old compilers that don't understand -Wno-unknown-pragma.
#define GCC_DIAGNOSTIC_AWARE ((__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 7)) || defined(__clang__))
我知道守卫正在工作,因为在 block 中添加 #error
会导致错误。此外,注释掉 guard 并调用 #pragma GCC diagnostic ignored "-Wunknown-pragmas"
也无济于事。最后,它在 Clang 下运行良好。
我也遇到过其他警告,例如 -Wunused-variable
、-Wunused-value
和 -Wunused-function
。我真的不想像建议的那样用潜在的重复来污染命令行。
当使用 -Wall
时,如何让 GCC pragma diagnostic
机制按预期工作以消除 GCC 下的警告?
相关,如果你想复制它(它基于 GNUmakefile,不需要配置或自动工具):
git clone https://github.com/weidai11/cryptopp.git cryptopp-warn
cd cryptopp-warn
make
EDIT:我们 checkin 了一个补丁,该补丁禁用了除 Clang 之外的 -Wall
。如果你想重现旧的行为,那么:
git clone https://github.com/weidai11/cryptopp.git cryptopp-warn
cd cryptopp-warn
export CXXFLAGS="-g2 -O3 -DNDEBUG -Wall"
make
最佳答案
这似乎是 gcc
中的一个错误。至少。以下代码:
#pragma GCC diagnostic ignored "-Wunknown-pragmas"
#pragma GCC diagnostic ignored "-Wuninitialized"
int fn(void) {
#pragma xyzzy
int x;
return x;
}
int main (void) {
return fn();
}
忽略未初始化的 x
没有问题值,但仍然提示编译指示(没有 uninitialized
编译指示,它会如您所料生成 x
的警告)。
如果您将命令行选项更改为 -Wall -Wno-unknown-pragmas
,然后它忽略它就好了。这对于您的特定情况没问题,因为您希望它应用于整个翻译单元,但它不允许您从 #pragma
获得的细粒度控制。方法(如果有效)。
我去提出关于 GCC 的错误报告,但发现它已经存在 (#53431)。
虽然该特定错误与 -Wundef
有关,其中一个评论中的片段表明它可能适用于影响预处理器的所有变体(为了强调而稍作修改):
The C++ parser lexes (and preprocesses) before handling the pragmas, whereas the C parser processes the pragmas as it sees them.
We must somehow parse these pragmas also in
cp/parser.c:631
. Maybe one can do something similar to what we do forcp_parser_initial_pragma
, but within the loop and only handling pragma diagnostic. Surely, it will need some trial and error to get it right. If any of you wants to give it a try and need some help, just ask here or in the mailing list.
这就解释了为什么我们在 -Wuninitialized
上看不到同样的问题。 , 因为它是在编译过程的后期阶段检测到的,在预处理结束时激活 pragma 之后。
因此,如果您希望看到它以更及时的方式得到修复(它是在三年前提出的),我建议(就像我一样)麻烦 GCC bugzilla 网站以尝试获得一些曝光。
关于c++ - GCC 不遵守 'pragma GCC diagnostic' 以消除警告,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31509434/
我正在尝试在 Conda 环境中编译一些代码,在那里我 之前安装的编译包gcc_linux-64 . 然而,即使在停用和重新激活环境之后,gcc还在/usr/bin/gcc . 我该怎么做才能让 Co
关闭。这个问题是opinion-based .它目前不接受答案。 想要改进这个问题? 更新问题,以便 editing this post 可以用事实和引用来回答它. 关闭 7 年前。 Improve
这其实是两个问题: 1 - 在我的 debian amd64 系统上,我似乎无法构建与 gmp/mpfr/mpc 动态链接的交叉 GCC。即使我删除 --disable-shared,它也总是静态链接
研究ELF格式的结果,可以看到目标文件中有一个符号对应每个函数,对应的符号表项的值为st_size,表示大小的功能。 问题是,即使我更改了目标文件中特定函数的 st_size 并链接了它,但可执行文件
海湾合作委员会的 documentation for #line directives说他们是这样的: #line "myfile.cpp" 123 但是当我用 g++ 5.1 检查输出时,它们实际上
我正在使用 as 和 gcc 来汇编和创建 ARM 汇编程序的可执行文件,正如 this 所推荐的那样教程,如下: 给定一个汇编源文件,program.s,我运行: as -o program.o p
long long x; double n; x=long long(n); 这不起作用。什么是正确的方法? 最佳答案 显而易见的: x = (long long) n; 关于gcc - 转换为长长
我想知道用于 gcc 的原子内置函数的头文件是什么? 我想使用这 2 个函数为我当前创建的线程库实现互斥锁。 bool __sync_bool_compare_and_swap (type *ptr,
它出现在 another question :gcc调用的程序和部件是什么? (特别是在编译 C 或 C++ 时)以便有人可以设计一些拦截和更改流程的方案以用于各种自定义编码目的? 最佳答案 编译器二
可能吗?我想使用 gcc喜欢 assembler并在将其编译为 ubuntu 上的可执行文件后。 我尝试过这个: gcc a.asm -o out.o 来自 out.o文件编译成.out可执行文件。
我写了一个简单的 C 程序 test.c : #include #include int add(int a, int b); int main() { int i=5,j=10;
即。所以如果你使用任何八进制文字,它会给你一个警告。 微软编译器的同样问题。 如果没有,是否有任何其他工具可以检测八进制文字。 (vim 似乎有一个很酷的技巧,它突出了第一个领先的将不同的颜色归零,但
我在旧线程中搜索。但没有找到任何线程回答我的问题。 gcc 是否像 vc++ 一样支持函数级链接? 如果是,我应该提供什么选项来链接目标文件和库? 最佳答案 看起来 gcc 不直接支持函数级链接。您可
也许标题并没有把问题说得那么准确:我知道当我运行 gcc foo.c 时,GCC 会调用其他为它完成所有工作的子程序,从而生成主 gcc 程序只是一个界面。但这究竟是如何完成的呢? 它是否使用syst
我听说最近版本的 gcc 非常擅长将通过函数指针的调用转换为直接调用。但是,我在网上或快速浏览 gcc 的源代码上找不到任何关于它的信息。有谁知道这是否真的是真的,如果是这样,它使用什么算法来做到这一
gcc/g++ 链接器选项“-Map”生成的“.map”文件用于什么? 以及如何阅读它们? 最佳答案 我建议为您投入生产的任何软件生成一个映射文件并保留一份副本。 它可用于破译崩溃报告。根据系统的不同
gcc信息文件在有关x86-64特定标志的部分中说 其他事情: There is no `-march=generic' option because `-march' ind
我想知道 gcc 链接器选项(例如:-Wl,options)是否可以更改编译后的可执行文件中的汇编指令,因为如果您使用某些 gcc 优化选项会发生这种情况? 当您比较编译后的二进制文件(例如比较签名)
是否有GCC编译指示会停止,暂停或中止编译过程? 我正在使用gcc 4.1,但也希望在gcc 3.x版本上也可以使用该编译指示。 最佳答案 您可能需要#error: edd@ron:/tmp$ g++
当我使用gcc编译C程序时我通常使用 -g 将一些调试信息放入 elf 文件中这样 gdb 就可以在需要时帮助我。 但是,我注意到有些程序使用 -ggdb,因为它应该使调试信息对 gdb 更加友好。
我是一名优秀的程序员,十分优秀!