GCC 11开始明确要求包含
<limits>
、
<memory>
、
<utility>
和
<thread>
,请参考
https://www.gnu.org/software/gcc/gcc-11/porting_to.html#header-dep-changes。
Clang 12(或更早版本)也有类似的情况,我不确定。
由于我经常在使用Node.js的软件包管理器
yarn
时遇到这个错误,它会覆盖所有源文件,因此我无法轻松添加
#include <limits>
:我需要分叉或者在编译过程中不断地运行
cp /tmp/fixedBad.h /installdir/bad.h
。
因此,我的解决方案是将以下代码添加到CXXFLAGS(而不是CFLAGS)中:
-include /usr/include/c++/11/limits
(Ubuntu 21.04,gcc 11.1.0)
-include /usr/include/c++/11.1.0/limits
(Arch Linux;版本相同,但路径与Debian / Ubuntu不同)
或优雅地:
-include /usr/include/c++/11*/limits
请注意,只有在shell(bash、sh、zsh等)或makefile中使用
*
才有效。换句话说,gcc和clang不会注意文件路径中的
*
,因此如果您使用
ninja build
或直接从C程序传递参数给gcc / clang,请注意。
我将这个设置添加到了
/etc/environment
中。
CPPFLAGS="-D_FORTIFY_SOURCE=2"
CFLAGS="-g -pipe -march=native -mtune=native -Ofast -pipe -ftree-vectorize -fstack-protector-strong"
CXXFLAGS="-g -pipe -march=native -mtune=native -Ofast -pipe -ftree-vectorize -fstack-protector-strong -include /usr/include/c++/11*/limits"
LDFLAGS="-Wl,-O1,--sort-common,--as-needed,-z,relro"
RUSTFLAGS="-C target-cpu=native -C opt-level=2"
请检查echo $CXXFLAGS
,如果更改未生效,请重新启动shell或操作系统,或注销当前tty,或切换到另一个tty,或在终端中运行:
export CXXFLAGS="-g -pipe -march=native -mtune=native -Ofast -pipe -ftree-vectorize -fstack-protector-strong -include /usr/include/c++/11*/limits"
或者使用 set -a; source /etc/environment; set +a;
我还尝试将-include '<limits>'
和-include '<limits.h>'
添加到CXXFLAGS中,但它会显示“没有这个文件或目录”
此外,我还有另一个解决方案(非常粗糙):
在最后一行 (#endif
) 之前将以下内容添加到 /usr/include/stdint.h
(或 stdlib.h
或其他一些流行的文件) 中:
#ifdef __cplusplus
extern "C++" {
#include <limits>
}
#endif
Ubuntu 21.04和Debian Buster的dpkg-query -S /usr/include/stdlib.h
显示它是由libc6-dev:amd64
拥有。而Arch Linux的pacman -Qo /usr/include/stdlib.h
显示它是由glibc
拥有。因此,在软件包更新时,这个hack将被覆盖,请不要忘记。
numeric_limits
的文档以了解如何使用它时,它没有告诉你它所在的头文件是什么吗? - Rob Kennedy#include <limits>
。 - Milovan Tomašević