Makefile:在单独的目录树中构建 [英] Makefile : Build in a separate directory tree

查看:114
本文介绍了Makefile:在单独的目录树中构建的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的项目(一种解释语言)有一个由多个文件组成的标准库,每个文件都将内置到一个.so动态库中,解释器将根据用户请求(使用import指令)加载. 每个源文件都位于代表其命名空间"的子目录中,例如:

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 :

构建过程必须创建一个"build"目录,然后在编译每个文件时都必须在"build"目录中创建其命名空间目录,例如,在编译时

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

他使用

mkdir -p build/std/io/network

Makefile片段为:

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)

我有两个问题:

1-问题是我真的不知道为什么make命令不会检查文件是否被修改,无论如何都不会在所有文件上启动构建过程,所以如果我只需要构建一个文件,我必须全部构建它们或使用命令:

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-以任何一种更清洁"的方式来执行此操作,而不必分发带有源代码的所有构建目录吗?

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

谢谢

推荐答案

对于1),问题是规则的目标(stdlib/something.so)并非规则的目标(build/something.so) ,因此Make始终认为它必须成为目标.这应该可以解决(我正在使用GNUMake):

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)

对于2)我不确定您的意思.如果您要描述的构建目录结构,就可以做到.

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

这篇关于Makefile:在单独的目录树中构建的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