在 c++ 中找不到头文件,可能的路径问题

Header files not found in c++, possible path issue

本文关键字:路径 问题 文件 c++ 找不到      更新时间:2023-10-16

我有一个这样的文件结构:

main.cpp         --> #include <headers/f1/v.h>
headers/f1/v.h   --> #include <headers/f1/i.h>
headers/f1/i.h

headers是外部库的目录。使用"g++ main.cpp"编译并得到文件未找到错误:

In file included from main.cpp:11:
./headers/f1/v.h:32:10: fatal error: 'headers/f1/i.h' file not found
#include <headers/f1/i.h>

对 c++ 非常陌生。真的想不通。这里出了什么问题?谢谢!

在同一构建树中包含自己的标头时,应使用引号而不是尖括号:

#include "headers/f1/v.h"

如果您确实遇到需要<>本地文件的情况,无论出于何种原因,都可以将目录添加到编译器的包含路径中:

g++ main.cpp -I .

其中.是"此目录"的 POSIX 约定。


延伸阅读:

  • #include <文件名>和 #include"文件名"有什么区别?