Path prefix to src and targets

David Deutsch <[email protected]>
Newsgroups gmane.comp.gnu.make.general
Message-ID <[email protected]>
Hi there,

I hope I'm not breaking any etiquette rules with this, but I was 
interested in seeing how GPT4 would respond to this request, so here goes:

https://chat.openai.com/share/72ec8309-6a66-45fc-b142-7fdc9cbb57b4

Content:

In GNU Make, a simple way to include all subdirectory makefiles in a 
"main" makefile would be to use the wildcard and include functions. 
However, as you've discovered, this can create confusion when you have 
duplicate file names in different directories.

One way to solve your problem would be to use a variable within each 
sub-Makefile to set the relative path to the sources and objects. Here's 
how you might do it.

In each of your sub-Makefiles, you would declare a variable to store the 
relative path. For instance:

|# In subdir_a/Makefile SUBDIR := subdir_a SOURCES := $(SUBDIR)/file.c # 
In subdir_b/Makefile SUBDIR := subdir_b SOURCES := $(SUBDIR)/otherfile.c 
# In subdir_b/subdir_c/Makefile SUBDIR := subdir_b/subdir_c SOURCES := 
$(SUBDIR)/file.c |

And so on. The |$(SUBDIR)| variable is being prepended to the names of 
your source files. Now, when the sources are compiled, they'll be 
compiled in the proper directory.

Your main Makefile would use the include function to include the 
sub-Makefiles, and the rule to compile your objects would use the 
|$(SOURCES)| variable.

|# Main Makefile include $(wildcard */Makefile) %.o: %.c $(CC) -c $< -o $@ |

Note that the pattern rule |%.o: %.c| will match the source files with 
the correct path, due to the |$(SUBDIR)| variable being prepended in the 
sub-Makefiles.

This way, when you compile a source file that exists in multiple 
directories, the appropriate version will be compiled thanks to the 
directory prefix.

Please remember to ensure the order of dependencies, and that the 
necessary header files are also located correctly.

The above approach helps to provide directory-based isolation and helps 
the build system distinguish between similarly named files in different 
directories.

Note: This solution does assume you're dealing with a relatively flat 
directory structure (i.e., not too many nested directories). If you are 
dealing with deeply nested directories, you may need to consider more 
sophisticated solutions, potentially including a build system that is 
designed to handle this type of project structure more elegantly, such 
as CMake or Bazel.
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.