4

我正在使用 makefile 来编译 MATLAB/C 项目。MATLAB 通常可以安装到多个标准位置,具体取决于其版本,例如/Applications/MATLAB_2012b.app/Applications/MATLAB_2013a.app等。

什么是最简单的方法来查看这些路径是否存在,一个接一个,并采取第一个找到的?我至少要测试五个值。我找到了$(wildcard filename)语法,但我想知道是否有比一一写出更短的方法。


根据要求进行澄清。我可以拼凑一些有用的东西,但我确信这远不是最好/最简洁的方法。有没有更好的办法?

ifeq ($(MATLAB),)
ifneq ($(wildcard /Applications/MATLAB_2011b.app),)
    MATLAB = /Applications/MATLAB_2011b.app
endif
endif

ifeq ($(MATLAB),)
ifneq ($(wildcard /Applications/MATLAB_2012a.app),)
    MATLAB = /Applications/MATLAB_2012a.app
endif
endif

ifeq ($(MATLAB),)
ifneq ($(wildcard /Applications/MATLAB_2012b.app),)
    MATLAB = /Applications/MATLAB_2012b.app
endif
endif

ifeq ($(MATLAB),)
ifneq ($(wildcard /Applications/MATLAB_2013a.app),)
    MATLAB = /Applications/MATLAB_2013a.app
endif
endif
4

2 回答 2

1

如果存在,您可以获得第一个这样的目录

MATLAB_DIR := $(firstword $(wildcard /Applications/MATLAB_*.app))

如果不存在,变量将为空

ifeq (,$(MATLAB_DIR))
  $(error Matlab not found)
endif

如果您的路径有空格,则可以改用 shell。像这样的东西可能会起作用:

MATLAB_DIR := $(shell ls -d /Applications/MATLAB_*.app | tail -n 1)
于 2013-03-19T16:25:39.623 回答
1

为了回答您关于文件/目录名称中的空格的问题,大多数 Makefile 函数不处理文件名中的空格,并且大多数函数甚至不尊重\转义空格的方式。

$@像,$<$%函数这样的变量似乎很少wildcard将转义序列正确地处理\为空格,但大多数其他 makefile 变量和函数像firstword等。无法处理空格。

假设您在通配符中找到一个有效目录,则无法区分列表中的 2 个有效目录与路径中带有空格的单个目录。

如果您使用的是 Makefile,我建议您尽可能避免在路径中使用空格。

但是总有解决方法;)假设您处于*nix 环境中,find并且sort命令可用,这似乎可行。

BASE_DIR_NAME := .
MATLAB_DIR := $(subst $(NULL) ,\ ,$(shell find $(BASE_DIR_PATH) -mindepth 1 -maxdepth 1 -name 'MATLAB_*.app' -type d -print | sort | head -1))

default:
    @echo MATLAB_DIR="$(MATLAB_DIR)"
    ls -l $(MATLAB_DIR)

.PHONY: default

这是目录结构和 Makefile 的输出:

$ ls -l
total 16
-rw-r--r-- 1 ash users  236 Mar 20 01:15 Makefile
drwxr-xr-x 2 ash users 4096 Mar 20 01:13 MATLAB_ a.app
drwxr-xr-x 2 ash users 4096 Mar 20 01:13 MATLAB_ b.app
drwxr-xr-x 2 ash users 4096 Mar 20 01:13 MATLAB_ c.app

$ find . -type f
./Makefile
./MATLAB_ a.app/file1
./MATLAB_ b.app/file2

$ make
MATLAB_DIR=./MATLAB_\ a.app
ls -l ./MATLAB_\ a.app
total 0
-rw-r--r-- 1 ash users 0 Mar 20 01:13 file1
于 2013-03-20T08:24:08.107 回答