Clang-tidy check like LLVMHeaderGuardCheck but for configurable include paths? - c++

Anyone know of any sources for a LLVMHeaderGuardCheck like clang-tidy check that formats the expected include guards based on configurable information like the include path(s) given on the command line?
More details...
The LLVMHeaderGuardCheck header guard check expects header file include guard macro names that are all uppercase letters of the path after include/ with the / replaced by _ and a suffix like .h or .hpp replaced with _H. That works as a fine header guard check so long as your header files are all under a common root that has include/ in its path.
What I'm looking instead for is a check which works based on the path given in a -I$path command line argument. So if given an include file path like -I/usr/local/xyz an include file named /usr/local/xyz/common/foo.h needs to have include guard of XYZ_COMMON_FOO_H.
I wrote some code to work for the meantime but my commit only uses the path as an option from the .clang-tidy configuration file. While this suffices, it's not as flexible as a solution that can also use the command line info.
So I'm wondering what else is out there as open source checks for clang-tidy for header file include guard naming checks as I haven't had any luck finding anything via the web searching I've done to-date.

Related

How to set includes to be relative to the relativity of the source file from the root path?

Sorry if my phrasing of the question is a bit confusing:
How do I set includes to be relative to the relativity of the source file from the root path?
I am trying to compile a C++ project (not my own), and there are two folders: src and include.
The .cpp source files are located in src and the header files are located in include.
I would like to compile all the source files.
However, the #includes in the source files are relative, as if the include folder tree and the src folder tree were merged.
For example src/foo/bar/baz.cpp might have #include "baz.h". In this scenario, the header file is supposed to be searched for in include/foo/bar/baz.h rather than include/baz.h.
How do I achieve this with GCC? I'm sure a similar question exists on the internet already, I just can't figure out how to phrase my question well.
Here is what I've tried so far which is incorrect:
gcc src/**/*.cpp -I include
Note: I still need to be able to append additional -I <folder> at the end of the command for including files the normal way.
There is no magic compiler option to do what you want.
Solution 1: Change the #include directives to be relative to the include directory.
Solution 2: Don't use separate directories and move the headers into src directory.

how can include <> ignore system default header file path(usr/include)?

I have a c++ project where the build process is organized by cmakelist, the compile stuck at a point and prompt that g++ cannot find json/json.h, I check my standard header path(/usr/include) which indeed has this hearder file. after struggle for a while I found This project use header file from its own thirdparty directory(not /usr/include) and the include_directory path was incorrectly set by me!! This upset me How can it be realized since #include <> should firstly search standard header file path(/usr/include) !!??
This documentation
https://en.cppreference.com/w/c/preprocessor/include
Says about including with ""
"Typical implementations first search the directory where the current file resides and, only if the file is not found, search the standard include directories"
I'm not certain I understand your question very well but,
if json/json.h is present in your local directories,
may be should you include this header-file with ""?

How to include header files without quotation marks?

