从Makefile中的$ @获取文件名

问题描述 投票:0回答:1

当所有cpp文件都位于一个目录中时,此makefile效果很好。创建子目录后,由于缺少子目录,因此无法创建* .o文件:

目录结构

.
├── Makefile
├── README.md
├── src
│   ├── Adventurer
│   │   └── Adventurer.cpp
│   ├── Board
│   │   ├── AdventureTile.cpp
│   │   └── Board.cpp

Makefile

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 $@)才能解决此问题。

当所有cpp文件都位于一个目录中时,此makefile效果很好。创建子目录后,它无法创建* .o文件,因为它缺少子目录:Directory Structure。 ├──...

c++ bash makefile
1个回答
0
投票

我不太明白您的意思。如何更改命令,使其仅是o文件的名称

© www.soinside.com 2019 - 2024. All rights reserved.