Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Only first command of makefile is executed

Tags:

makefile

I have 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

Problem is only one command of that file is being executed, so if I want to execute the next one< I have to delete or comment previous command. What is wrong with this makefile?

like image 986
unresolved_external Avatar asked Oct 11 '12 10:10

unresolved_external


2 Answers

Make execute by default the first rule you provide it.

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

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

and place it at the begin of your Makefile or call it with make all

like image 62
tomahh Avatar answered Nov 08 '22 17:11

tomahh


This is by design. You can tell make which targets to build:

make main.o DriveInfo.o

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

all: main.o DriveInfo.o …

Now, make all (or just make) will build all dependent targets. It’s also good practice to declare all as a phony target to tell make that it doesn’t correspond to an actually existing file. Do this by putting .PHONY: all in the Makefile.

A final remark, the name all is arbitrary. You could use any other name here but all is conventional. The important part is that it’s the first rule in your Makefile if you want it to be the one that’s executed when you call make without any explicit targets.

like image 5
Konrad Rudolph Avatar answered Nov 08 '22 17:11

Konrad Rudolph