I was wondering …
When you make a change to either file1.c or file2.c or file1.h, the following makefile recompiles only what’s needed (which is nice)
# Link to executable
result: file1.o file2.o
gcc file1.o file2.o -o result23
# Assemble to .o object files
file1.o: file1.s
gcc -c dist/file1.s
file2.o: file2.s
gcc -c dist/file2.s
# Compile to .s assembly files
file1.s: file1.c
gcc -S file1.c
file2.s: file2.c
gcc -S file2.c
When I move built object to another directory, however, everything is rebuilt at all times, regardless whether only 1 file’s content was changed.
# Link to executable
result: file1.o file2.o
gcc file1.o file2.o -o result23
# Assemble to .o object files
file1.o: file1.s
gcc -c dist/file1.s
mv file1.o dist
file2.o: file2.s
gcc -c dist/file2.s
mv file2.o dist
# Compile to .s assembly files
file1.s: file1.c
gcc -S file1.c
mv file1.s dist
file2.s: file2.c
gcc -S file2.c
mv file2.s dist
It seems that this is happening because make does not know where the .o files are in it’s environment.
With this a few questions:
- Can make have access to environment variables? If so, could you provide an example?
- Can make be made aware of incremental builds when distribution directory is different from current directory?
To fix your build you’ll need to do something like this:
You can certainly use environment variables in make too.. though I’m not sure how that relates to the core of your question: Just use something like this, set BUILDDIR and OBJDIR and have a makefile like this:
Though you can make this (and my fixed makefile too) nicer using automatic variables: