Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Makefile : Build in a separate directory tree

Tags:

makefile

My project (an interpreted language) has a standard library composed by multiple files, each of them will be built into an .so dynamic library that the interpreter will load upon user request (with an import directive). Each source file is located into a subdirectory representing its "namespace", for instance :

libraries tree

The build process has to create a "build" directory, then when each file is compiling has to create its namespace directory inside the "build" one, for instance, when compiling

std/io/network/tcp.cc

he run an mkdir command with

mkdir -p build/std/io/network

The Makefile snippet is :

STDSRC=stdlib/std/hashing/md5.cc \
       stdlib/std/hashing/crc32.cc \
       stdlib/std/hashing/sha1.cc \
       stdlib/std/hashing/sha2.cc \
       stdlib/std/io/network/http.cc \
       stdlib/std/io/network/tcp.cc \
       stdlib/std/io/network/smtp.cc \
       stdlib/std/io/file.cc \
       stdlib/std/io/console.cc \
       stdlib/std/io/xml.cc \
       stdlib/std/type/reflection.cc \
       stdlib/std/type/string.cc \
       stdlib/std/type/matrix.cc \
       stdlib/std/type/array.cc \
       stdlib/std/type/map.cc \
       stdlib/std/type/type.cc \
       stdlib/std/type/binary.cc \
       stdlib/std/encoding.cc \
       stdlib/std/os/dll.cc \
       stdlib/std/os/time.cc \
       stdlib/std/os/threads.cc \
       stdlib/std/os/process.cc \
       stdlib/std/pcre.cc \
       stdlib/std/math.cc

STDOBJ=$(STDSRC:.cc=.so)

all: stdlib

stdlib: $(STDOBJ)

.cc.so: 
    mkdir -p `dirname $< | sed -e 's/stdlib/stdlib\/build/'`
    $(CXX) $< -o `dirname $< | sed -e 's/stdlib/stdlib\/build/'`/`basename $< .cc`.so $(CFLAGS) $(LDFLAGS)

I have two questions :

1 - The problem is that the make command, i really don't know why, doesn't check if a file was modified and launch the build process on ALL the files no matter what, so if i need to build only one file, i have to build them all or use the command :

make path/to/single/file.so

Is there any way to solve this?

2 - Any way to do this in a "cleaner" way without have to distribute all the build directories with sources?

Thanks

like image 219
Simone Margaritelli Avatar asked Apr 14 '10 20:04

Simone Margaritelli


People also ask

Should makefile be in src directory?

This makefile should be located in the src directory. Note that it also includes a rule for cleaning up your source and object directories if you type make clean. The . PHONY rule keeps make from doing something with a file named clean.

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 add a header file path in makefile?

Including Header file from Different Directories This can be done using -I option in makefile. Assuming that functions. h file is available in /home/tutorialspoint/header folder and rest of the files are available in /home/tutorialspoint/src/ folder, then the make file would be written as follows.

Where do you put makefiles?

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.


1 Answers

For 1) the problem is that the target of your rule (stdlib/something.so) is not exactly what the rule makes (build/something.so), so Make always thinks it must make the target. This should fix it (I am using GNUMake):

STDOBJ=$(patsubst stdlib%.cc,build%.so, $(STDSRC))

all: stdlib

stdlib: $(STDOBJ)

build/%.so: stdlib/%.cc
    mkdir -p $(dir $@)
    $(CXX) $< -o $@ $(CFLAGS) $(LDFLAGS)

For 2) I'm not sure what you mean. If you want the build directory structure you describe, this will do it.

like image 187
Beta Avatar answered Sep 28 '22 02:09

Beta