User Tools

Site Tools


guides:software:make:start

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
guides:software:make:start [2020/07/20 17:30] – [Makefiles for Multiple File Compilation] wikiadminguides:software:make:start [2022/08/02 11:59] (current) – external edit 127.0.0.1
Line 3: Line 3:
  
 Make is a build automation tool that is designed to automatically construct executable programs from source code. Make is a build automation tool that is designed to automatically construct executable programs from source code.
 +
 +Make simplifies the build process for projects at all levels.
  
 ===== Simple Use ===== ===== Simple Use =====
Line 291: Line 293:
  
 One of the most powerful uses of make is to compile multiple files into a single executable. One of the most powerful uses of make is to compile multiple files into a single executable.
 +
 +==== Example ====
  
 For this example the code is decomposed into the following files: For this example the code is decomposed into the following files:
Line 379: Line 383:
 OBJS =  hello OBJS =  hello
  
-.PHONY: all 
 all: ${OBJS} all: ${OBJS}
  
Line 388: Line 391:
 stringTools.o: stringTools.h stringTools.o: stringTools.h
  
-.PHONY: clean 
 clean: clean:
         rm -f ${OBJS} *.o         rm -f ${OBJS} *.o
Line 416: Line 418:
 </code> </code>
  
-Note that all components are compiled and the final executable is produced.  If any of the component files change, make will recompile only the parts necessary to rebuild the entire project.+Note that all components are compiled and the final executable is produced.  If any of the component files change, make will recompile only the parts necessary to rebuild the entire project.  For example, if greeting.cpp were change, that file, along with hello.cpp would need to be recomipled, but stringTools.o would not need to be rebuilt. 
 + 
 +<code> 
 +$ touch greeting.cpp 
 +$ make 
 +g++ -g -O3 -Wpedantic -Wall -Wextra -Wmisleading-indentation -Wunused -Wuninitialized -Wshadow -Wconversion -std=c++14   -c -o greeting.o greeting.cpp 
 +g++ -g -O3 -Wpedantic -Wall -Wextra -Wmisleading-indentation -Wunused -Wuninitialized -Wshadow -Wconversion -std=c++14    hello.cpp greeting.o stringTools.o   -o hello 
 +</code> 
 + 
 +==== The Makefile ==== 
 + 
 +In order to build this project using make, you need to inform make of the dependencies built into the project.  In this case, to build hello, we need to have both //greeting.o// and //stringTools.o// because the code in hello calls code in both of these files.  The following line in the makefile performs this task 
 + 
 +<code make> 
 +hello:  greeting.o stringTools.o 
 +</code> 
 + 
 +You do not need to tell make that //hello// depends on //hello.cpp//, that is built into the system. 
 + 
 +You also do not need to tell make that //hello// depends on //greeting.h// or //stringTools.h// This is accomplished by the next set of instructions in the makefile. 
 + 
 +<code make> 
 +stringTools.o: stringTools.h 
 +</code>  
 + 
 +This line tells make that //stringTools.o// depends on //stringTools.h// Make does not have default rules for header files, so this line is necessary.  The result of this line is if stringTools.h changes, stringTools.o will need to be rebuilt.   If this happens, make is able to deduce that //hello// will need to be rebuilt as well.  This is good since //hello.cpp// includes //stringTools.h//
 + 
 +<code make> 
 +greeting.o: greeting.h stringTools.h 
 +</code> 
 + 
 +This line tells make that //greeting.o// depends on both //greeting.h// and //stringTools.h// Again //greeting.o// also depends on //greeting.cpp//, but make has this rule built in.   
guides/software/make/start.1595266248.txt.gz · Last modified: 2022/08/02 11:59 (external edit)