Kulpas
Kulpas

Reputation: 394

g++ -I include all subdirectories with header files

I've just stumbled upon this post about compiling all .cpp files, including those in subdirectories using the linux find command:

g++ -g $(find RootFolderName -type f -iregex ".*\.cpp") -o OutputName

The problem with this is that all files need their relative path written out when doing #include for this to work. You can get around it by adding what ever directory you need using the -I tag:

g++ -g $(find RootFolderName -type f -iregex ".*\.cpp") -o OutputName -I ./somePath

But that's still quite a hassle if you have multiple subdirectories. Is it possible to use find again with some other regular expression to include all of the subdirectories?

Upvotes: 0

Views: 1030

Answers (1)

KamilCuk
KamilCuk

Reputation: 140960

Is it possible to use find again with some other regular expression to include all of the subdirectories?

Yes it is - some projects, like mbed and arduino, seem to include all possible directories to include paths. In shell assuming there are no whitespaces, you could:

find . -type f -iname '*.h' -printf "-I%h\n" | sort -u

This is error prone to whitespaces in path. When using:

command $(stuff)

you will have problems with spaces in filenames. Research other methods and how to handle whitespaces in shell. Better yet, do not write such stuff manually and reinvent the wheel and move to a build system, like cmake.

Upvotes: 2

Related Questions