gcc预编译头文件:pragma出现在主文件中 [英] gcc precompiled header: pragma once in main file

查看:325
本文介绍了gcc预编译头文件:pragma出现在主文件中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了头文件.简单一些如下.

I created a header file. Something simple as follows.

#pragma once

#include <iostream>

template<typename T>
void say(T t) {
    std::cout << t << std::endl;
}

,然后使用g++g++ hello.h创建gch预编译头.它给了我这个警告->

and then use g++ to create the gch pre-compiled header with g++ hello.h. It gives me this warning ->

pch.h:2:9: warning: #pragma once in main file
    2 | #pragma once
      |         ^~~~

但是创建的gch文件和预编译的头文件可以正常工作.如果使用标题保护,则该错误消失.

But the gch file created and the pre-compiled header works fine. This error goes away if I use header guards.

我在这里做错什么了吗?

Am I doing something wrong here?

推荐答案

您没有做错任何事情;这是一种实施质量问题,之前在问题跟踪器中已经提到 (但是据我所知,目前尚无改变行为的计划).

You're not doing anything wrong; this is a quality of implementation issue that has been mentioned on the issue tracker before (but, to my knowledge, there are currently no plans to change the behaviour).

在Clang中,您可以针对特定的编译器调用(使用-Wno-pragma-once-outside-header)关闭警告;在GCC中,您现在只需要咧着嘴笑着忍着.

In Clang, you could turn off the warning for that particular compiler invocation (with -Wno-pragma-once-outside-header); in GCC you'll just have to grin and bear it for now.

这篇关于gcc预编译头文件:pragma出现在主文件中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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