[c++] [makefile]没有这样的文件或目录
我正在学习makefile,我有问题。
这是我的项目结构:
root
├── include/
│ └── all .h files here
├── src/
│ └── all .c files here
├── bin/
│ └── output binary
└── Makefile
└── Main.cpp
这是我的Makefile:
IMPL_DIR := src
HEADER_DIR := include
BIN_DIR := bin
output: main.o filemanager.o
g++ -std=c++0x -Wall main.o filemanager.o -o $(BIN_DIR)/login
main.o: main.cpp
g++ -c main.cpp
filemanager.o: $(HEADER_DIR)/CFileManager.hpp $(IMPL_DIR)/CFileManager.cpp
g++ -c CFileManager.cpp
$(BIN_DIR):
mkdir -p $@
我有这样的错误:
g++ -c main.cpp
main.cpp:2:10: fatal error: CFileManager.hpp: No such file or directory
2 | #include "CFileManager.hpp"
| ^~~~~~~~~~~~~~~~~~
compilation terminated.
make: *** [Makefile:9: main.o] Error 1
有人可以帮助我解决这个错误吗?我的代码或项目结构有问题吗? 我不确定,但是我认为我应该使用-i参数,但我不知道如何使用。
更新:
此makefile有效:
IMPL_DIR := src
HEADER_DIR := include
BIN_DIR := bin
output: main.o CFileManager.o
g++ -std=c++0x -Wall -I$(HEADER_DIR) main.o CFileManager.o -o $(BIN_DIR)/login
main.o: main.cpp | $(BIN_DIR)
g++ -I$(HEADER_DIR) -c main.cpp
CFileManager.o: $(HEADER_DIR)/CFileManager.hpp $(IMPL_DIR)/CFileManager.cpp
g++ -I$(HEADER_DIR) -c $(IMPL_DIR)/CFileManager.cpp
$(BIN_DIR):
mkdir -p $@
但是,如果我想在末端添加行:
clean:
rm -f *.o
此命令不会清洁*.o对象。为什么?
I'm learning Makefile and I have a problem.
This is my project structure:
root
├── include/
│ └── all .h files here
├── src/
│ └── all .c files here
├── bin/
│ └── output binary
└── Makefile
└── Main.cpp
And this is my makefile:
IMPL_DIR := src
HEADER_DIR := include
BIN_DIR := bin
output: main.o filemanager.o
g++ -std=c++0x -Wall main.o filemanager.o -o $(BIN_DIR)/login
main.o: main.cpp
g++ -c main.cpp
filemanager.o: $(HEADER_DIR)/CFileManager.hpp $(IMPL_DIR)/CFileManager.cpp
g++ -c CFileManager.cpp
$(BIN_DIR):
mkdir -p $@
And I have such error:
g++ -c main.cpp
main.cpp:2:10: fatal error: CFileManager.hpp: No such file or directory
2 | #include "CFileManager.hpp"
| ^~~~~~~~~~~~~~~~~~
compilation terminated.
make: *** [Makefile:9: main.o] Error 1
Anyone can help me with this error? Is it problem with my code or project structure?
I'm not sure but I think that I should use -I parameter, but I don't know how.
UPDATE:
This Makefile works:
IMPL_DIR := src
HEADER_DIR := include
BIN_DIR := bin
output: main.o CFileManager.o
g++ -std=c++0x -Wall -I$(HEADER_DIR) main.o CFileManager.o -o $(BIN_DIR)/login
main.o: main.cpp | $(BIN_DIR)
g++ -I$(HEADER_DIR) -c main.cpp
CFileManager.o: $(HEADER_DIR)/CFileManager.hpp $(IMPL_DIR)/CFileManager.cpp
g++ -I$(HEADER_DIR) -c $(IMPL_DIR)/CFileManager.cpp
$(BIN_DIR):
mkdir -p $@
But if I want add line at the end:
clean:
rm -f *.o
This command doesn't clean*.o objects. Why?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我没有在编译器标志中看到
-i $(header_dir)
的任何用法。如果要自动清洁,请在任何其他规则之前添加规则
I don't see any usage of
-I$(HEADER_DIR)
in compiler flags.If you want automatic clean, add the rule before any other rule