Makefile :在单独的目录树中构建

发布于 2024-08-28 17:41:21 字数 1806 浏览 4 评论 0原文

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

库树

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

std/io/network/tcp.cc

他运行 mkdir 命令,

mkdir -p build/std/io/network

Makefile 片段是:

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

make path/to/single/file.so

有什么方法可以解决这个问题吗?

2 - 有什么方法可以以“更干净”的方式做到这一点,而不必分发所有带有源代码的构建目录?

谢谢

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 
lt; | sed -e 's/stdlib/stdlib\/build/'`
    $(CXX) 
lt; -o `dirname 
lt; | sed -e 's/stdlib/stdlib\/build/'`/`basename 
lt; .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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

望笑 2024-09-04 17:41:21

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

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

all: stdlib

stdlib: $(STDOBJ)

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

对于 2)我不确定你的意思。如果您想要您描述的构建目录结构,这就可以了。

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) 
lt; -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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文