You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

设置include标志后g++仍报‘no such file or directory’错误求助

问题原因分析

结合你的描述,可能的原因有以下几种:

1. 代码中的include语句路径错误

这是最常见的问题。如果你的代码里写的是:

#include <json.hpp>

而不是正确的:

#include <nlohmann/json.hpp>

g++会直接在/usr/include下查找json.hpp,不会自动进入nlohmann子目录,自然会报找不到文件的错误。

2. 目录或文件的权限问题

Linux下普通用户编译时需要具备读取/usr/include/nlohmann目录和json.hpp文件的权限。如果移动文件时未正确设置权限,比如:

  • nlohmann目录权限为drwx------(仅所有者可读)
  • json.hpp文件权限为-rw-------(仅所有者可读)
    普通用户运行g++时就无法读取这些文件,导致报错。可以用以下命令检查权限:
ls -ld /usr/include/nlohmann
ls -l /usr/include/nlohmann/json.hpp

正常情况下,目录权限应为drwxr-xr-x,文件权限应为-rw-r--r--。

3. 大小写匹配问题

Linux文件系统大小写敏感,如果代码里写的是#include <Nlohmann/json.hpp>或#include <nlohmann/JSON.hpp>,而实际路径是nlohmann/json.hpp,也会导致找不到文件。

4. 文件损坏或未完整移动

如果移动过程中json.hpp被截断、损坏,或者未完整复制到目标目录,g++读取时也会报错(有时错误信息可能表现为找不到文件,或后续出现编译错误)。可以用cat /usr/include/nlohmann/json.hpp检查文件内容是否完整。

内容的提问来源于stack exchange,提问作者alvrm

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.31 20:20:17