[英]Get filename from $@ in Makefile
當所有 cpp 文件都在一個目錄中時,這個 makefile 工作得很好。 創建子目錄時,它無法創建 *.o 文件,因為它缺少子目錄:
.
├── Makefile
├── README.md
├── src
│ ├── Adventurer
│ │ └── Adventurer.cpp
│ ├── Board
│ │ ├── AdventureTile.cpp
│ │ └── Board.cpp
CC := g++ # This is the main compiler
SRCDIR := src
BUILDDIR := build
TARGET := bin/forbidden-island
SRCEXT := cpp
SOURCES := $(shell find $(SRCDIR) -type f -name *.$(SRCEXT))
OBJECTS := $(patsubst $(SRCDIR)/%,$(BUILDDIR)/%,$(SOURCES:.$(SRCEXT)=.o))
CFLAGS := -g -Wall
LIB := -lsfml-graphics -lsfml-window -lsfml-system
INC := -I include
$(BUILDDIR)/%.o: $(SRCDIR)/%.$(SRCEXT)
# BUILDPATH=$($(BUILDDIR)/%.o) # missing step of creating subdirectories
# OFILE=$(basename $@)
@mkdir -p $(BUILDDIR)
@echo " $(CC) $(CFLAGS) $(INC) -c -o $@ $<"; $(CC) $(CFLAGS) $(INC) -c -o $@ $<
此代碼的示例輸出是:
g++ -g -Wall -I include -c -o build/Board/Board.o src/Board/Board.cpp
src/Board/Board.cpp: In member function ‘AdventureTile Board::getTile(int, int)’:
src/Board/Board.cpp:13:1: warning: no return statement in function returning non-void [-Wreturn-type]
}
^
Assembler messages:
Fatal error: can't create build/Board/Board.o: No such file or directory
Makefile:23: recipe for target 'build/Board/Board.o' failed
make: *** [build/Board/Board.o] Error 1
如何更改命令,使其只是 o 文件的名稱而不是完整路徑。 使用特殊變量 $@。
(basename $@)
返回build/Board/Board
但需要它是 board.o
Makefile 失敗,因為未創建子目錄。 行@mkdir -p $(BUILDDIR)
需要變成@mkdir -p $(dir $@)
來解決這個問題。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.