目录的可移植makefile创建 [英] Portable makefile creation of directories

查看:70
本文介绍了目录的可移植makefile创建的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望通过制作一个相当通用的makefile来节省下更多的精力,该makefile将为我提供相对简单的C ++项目,并且对makefile的修改最少.

I'm looking to save myself some effort further down the line by making a fairly generic makefile that will put together relatively simple C++ projects for me with minimal modifications required to the makefile.

到目前为止,我已经掌握了它,因此它将使用同一目录和指定子目录中的所有.cpp文件,将所有这些文件放置在obj子目录的匹配结构中,并将结果文件放置在另一个子目录中bin.我几乎想要的.

So far I've got it so it will use all .cpp files in the same directory and specified child directories, place all these within a matching structure in a obj subdir and place the resulting file in another subdir called bin. Pretty much what I want.

但是,尝试获取它,以便在不存在所需的obj和bin目录时创建它们,这为跨平台工作提供了尴尬-特别是,我仅在Windows 7和Windows XP中进行测试. Ubuntu(记不清版本)了,我不能同时使用它.

However, trying to get it so that the required obj and bin directories is created if they don't exist is providing awkward to get working cross-platform - specifically, I'm just testing with Windows 7 & Ubuntu (can't remember version), and I can't get it to work on both at the same time.

Windows错误地读取了mkdir -p dir并创建了一个-p目录,显然这两个平台分别将\/用作路径分隔符-使用错误的目录时会出错.

Windows misreads mkdir -p dir and creates a -p directory and obviously the two platforms use \ and / respectively for the path separator - and I get errors when using the wrong one.

以下是与makefile相关的一些选定部分:

Here is a few selected portions of the makefile that are relevant:

# Manually edited directories (in this example with forward slashes)
SRC_DIR = src src/subdir1 src/subdir2

# Automagic object directories + the "fixed" bin directory
OBJ_DIR = obj $(addprefix obj/,$(SRC_DIR))
BIN_DIR = bin

# Example build target
debug: checkdirs $(BIN)

# At actual directory creation
checkdirs: $(BIN_DIR) $(OBJ_DIR)
$(BIN_DIR):
    @mkdir $@

$(OBJ_DIR):
    @mkdir -p $@

这是我在过去一周左右的时间里根据我一直在阅读的内容(主要是在StackOverflow上)整理而成的,因此,如果碰巧是我正在遵循一些可怕的不良习惯或类似性质的内容请让我知道.

This has been put together by me over the last week or so from things I've been reading (mostly on Stack Overflow), so if it happens to be I'm following some horrible bad practice or anything of that nature please let me know.

简而言之的问题:

是否有一种简单的方法可以使此目录创建在单个Makefile中起作用,并提供尽可能多的可移植性?

Is there a simple way to get this directory creation to work from a single makefile in a way that provides as much portability as possible?

推荐答案

Windows mkdir始终会执行Unix mkdir在打开-p时所做的工作.您可以使用$(subst)处理反斜杠问题.因此,在Windows上,您需要这样做:

Windows mkdir always does what Unix mkdir does with the -p switch on. And you can deal with the backslash problem with $(subst). So, on Windows, you want this:

$(BIN_DIR) $(OBJ_DIR):
        mkdir $(subst /,\\,$@)

,在Unix上,您需要这样:

and on Unix you want this:

$(BIN_DIR) $(OBJ_DIR):
        mkdir -p -- $@

在makefile中执行这些操作之间的选择是不切实际的.这就是Autoconf的目的.

Choosing between these is not practical to do within a makefile. This is what Autoconf is for.

作为旁注,切勿在makefile中使用@command功能.有一天,您需要在无法直接访问的机器上调试构建过程,这一天,您会后悔的.

As a side note, never, ever use the @command feature in your makefiles. There will come a day when you need to debug your build process on a machine you do not have direct access to, and on that day, you will regret it.

这篇关于目录的可移植makefile创建的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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