2012-06-25 48 views
8

我在makefile教程中發現了以下幾行代碼,但我在粗體代碼中遇到了一些問題。通配符在生成文件中意味着什麼?

在1號線,如果我寫

program_C_SRCS:=$(*.c) 

這是行不通的。所以請告訴我什麼是 通配詞在這裏做。這個詞只是特定於makefile嗎?

在教程中寫入第二行將執行測試替換。任何人都可以告訴我關於這個文字替換的一些東西嗎

請原諒我,如果我的問題是非常基本的,因爲我是新做filestuff。

link of tutorial

CC:=g++ 
program_NAME:=myprogram 
**program_C_SRCS:=$(wildcard *.c)** # 1 line 
program_CXX_SRCS:=$(wildcard *.cc) 
**program_C_OBJ:=$(program_C_SRCS:.c=.o)** # 2 line 
program_CXX_OBJ:=$(program_CXX_SRCS:.c=.o) 
program_OBJ:= $(program_C_OBJ) $(program_CXX_OBJ) 

回答

10

假設你有兩個源文件。 foo.cbar.c

program_C_SRCS:=$(wildcard *.c) # 1 line 

wildcard函數是Make語法。變量program_C_SRCS現在有(按順序也許不是)的值foo.c bar.c

program_C_OBJ:=$(program_C_SRCS:.c=.o) # 2 line 

這是一個substitution reference。它轉換文本,用另一個替換一個子字符串。變量program_C_OBJ目前擁有價值foo.o bar.o

1

make文件中使用通配符卡功能是列出所有帶有特定擴展名的源文件。例如:

program_C_SRCS:=$(*.c) // In this the variable program_C_SRCS will have all the files with ".c" extension. 

假設,如果你想.c文件轉換爲.o文件,那麼下面的語法可能是有用的:

program_C_OBJS:=$(patsubst %.c,%.o,$(wildcard *.c))