从 Makefile 中的 $@ 获取文件名
Get filename from $@ in Makefile
当所有 cpp 文件都在一个目录中时,此生成文件效果很好。 创建子目录时,它无法创建 *.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
但需要它来登机。
====
=========== 解决方案 =================生成文件失败,因为未创建子目录。 这条线@mkdir -p $(BUILDDIR)
需要变得@mkdir -p $(dir $@)
来解决这个问题。
我真的不明白你的意思是如何更改命令,使其只是 o 文件的名称......哪个命令?
配方创建一个不完全是$@
值的文件是不正确的。 这就是 make 期望您创建的内容,如果您的配方创建其他文件,则无法正常工作。
无论如何,GNU支持的功能都可以在这里找到。 你想要的那个是$(notdir $@)
. 但是,您甚至不需要它,因为目录($(@D)
(和文件($(@F)
(的自动变量有变体。
相关文章:
- 从 Makefile 中的 $@ 获取文件名
- 有没有一种简单的方法可以从 c++ 中的路径获取文件名?
- 从 SetClipboardData 挂钩获取文件名
- 是否有一种便携式/标准的方法可以在堆栈跟踪中获取文件名和亚麻布
- 如何从文件夹中获取文件名,并将它们添加到QT中的孩子中
- 从 Windows 上的 tmpfile() 获取文件名
- 是否可以从FSEvent id获取文件名
- 如何在单击mfc代码中的关系文件时获取文件名
- 如何在Qt中获取文件名
- 是否有一个WinAPI可以从带有可选空格和其他参数的命令行获取文件名
- 从控制台应用中的命令行参数获取文件名C++
- 从流中获取文件名(或路径)
- 如何在下载文件之前获取文件名
- 在调用函数的任何位置获取文件名和函数名
- 在c++ boost库中使用directory_iterator列出和获取文件名的问题
- 从路径中获取文件名
- gtkmm 和 FileChooserButton 无法获取文件名
- 如何从http请求体字符串获取文件名
- 如何在GNU Makefile中只获取文件名而不扩展名
- 如何从argv中获取文件名并将其与ifstream一起使用/将其更改为ofstream的txt