c++ - gcc 预编译头文件 : pragma once in main file

标签 c++ g++ pragma precompiled-headers

我创建了一个头文件。简单的事情如下。

#pragma once

#include <iostream>

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

然后使用g++ 创建带有g++ hello.hgch 预编译头文件。它给了我这个警告 ->

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

但是创建的 gch 文件和预编译的头文件工作正常。如果我使用 header guards,这个错误就会消失。

我是不是做错了什么?

最佳答案

你没有做错任何事;这是一个实现质量问题 has been mentioned on the issue tracker before (但是,据我所知,目前没有改变这种行为的计划)。

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

关于c++ - gcc 预编译头文件 : pragma once in main file,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56563679/

相关文章:

java - 将 Java 中的 C++ DLL 与 JNA 结合使用

c++ - 编译器忽略 operator new 分配

c++ - std::set 没有成员 emplace

c++ - 在 Mac 上通过命令行编译 Allegro 5 程序

在编译期间将文本写入文件的 C pragma

c++ - 我可以在不调用未定义行为的情况下存储指向不存在数据的指针吗?

c++ - 使用 Bison/Flex 时的 C 字符串

c++ - 根据优化级别调试 hello world 崩溃的构建

c++ - 是否可以禁用编译器警告 C4503?

c++ - 我可以更改 C++ 中的静态变量初始化顺序吗?