Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can Makefile use separate directories for source code and binaries?

Tags:

I want to be able to split up my bin and my code files into separate directories as it is becoming hard to manage in it's current state.

I ideally would like to have

project_dir |-Makefile |-run_tests.sh | |__source |  |-program1.cpp |  |-program2.cpp | |__bin    |-program1    |-program2 

However I am unable to get this to work with my current system without having to manually write out the rules for every program (bear in mind that every program is a separate program, not a series of objects linked together)

#Current make system BIN=./bin/ SOURCE=./source/  LIST=program1 program2...  all: $(LIST)  %:  $(SOURCE)%.cpp     $(CC) $(INC) $< $(CFLAGS) -o $(BIN)$@ $(LIBS) 

this works except it I it can't see the target in the current path so it think it always rebuilds the binaries even if the source files haven't changed.

My only thought at the moment is to write a program to make a makefile but I don't want to do that.

like image 789
111111 Avatar asked Feb 07 '12 14:02

111111


People also ask

Can makefile target be a directory?

Yes, a Makefile can have a directory as target. Your problem could be that the cd doesn't do what you want: it does cd and the git clone is carried out in the original directory (the one you cd ed from, not the one you cd ed to). This is because for every command in the Makefile an extra shell is created.

How do I link my makefile to .a files?

$(CC) $(CFLAGS) -I$(LIB_PATH) -L$(LIB_PATH) -o $(PROGRAM) main. c -l$(LIB) `pkg-config ...` Basically, you need set the include path to the . h file with -I, then -L for the lib path and -l to set the lib name.

Where should I put my makefile?

some projects put their makefile in src/ subdirectory of the root directories of the projects, some projects put their makefiles in the root directory of the project.


2 Answers

You were almost there ...

#Current make system BIN=./bin/ SOURCE=./source/  LIST=$(BIN)/program1 $(BIN)/program2...  all: $(LIST)  $(BIN)/%:  $(SOURCE)%.cpp     $(CC) $(INC) $< $(CFLAGS) -o $@ $(LIBS) 

You can also make the LIST easier by using the following

PROG=program1 program2 LIST=$(addprefix $(BIN)/, $(PROG)) 
like image 151
Didier Trosset Avatar answered Oct 23 '22 08:10

Didier Trosset


Here is a complete ready-to-go version of the Makefile.

Prepare your folder with the following directory

for .o files:/build/ for .cpp files: /src/ for binary files: /bin/

Then use the following Makefile at /

CC          = g++ LD          = g++  CFLAG       = -Wall PROG_NAME   = prog  SRC_DIR     = ./src BUILD_DIR   = ./build BIN_DIR     = ./bin SRC_LIST = $(wildcard $(SRC_DIR)/*.cpp) OBJ_LIST = $(BUILD_DIR)/$(notdir $(SRC_LIST:.cpp=.o))  .PHONY: all clean $(PROG_NAME) compile  all: $(PROG_NAME)  compile:      $(CC) -c $(CFLAG) $(SRC_LIST) -o $(OBJ_LIST)  $(PROG_NAME): compile     $(LD) $(OBJ_LIST) -o $(BIN_DIR)/$@  clean:     rm -f $(BIN_DIR)/$(PROG_NAME) $(BUILD_DIR)/*.o 

Your final program will be named as prog in the /bin/, everytime you compile the code, binaries will be separated at /build/ so that source code directory will be very neat and clean.

like image 29
ArtificiallyIntelligence Avatar answered Oct 23 '22 06:10

ArtificiallyIntelligence