Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I search directory and subdirectories for header files?

Tags:

c++

c

gcc

makefile

Is is possible to instruct g++ to search a folder recursively for header files? In my example I would like g++ to search

/ARDrone_SDK_2_0_1/ARDroneLib/Soft/

and all subdirectories for header files. Can this be done? Here's a simple Makefile example:

C=g++
CFLAGS=-c -Wall
LDFLAGS=
INC1=/ARDrone_SDK_2_0_1/ARDroneLib/Soft/ <- can this be recursive?
INCDIRS= -I${INC1}
SOURCES=src/dronevid.cpp
OBJECTS=$(SOURCES:.cpp=.o)
EXECUTABLE=build/dronevid

all: $(SOURCES) $(EXECUTABLE)

$(EXECUTABLE): $(OBJECTS) 
    $(CC) $(LDFLAGS) $(OBJECTS) -o $@

.cpp.o:
    $(CC) $(CFLAGS) $(INCDIRS) $< -o $@
like image 317
Nick Avatar asked Nov 02 '13 04:11

Nick


People also ask

How do you tell the compiler to look for header files in a different directory?

it's simple, use the "-B" option to add . h files' dir to search path. Show activity on this post. Headers included with #include <> will be searched in all default directories , but you can also add your own location in the search path with -I command line arg.

Where can I find header files?

These are the directories that gcc looks in by default for the specified header files ( given that the header files are included in chevrons <>); 1. /usr/local/include/ --used for 3rd party header files. 2. /usr/include/ -- used for system header files.

Can Python search file subdirectories?

Yes. It is rather unlike that there is a directory named like this.

How does GCC search for header files?

GCC looks for headers requested with #include " file " first in the directory containing the current file, then in the directories as specified by -iquote options, then in the same places it would have looked for a header requested with angle brackets. For example, if /usr/include/sys/stat. h contains #include "types.


1 Answers

The question is a little confusing because you're conflating two different tools, make and g++.

There is no way to get g++ search all subdirectories of a given directory. Every directory you want to use to find an included file must be individually specified on the command line with a -I flag.

If you want to, you can get make to construct those arguments and put them on your command line. Assuming you're using GNU make, and a UNIX-like system that supports the find command, you can do something like this:

INCDIRS := $(addprefix -I,$(shell find /ARDrone_SDK_2_0_1/ARDroneLib/Soft -type d -print))

I should just say up-front, this is not really a good idea. You don't know what order those directories will show up in, and you don't know if there are multiple copies of the same header file in different directories that might cause problems.

Generally the way headers in subdirectories are expected to work is that you add the top-level directory to the compile line, then use relative paths in the #include line in your code. Something like:

#include <subdir/subsubdir/header.h>

Then add:

-I/top/level/dir

to the g++ compile line.

like image 173
MadScientist Avatar answered Nov 15 '22 18:11

MadScientist