I'm kind of new to programming so please go easy on me. Anyways, I know about including header files that you, yourself, have defined. For example:
#include "yourHeader.h"
I'm trying to use FLTK for its GUI options, however, many of its header files include other header files using an include like this:
#include <FL/Blah.h>
instead of this:
#include "FL/Blah.h"
I would have to go every header file that has the include in angle brackets and change them to quotation marks for them to work. I am currently working in CodeBlocks right now, if that matters. Is there any way to include the header files using angle brackets instead of quotation marks, or am I stuck with having to go into the header files themselves and manually swapping them all out?
Generally, the header file from
#include "headerfile"
will searched in the current source path. If the search fails, it is reprocessed as if
#include <header file>
does.
Your FLTK library is using include like the following?
#include <FL/Blah.h>
The FL's parent path should be in the predefined INCLUDE path. You may edit you Makefile or project settings.
You can add the folder which contains all your headers to your include path while compiling.
How to add a default include path for GCC in Linux?
Some background
Ok there are two set of include search path.
The user include path:
This is usually only the current directory (also known as ".").
Note: It may be others but for simplicity lets just use "." in the examples below.
Then there is the system include path:
This is usually a few places in your machines (could be /usr/include and /usr/local/include).
Note: It may be others but for simplicity lets just assume these in the examples below.
How it usually works.
There are caveats and not all compilers work exactly the same. But the following are a good rules of thumb.
When you include a file using quotes "".
#include "yourHeader.h"
It will search for this file in all the directories specified in the "user include path". If it fails to find them there it will then look in all the directories specified by the "system include path". So your compiler will search for the following files:
./yourHeader.h
/usr/include/yourHeader.h
/usr/local/include/yourHeader.h
It will use the first one it finds.
When you use the <> in the include:
#include <FL/Blah.h>
It will search for the files in the "system include path" first. Then depending on your compiler may optionally search the "user include path" (but lets assume not for now).
So in this case it will search for the files:
/usr/include/FL/Blah.h
/usr/local/include/FL/Blah.h
It will use the first one it finds.
Modifying the Default
So these are the default paths that will be searched for a file. But your compiler will allow you to add extra paths to both of these search paths (usually). It depends on your compiler how to add search paths.
For gcc (and probably clang) it uses -I and -isystem (and probably more)
Expectations.
When you see <> in the include header it usually means this is an already installed library that you are looking for. So your code assumes that the FLTK library has already been installed on your machine.
When you see "" in the include header you should assume that it is a local file that belongs to the project.

Difference between <include.hpp> and "include.hpp" [duplicate]

This question already has answers here:
What is the difference between #include <filename> and #include "filename"?
(30 answers)
Closed 8 years ago.
I am new to C++.
What is the difference between including the c++ header files using "" and <>
I am trying to use some of the header files form an open source library. All header files in that library are included using <>. Now when I do the same in my header file, its failing at compile time.
<> looks firstly in the header path for the header file whereas "" looks firstly in the current directory of the file for the header.
The distinction is very largely implementation defined; the "..." form
should look first in the place where the file which includes it is
situated; the <...> no. Beyond that, both look in an implementation
defined list of places, with the additional requirement that if the
compiler doesn't find a "..." form in any of the expected places, it
reprocesses the include as if it were a <...> form.
In practice, all of the compilers I know build a list of places using
the -I or /I options, followed by a number of "standard" and
compiler defined places. This list is for <...>; "..." is searched
in the same directory as the including file, then treated as a <...>.
(Some compilers, at least, also have options to add to the list for
"...".)
I'm not sure what's happening with regards to the library. Normally,
when using a third party library, you have to add one or more -I or
/I options to tell the compiler where to find its headers. Once
you've done that, both your code and the library code should find all of
the necessary headers. The one case I can think of where an include
might work in a library header, and not in your own headers, is a
"..." style include in a library header which was included from
another library header, using a path specifier, e.g.:
LibraryFile1.hpp:
#include "Subdir/LibraryFile2.hpp"
LibraryFile2.hpp:
#include "LibraryFile3.hpp"
You will have told the compiler to look for the headers (using a -I
option) in something like LibraryRoot/include, which is where
LibraryFile1.hpp is located; LibraryFile2.hpp is relative to this
location, and in LibraryFile2.hpp, the compiler finds
LibraryFile3.hpp because it is in the same directory as the file which
includes it. If you try to include LibraryFile3.hpp directly,
however, the compiler won't find it.
File includes between <> are looked for in your compiler's path, whereas "" is looking relatively to your current directory (or absolute if you specify a path that starts with / or c:\ but this is not recommended)
On Unix systems, by default the path contains /usr/include. This path may be completed by adding -Isome_directory for it to search in it.
For example, if you have your file test.c and you want to include include/test.h, you have different choices:
Write #include "include/test.h", which will look relatively from the directory of the compiled file.
Write #include <test.h>, but this time you will need to specify -Iinclude to the compiler to add the ./include directory to the compiler's path.
Note, however, that some compilers accept the "" notation for lookups in the path, but that always confused me and is a bad thing to do.
The quotes mean include from local folder and the <> mean to include from another directory specified using a flag to g++ or MSVC or whatever compiler you are using or system headers.
<> looks in the default directory for include files, "" looks in the current directory and than in the default directory
This question is a duplicate of Question 21593. None of the above answers above are totally correct. Like many programmers, I have used the informal convention of using the "myApp.hpp" form for application specific files, and the form for library and compiler system files, i.e. files specified in /I and the INCLUDE environment variable. However, the C standard states that the search order is implementation specific.
Here's the msdn explanation copied here for your convenience).
Quoted form
The preprocessor searches for include files in this order:
1. In the same directory as the file that contains the #include statement.
2. In the directories of the currently opened include files, in the reverse order in which
they were opened. The search begins in the directory of the parent include file and
continues upward through the directories of any grandparent include files.
3. Along the path that's specified by each /I compiler option.
4. Along the paths that are specified by the INCLUDE environment variable.
Angle-bracket form
The preprocessor searches for include files in this order:
1. Along the path that's specified by each /I compiler option.
2. When compiling occurs on the command line, along the paths that are specified by the INCLUDE
environment variable.
Including a file using <> will tell the compiler to look for those files in environment-defined inclusion folders. Those folders can be standard system folders, or defined by your Makefile if you use one, etc. Using "", the compiler will look for inclusion files only in the source file's path.
So you can use "" and use absolute paths or the path which is relative to the source file you try to include in, or you can use <> after defining your inclusion folders, and just specify the name of the header file to include.
IMHO, the second option is better, especially if you use a lot of headers, or multiple libraries, etc...
To define inclusion folders at compilation time : gcc -I ... (man gcc!)

