Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clarify include style when headers are in the same folder as the including file #5027

Open
fruffy opened this issue Nov 22, 2024 · 2 comments
Labels
question This is a topic requesting clarification.

Comments

@fruffy
Copy link
Collaborator

fruffy commented Nov 22, 2024

This came up in #5006 (comment)_

If we have the following two files:

dir
|- file.cpp
|- file.h

How should file.cpp include file.h
Using the project-absolute path: #include "dir/file.h" or just #include "file.h"?

We currently have a mix of these in the compiler. I am partial to the project-absolute path but not opposed to the relative style. If we go with one option we should add a linter or tool to fix these paths and make them consistent.

@fruffy fruffy added the question This is a topic requesting clarification. label Nov 22, 2024
@asl
Copy link
Contributor

asl commented Nov 22, 2024

I would say the rule might be the following:

  • If the header is expected to be the part of API, then it should be always used like #include "dir/file.h".
  • Otherwise, if the header is local, then it should be included like #include "file.h"

Though, it is pretty hard to automatically determine if this is so. Then, maybe the rule should be as follows:

  • The primary header for the file should be included like #include "file.h" only from within file.cpp
  • Otherwise – always use #include "dir/file.h".

@fruffy
Copy link
Collaborator Author

fruffy commented Nov 22, 2024

I would say the rule might be the following:

* If the header is expected to be the part of API, then it should be always used like `#include "dir/file.h"`.

* Otherwise, if the header is local, then it should be included like #include "file.h"

Though, it is pretty hard to automatically determine if this is so. Then, maybe the rule should be as follows:

* The primary header for the file should be included like `#include "file.h"` only from within `file.cpp`

* Otherwise – always use `#include "dir/file.h"`.

Yeah, we do not really have a notion of API, which might be the reason why things are as they are.

At least for the second example, writing a tool could be easily doable. Maybe I can even generate this with ChatGPT :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question This is a topic requesting clarification.
Projects
None yet
Development

No branches or pull requests

2 participants