2016-04-12 57 views
0

我使用在線教程製作了測試makefile。它的工作原理,但我也想讓我所有的.o文件轉到特定的子文件夾。有沒有辦法使用makefile來做到這一點?這是我到目前爲止。在MakeFile中設置選項將目標文件發送到特定文件夾

CC=gcC# specifies the compiler to use. 

CFLAGS=-I. # specifies to look in the current directory. 

DEPS = path_tools.h # DEPS stores each .h file to be added. 

OBJ = checkpath.o path_tools.o # OBJ stores each object file to compile. 

%.o: %.c $(DEPS) 

     $(CC) -c -o [email protected] $< $(CFLAGS) 

checkpath.exe: $(OBJ) 

     gcc -o [email protected] $^ $(CFLAGS) 
+0

你知道你現在的makefile有什麼功能嗎?它是如何做到的? –

回答

1

對於GNU,你可以使用這個Makefile。

 

    ODIR:=obj 
    CC:=gcc 
    CFLAGS:=-I. 
    DEPS:=path_tools.h 
    OBJ_:= checkpath.o path_tools.o 
    OBJ:=$(addprefix $(ODIR)/, $(OBJ_)) 
    PROG=checkpath.exe 
    all:$(PROG) 
    $(OBJ): $(DEPS) 
    $(ODIR)/%.o: %.c 
     $(CC) -c -o [email protected] $< $(CFLAGS) 
    $(PROG): $(OBJ) 
     $(CC) -o [email protected] $^ $(CFLAGS) 
    clean: 
     rm -f $(OBJ) $(PROG) 

+0

謝謝。我無法得到這個工作:matt @ matt-VirtualBox:〜/ code $ make -f makefile_Example_new make:***目標'path_tools.h'沒有規則,需要'obj/checkpath.o' 。停止。 – netcat

+0

謝謝。這工作完美。我有錯誤的子文件夾。 – netcat

1

你可以通過你的文件夾的路徑爲Makefile創建並把結果在 要傳遞參數的Makefile:

化妝DPATH =您的路徑

要在生成文件使用:

$(DPATH)

創建此路徑,並把它添加到您的* .o文件的頭作爲路徑。

相關問題