admin 管理员组文章数量: 1086019
I have the following Makefile
for building my C++ application :
SRC_DIR := ./src
OBJ_DIR := ./obj
INC_DIR := -I./include
SRC_FILES := $(wildcard $(SRC_DIR)/*.cpp)
OBJ_FILES := $(patsubst $(SRC_DIR)/%.cpp,$(OBJ_DIR)/%.o,$(SRC_FILES))
CXXFLAGS := -Wall -g
main.exe: $(OBJ_FILES)
g++ $(LDFLAGS) -o $@ $^
$(OBJ_DIR)/%.o: $(SRC_DIR)/%.cpp
g++ $(CPPFLAGS) $(CXXFLAGS) $(INC_DIR) -c -o $@ $<
The problem is that I must manually create an obj directory each time. Otherwise make fails on a fresh build with with :
Fatal error: can't create obj/Myclass.o: No such file or directory
How could I solve this so that make can create an obj directory if it doesn't exist?
I have the following Makefile
for building my C++ application :
SRC_DIR := ./src
OBJ_DIR := ./obj
INC_DIR := -I./include
SRC_FILES := $(wildcard $(SRC_DIR)/*.cpp)
OBJ_FILES := $(patsubst $(SRC_DIR)/%.cpp,$(OBJ_DIR)/%.o,$(SRC_FILES))
CXXFLAGS := -Wall -g
main.exe: $(OBJ_FILES)
g++ $(LDFLAGS) -o $@ $^
$(OBJ_DIR)/%.o: $(SRC_DIR)/%.cpp
g++ $(CPPFLAGS) $(CXXFLAGS) $(INC_DIR) -c -o $@ $<
The problem is that I must manually create an obj directory each time. Otherwise make fails on a fresh build with with :
Fatal error: can't create obj/Myclass.o: No such file or directory
How could I solve this so that make can create an obj directory if it doesn't exist?
Share Improve this question asked Mar 27 at 10:52 Engineer999Engineer999 4,0478 gold badges42 silver badges95 bronze badges 1 |1 Answer
Reset to default 6You can write a makefile rule for that, same as for other files:
$(OBJ_DIR)/%.o: $(SRC_DIR)/%.cpp | $(OBJ_DIR)
g++ $(CPPFLAGS) $(CXXFLAGS) $(INC_DIR) -c -o $@ $<
$(OBJ_DIR):
mkdir -p $@
Putting the dependency after |
ensures that it is not part of $^
expansion, should you need it, and its timestamp does not trigger rebuilds.
本文标签: C Makefile object file doesn39t exist It needs to be manually createdStack Overflow
版权声明:本文标题:C++ Makefile object file doesn't exist. It needs to be manually created - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.roclinux.cn/p/1744095319a2532791.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
%.o
rule; before theg++
command you add a command linemkdir -p $(@D)
– MadScientist Commented Mar 27 at 13:49