Visual Studio 2010 - files in folders without ../../Include.h

My physical file structure for a project I have is something like:
Source folder
Engine
Folder1
Folder2
etc.
I have some files in 'Source', some in 'Engine', some in 'Engine/Folder1', etc.
On my project, I have gone All Configurations->Source Directories and included Source, Engine, Engine/Folder2, etc. However, I still get errors that it cannot find files when I try to include "Foo.h" or whatever from a different folder. Is there a way to make it so I don't have to have ../Folder1/ in front of everything?
Is there a way to make it so I don't have to have ../Folder1/ in front of everything?
Yes, there is. The answer depends on several factors and I'm sure I'll miss a few.
Check the following:
In compiler settings check "Additional Includes" under "C/C++"
Also check in "VC++" the value for "Include Directories"
Check the setting for "Ignore Standard Include Paths" in "C/C++/Preprocessor"
Check your precompiled header settings
Check your "#define" / "#undef" in your source files and the compiler settings
Check the property sheets your project may be using or inheriting
If you use "foo.h" (rather than <foo.h>) the preprocessor will look in your project specific folders first and in the IDE specific folders last. If you use <foo.h> it starts in the standard include folders first, e.g. those that are required for standard runtime libraries.
When a file uses "../foo.h" its a path relative to the location of the file that includes the file. There can be tricky exceptions.
There are many more things that can influence how the preprocessor finds its include paths. If you are unsure what the preprocessor is doing with a particular file you can make the preprocessor output visible by switch on "Preprocess to a file" in the preprocessor settings. The file shows you the source code for a file after the preprocessor is finished and before the compiler starts its work.
The whole thing becomes much easier with more experience and in particular a clear strategy for the folder/project structure and how to include files. For example make sure you have "#pragma once" as the first non-comment line in each include file.
I hope this gives you a few ideas for the next steps. Good luck!
I consider this to be a good practice:
When the included file path doesn't require the use of "..", use relative paths
When it requires the use of "..", use absolute paths (that is, relative to the root folder of your source files.
For absolute paths to work, add the root source folder to the include directories list (relative to the project's file location).