c++ - How to create a makefile for several .cpp files with .h files and main.cpp without a .h -



c++ - How to create a makefile for several .cpp files with .h files and main.cpp without a .h -

i have:

main.cpp distance.cpp distance.h adjacencylist.cpp adjacencylist.h

here makefile:

all: distance main adjacencylist g++ distance.o main.o adjacencylist.o main.o: main.cpp g++ main.cpp -lstdc++ adjacencylist.o: adjacencylist.cpp g++ adjacencylist.cpp -lstdc++ distance.o: distance.cpp g++ distance.cpp -lstdc++ clean: rm -rf *.o

i getting error. i'm pretty sure i'm doing wrong main because not class other 2 , not have .h file.

update:

after trying ben voigt's solution getting 1 error:

your rules create object files missing -c option, "compile only". trying link, , failing because there no main().

then, all target names executable each of compilation units. again, that's wrong because don't have main(). should have 1 executable. all should configured phony target, because doesn't build actual file named all.

all rules failing command name of output file.

all rules failing pass flags.

your rules missing dependencies on headers, editing headers won't cause right files recompiled.

really, should rid of compile , link rules , allow make utilize built-in ones. focus on build targets , dependencies.

your end makefile should (of course, using spaces not tabs)

all : main .phony : clean cc = g++ ld = g++ main : main.o adjacencylist.o distance.o main.o: main.cpp adjacencylist.h distance.h adjacencylist.o: adjacencylist.cpp adjacencylist.h distance.o: distance.cpp distance.h clean: rm -rf *.o main

c++ makefile

Comments

Popular posts from this blog

Delphi change the assembly code of a running process -

json - Hibernate and Jackson (java.lang.IllegalStateException: Cannot call sendError() after the response has been committed) -

C++ 11 "class" keyword -