Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve compilation dependency handling #78

Closed
jcoupey opened this issue Feb 9, 2018 · 3 comments
Closed

Improve compilation dependency handling #78

jcoupey opened this issue Feb 9, 2018 · 3 comments

Comments

@jcoupey
Copy link
Collaborator

jcoupey commented Feb 9, 2018

This ticket captures a problem discussed in #76.

TL;DR: dependencies are not handled properly with the current makefile.

This is not a problem when checking out one version and compiling once. But in a dev environment, changing files does not always trigger all required re-compilation. For a more detailed explanation, see @sashakh comment and proposed fix.

I'm by no mean an expert at compilation and makefiles, so I'd be grateful for other views on this to pick a solution.

@krypt-n
Copy link
Contributor

krypt-n commented Feb 14, 2018

A simple solution would be:

--- a/src/makefile
+++ b/src/makefile
@@ -33,6 +33,7 @@ else
 endif
 
 OBJ = $(SRC:.cpp=.o)
+DEPS = $(SRC:.cpp=.d)
 
 # Main target.
 all : $(MAIN) $(LIB)
@@ -46,8 +47,10 @@ $(LIB) : $(OBJ)
 	$(AR) cr $@ $^
 
 # Building .o files.
-%.o : %.cpp %.h
-	$(CXX) $(CXXFLAGS) -c $< -o $@
+%.o : %.cpp
+	$(CXX) $(CXXFLAGS) -MMD -MP -c $< -o $@
+
+-include ${DEPS}
 
 # Specific target for main (no .h file).
 main.o : main.cpp

This causes the c++ compiler to emit .d files that contain the header-cpp dependency information. These files are then included during the next run of make.

Pro: this does not require manual invocation of make dep, everything is handled automatically

Contra: a lot of .d files in the src-directory. They (aswell as the .o files) should at least be gitignored.

Do you want a PR for this change?

@jcoupey
Copy link
Collaborator Author

jcoupey commented Feb 14, 2018

Thanks for the input @krypt-n. The advantage I see with this approach is that there is no need to use an additional custom target to generate the dependency files, so this would work out of the box for anyone just using plain make.

The downside of having .d files all around does not look problematic to me, as long as they're ignored by git.

A PR would be really appreciated!

@jcoupey jcoupey added this to the v1.2.0 milestone Feb 14, 2018
@jcoupey
Copy link
Collaborator Author

jcoupey commented Feb 14, 2018

Solved by #85

@krypt-n thanks a lot for the PR and the makefile springclean!

@jcoupey jcoupey closed this as completed Feb 14, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants