Only the first makefile command is executed

I have the following makefile:

main.o: ServerSocket/main.cpp ../Shared/socket.h g++ -c ServerSocket/main.cpp -lstdc++ DriveInfo.o: ServerSocket/DriveInfo.cpp ServerSocket/DriveInfo.h ServerSocket/Consts.h g++ -c ServerSocket/DriveInfo.cpp -lstdc++ ProcessInfo.o: ServerSocket/ProcessInfo.cpp ServerSocket/ProcessInfo.h ServerSocket/Consts.h g++ -c ServerSocket/ProcessInfo.cpp -lstdc++ thread_pool.o: ServerSocket/thread_pool.cpp ServerSocket/thread_pool.h DriveInfo.o ProcessInfo.o thread_pool.o server: main.o g++ -o server main.o DriveInfo.o ProcessInfo.o thread_pool.o 

The problem is that only one command of this file is executed, so if I want to execute the following command <I need to delete or comment on the previous command. What is wrong with this makefile?

+7
source share
2 answers

Make the first rule you set the default.

You want to create a rule that fulfills all the rules that you provide

 all : main.o DriveInfo.o ProcessInfo.o thread_pool.o 

and put it at the top of your makefile or call it with make all

+17
source

This is by design. You can specify make for which you want to build:

 make main.o DriveInfo.o 

will build these two goals. In addition, if you want make to build all (or some) goals by default, you can specify a rule at the beginning of the Makefile, indicating that:

 all: main.o DriveInfo.o … 

Now make all (or just make ) will build all the dependent objects. Its also good practice to declare all as a fake target to tell make that it does not match the actual existing file. Do this by placing .PHONY: all in the Makefile.

One final note, the name all arbitrary. You can use any other name, but all is common. The important part is that this is the first rule in your Makefile if you want it to be the one that was executed when you invoke make without any explicit purpose.

+4
source

All Articles