c++ - Why won't my project link without cleaning? -
i getting started makefiles, , have written 1 project has 2 targets. if run new make works fine. if don't clean after that, , edit , save 1 source file, example, , run make again, project compiles source file edited , makes no attempt link sources new executable.
in other words: first time run make $(cc) -c <sourcefile> each source, , $(cc) -o <sourceobject1> <sourceobject2>... , works. if go , edit sourcefile, , try make again, $(cc) -c <editedsourcefile>, , no linking after that! @ least make not trying compile source files again, why isn't linking me new executable? expected behavior?
bonus question: there nicer way move object files subdirectory sticking mv *.o @ top of target rule? have looked @ this answer, didn't understand going on , couldn't figure out how adapt case 2 targets.
here makefile:
cc = clang++ optflags = -o3 -flto -m64 cflags = -wall -c $(optflags) lflags = -wall $(optflags) make = make rm = \rm -f target-dir = build obj-dir = $(target-dir)/obj objs = $(addprefix $(obj-dir)/, mcts.o nodepool.o node.o fastmath.o \ board.o patterns.o fastset.o fasthash.o fasthash2.o fasthashmap.o) gtp-objs = $(addprefix $(obj-dir)/, gtpplayer.o) gtp-target = $(target-dir)/go-gtp test-objs = $(addprefix $(obj-dir)/, testsmain.o mctstests.o nodepooltests.o \ nodetests.o fastmathtests.o boardtests.o fastsettests.o \ fasthashtests.o fasthash2tests.o fasthashmaptests.o) test-target = $(target-dir)/go-test .phony : : $(gtp-target) $(test-target) $(gtp-target) : $(gtp-objs) $(objs) mv *.o $(obj-dir) $(cc) $(lflags) $(gtp-objs) $(objs) -o $(gtp-target) $(obj-dir)/gtpplayer.o : gtpplayer.hpp gtpplayer.cpp mcts/mcts.hpp $(cc) $(cflags) gtpplayer.cpp $(obj-dir)/mcts.o : mcts/mcts.hpp mcts/mcts.cpp mcts/nodepool.hpp $(cc) $(cflags) mcts/mcts.cpp $(obj-dir)/nodepool.o : mcts/nodepool.hpp mcts/nodepool.cpp mcts/node.hpp $(cc) $(cflags) mcts/nodepool.cpp $(obj-dir)/node.o : mcts/node.hpp mcts/node.cpp mcts/fastmath.hpp board/board.hpp $(cc) $(cflags) mcts/node.cpp $(obj-dir)/fastmath.o : mcts/fastmath.hpp mcts/fastmath.cpp $(cc) $(cflags) mcts/fastmath.cpp $(obj-dir)/board.o : board/board.hpp board/board.cpp board/patterns.hpp struct/fastset.hpp struct/fasthash.hpp $(cc) $(cflags) board/board.cpp $(obj-dir)/patterns.o : board/patterns.hpp board/patterns.cpp struct/fasthash2.hpp struct/fasthashmap.hpp $(cc) $(cflags) board/patterns.cpp $(obj-dir)/fastset.o : struct/fastset.hpp struct/fastset.cpp $(cc) $(cflags) struct/fastset.cpp $(obj-dir)/fasthash.o : struct/fasthash.hpp struct/fasthash.cpp $(cc) $(cflags) struct/fasthash.cpp $(obj-dir)/fasthash2.o : struct/fasthash2.hpp struct/fasthash2.cpp $(cc) $(cflags) struct/fasthash2.cpp $(obj-dir)/fasthashmap.o : struct/fasthashmap.hpp struct/fasthashmap.cpp $(cc) $(cflags) struct/fasthashmap.cpp $(test-target) : $(test-objs) $(objs) mv *.o $(obj-dir) $(cc) $(lflags) $(test-objs) $(objs) -o $(test-target) $(obj-dir)/testsmain.o : test/testsmain.cpp test/mctstests.hpp test/nodepooltests.hpp \ test/nodetests.hpp test/fastmathtests.hpp test/boardtests.hpp \ test/fastsettests.hpp test/fasthashtests.hpp test/fasthash2tests.hpp test/fasthashmaptests.hpp $(cc) $(cflags) test/testsmain.cpp $(obj-dir)/mctstests.o : test/mctstests.hpp test/mctstests.cpp mcts/mcts.hpp $(cc) $(cflags) test/mctstests.cpp $(obj-dir)/nodepooltests.o : test/nodepooltests.hpp test/nodepooltests.cpp $(cc) $(cflags) test/nodepooltests.cpp $(obj-dir)/nodetests.o : test/nodetests.hpp test/nodetests.cpp $(cc) $(cflags) test/nodetests.cpp $(obj-dir)/fastmathtests.o : test/fastmathtests.hpp test/fastmathtests.cpp $(cc) $(cflags) test/fastmathtests.cpp $(obj-dir)/boardtests.o : test/boardtests.hpp test/boardtests.cpp $(cc) $(cflags) test/boardtests.cpp $(obj-dir)/fastsettests.o : test/fastsettests.hpp test/fastsettests.cpp $(cc) $(cflags) test/fastsettests.cpp $(obj-dir)/fasthashtests.o : test/fasthashtests.hpp test/fasthashtests.cpp $(cc) $(cflags) test/fasthashtests.cpp $(obj-dir)/fasthash2tests.o : test/fasthash2tests.hpp test/fasthash2tests.cpp $(cc) $(cflags) test/fasthash2tests.cpp $(obj-dir)/fasthashmaptests.o : test/fasthashmaptests.hpp test/fasthashmaptests.cpp $(cc) $(cflags) test/fasthashmaptests.cpp .phony : clean clean : $(rm) $(obj-dir)/*.o .phony : distclean distclean : $(make) clean $(rm) $(gtp-target) $(test-target) .phony : again again: $(make) distclean $(make) $(gtp-target) $(make) $(test-target) .phony : tar tar: tar cfv $(gtp-target).tar $(gtp-objs) $(objs) tar cfv $(test-target).tar $(test-objs) $(objs) example output:
$ make clang++ -wall -c -o3 -flto -m64 gtpplayer.cpp clang++ -wall -c -o3 -flto -m64 mcts/mcts.cpp clang++ -wall -c -o3 -flto -m64 mcts/nodepool.cpp clang++ -wall -c -o3 -flto -m64 mcts/node.cpp clang++ -wall -c -o3 -flto -m64 mcts/fastmath.cpp clang++ -wall -c -o3 -flto -m64 board/board.cpp clang++ -wall -c -o3 -flto -m64 board/patterns.cpp clang++ -wall -c -o3 -flto -m64 struct/fastset.cpp clang++ -wall -c -o3 -flto -m64 struct/fasthash.cpp clang++ -wall -c -o3 -flto -m64 struct/fasthash2.cpp clang++ -wall -c -o3 -flto -m64 struct/fasthashmap.cpp mv *.o build/obj clang++ -wall -o3 -flto -m64 build/obj/gtpplayer.o build/obj/mcts.o build/obj/nodepool.o build/obj/node.o build/obj/fastmath.o build/obj/board.o build/obj/patterns.o build/obj/fastset.o build/obj/fasthash.o build/obj/fasthash2.o build/obj/fasthashmap.o -o build/go-gtp clang++ -wall -c -o3 -flto -m64 test/testsmain.cpp clang++ -wall -c -o3 -flto -m64 test/mctstests.cpp clang++ -wall -c -o3 -flto -m64 test/nodepooltests.cpp clang++ -wall -c -o3 -flto -m64 test/nodetests.cpp clang++ -wall -c -o3 -flto -m64 test/fastmathtests.cpp clang++ -wall -c -o3 -flto -m64 test/boardtests.cpp clang++ -wall -c -o3 -flto -m64 test/fastsettests.cpp clang++ -wall -c -o3 -flto -m64 test/fasthashtests.cpp clang++ -wall -c -o3 -flto -m64 test/fasthash2tests.cpp clang++ -wall -c -o3 -flto -m64 test/fasthashmaptests.cpp mv *.o build/obj clang++ -wall -o3 -flto -m64 build/obj/testsmain.o build/obj/mctstests.o build/obj/nodepooltests.o build/obj/nodetests.o build/obj/fastmathtests.o build/obj/boardtests.o build/obj/fastsettests.o build/obj/fasthashtests.o build/obj/fasthash2tests.o build/obj/fasthashmaptests.o build/obj/mcts.o build/obj/nodepool.o build/obj/node.o build/obj/fastmath.o build/obj/board.o build/obj/patterns.o build/obj/fastset.o build/obj/fasthash.o build/obj/fasthash2.o build/obj/fasthashmap.o -o build/go-test now if edit , save board.cpp example , run make again, this:
$ make clang++ -wall -c -o3 -flto -m64 board/board.cpp what happened linking command? want new target!
it won't link because newer obj files not in obj_dir in wherever before moved (current directory?). make looks @ executable , object files , correctly concludes executable newer object files in obj_dir.
make compiler generate them in obj_dir in first place (use -o) or move mv command each of rules generating object files (not recommended) after running rule, obj file in obj_dir updated.
Comments
Post a Comment