How to exclude some specific header files from include path in Makefile? - makefile

Let's assume we have two directories dirA and dirB which contains 50 files each.
dirA - header_1.hh header_2.hh header_3.hh ---- header_50.hh
dirB - header_1.hh header_2.hh header_3.hh ----- header_50.hh
In my main.cc file, I want to include header_<1-40>.hh files from dirA, and remaining header_<41-50>.hh from dirB.
In Makefile, I have include path as:
INCL_PATH += -I/dirA
INCL_PATH += -I/dirB
How can I exclude 41-50 header files from getting included from path A?

You can't change the include path in the middle of a compilation unit -- there's no preprocessor directive to do that. You best bet is probably to include the directories in the #include directives in the file:
#include <dirA/header_1.hh>
:
#include <dirA/header_40.hh>
#include <dirB/header_41.hh>
:
#include <dirB/header_50.hh>
Then just have your -I option specify the parent of dirA/dirB

Related

gulp-preprocess. How to set #include directory path with a higher lever than current directory?

For example there is the following structure of a project fragment:
directory1
file1.inc
directory2
file.php
How in gulp-preprocess to write <-- #include ??? --> instruction in the file.php, to include file.inc?

How to create this makefile? Sources in subdirectories, separate output path, linking objects in subdirectories

Is there a way to create a makefile that does this?
I gave up after trying to follow the docs and lots of trial and error so I'll just post a description of what the makefile should do.
general directory structure:
src/ - contains c source files in various subdirectories (written manually by maintainer)
inc/ - contains h header files in subdirectories matching src (written manually by maintainer)
obj/ - contains o header files in subdirectories matching src (autogenerated by a make call)
bin/ - should contain binary (autogenerated by a make call)
makefile
so for example at a given point of time the project might look like
src/
main.c
sub1/
other1.c
other2.c
sub2/
sub3/
other3.c
inc/
sub1/
other1.h
other2.h
sub2/
sub3/
other3.h
obj/
main.o
sub1/
other1.o
other2.o
sub2/
sub3/
other3.o
bin/
release
makefile
(probably not relevant: Note that main doesn't have a header file but most likely every other c file will have a matching h file.)
I want to be able to call make, and have it:
use gcc to recompile only changed c files into respective o files in obj/, generating missing subdirectories if needed.
for example, from the above state, if I add a new subdirectory sub4 inside src/sub1/, and then create other4.c inside src/sub1/sub4/, I would like make to generate sub4 inside obj/sub1/ and then generate other4.o inside obj/sub1/sub4/
create a binary at bin/release by linking all object files (from all subdirectories in obj/)
I don't want to have to change the makefile each time I add directories in src
I don't want to manually have to create directories in obj, the makefile should take care of it. if this is not possible, maybe have it rename all obj o files to a flat naming pattern? i.e. obj/sub2_sub3_other3.o instead of obj/sub2/sub3/other3.o (although this can cause issues)
probably not relevant here, but the C files use include statements in this format:
#include "sub2/sub3/other3.h"
so -I./inc would be included in the gcc call. Whereas the linker would receive inputs like -s -O3. I want to make sure those options (compiler options, linker options) are listed at the top of the makefile in variables (CFLAGS, LDFLAGS, etc) and not passed incorrectly to the targets.
is this even possible? if not, what's the closest possible?
Also, can this makefile be made to work on both POSIX systems and on Windows based systems? e.g. work the same on linux/gcc and win/mingw

C++ makefile with relative include path to parent

I've got a working project that I need to take parts from it without changing it's code and just write new main() for it.
My directory structure:
[main_dir]/[main.cpp]
[main_dir]/[dir1]/[child1]/file1.h
[main_dir]/[dir2]/[child2]/file2.h
in main.cpp I have: include "dir1/child1/file1.h"
In file1.h I have: include "dir2/child2/file2.h"
I'm compiling:
g++ main main.cpp
I'm getting "dir2/child2/file2.h" no such file or directory.
I can't change file1 to do: include "../../dir2/child2/file2.h"
Somehow in the original project something in the makefile to search for all include path relative to the [main_dir] so the include from file1.h can be found.
What should I add to the makefile in order to do it as well?
When using double-quotes to include a header file, as in
#include "dir2/child2/file2.h"
then the compiler will use the directory of the current file to search for the header file.
If you have it in your file1.h then the compiler will look for the header file [main_dir]/dir1/child1/dir2/child2/file2.h. Which isn't correct.
You can solve it by telling the compiler to add [main_dir] to the list of standard include search paths. This is done with the -I (upper-case i) option:
g++ -I [main_dir] main.cpp
Then when the compiler fails to find dir2/child2/file2.h in its first search, it will continue with the list of standard include search paths, and it should be found.
You need to manage CPPFLAGS in your Makefile.
CPPFLAGS="-I[main_dir]"
And compile application with receipt like this:
g++ $(CPPFLAGS) main.cpp -o main
Also it's recommended to read make style guides to write a good one Makefile. You can meet there tips for include folders declaration.

How can I make gcc search for #include <...> files in the current source file's directory?

When a file contains an include line like this:
#include "name.h"
gcc (and other C compilers) search for the file "name.h" in the directory containing the file being compiled. This does not by default happen if the line looks like this:
#include <name.h>
Is there an option to gcc to make it behave this way in the latter case too? As noted in the gcc documentation, "-I. searches the compiler's current working directory for header files. That may or may not be the same as the directory containing the current file." In the case I am working on (importing external code that used a build environment that automatically added the containing directory to the search path into a system that doesn't have such a build facility), the current directory is unfortunately not the same. What can I do? I'd rather not have to specifically modify the files...

Is include path relative to current directory or source code location?

I am confused about makefiles searching the include paths.
Lets say I have a file structure:
.
├── hdrMainFolder.h
├── headers
│   └── hdrDifferentPath.h
├── makefile
├── sourceCode.cpp
└── src
├── hdrSamePath.h
└── src1.cpp
I use -I option in the makefile to indicate the paths of the included headers.
Here are the included headers from src1.cpp
#include "hdrSamePath.h"
#include "hdrMainFolder.h"
#include "hdrDifferentPath.h"
Which of the paths I should indicate explicitly in the makefile? Which of them are unnecessary? Is it enough like below?
INCLUDING = -Isrc -Iheaders
Is it necessary to indicate to the path of a header, if it is only included by a source file under the same path?
Which of the paths I should indicate explicitly in the makefile?
Which of them are unnecessary?
On the command shell, the directory where compilation instruction is being run is the current directory for the compiler.
[Compilation being done using direct command by user or using makefile]
The current directory (./) is default included by compiler for header file search paths.
If you create sub-directories and place your header files in sub-directory structure, then, you need to explicitly add -I rule for each sub-directory which contains the required header files.
hdrMainFolder.h -> present in current directory, no need to add -I rule for this
hdrDifferentPath.h -> need to add -I rule (-I./headers)
hdrSamePath.h -> need to add -I rule (-I./src)
[You may omit ./ in above added -I rules, I follow for better clarity]
Is it necessary to indicate to the path of a header, if it is only
included by a source file under the same path?
Yes, source file location is not used to determine the user-defined header file search path. Need to explicitly mention it.

Resources