简体   繁体   English

[C++][Makefile] 没有这样的文件或目录

[英][C++][Makefile] No such file or directory

I'm learning Makefile and I have a problem.我正在学习 Makefile,但遇到了问题。

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:这是我的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.我不确定,但我认为我应该使用 -I 参数,但我不知道如何。


UPDATE:更新:

This Makefile works:这个 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 $@

But if I want add line at the end:但是如果我想在最后添加一行:

clean:
    rm -f *.o

This command doesn't clean*.o objects.此命令不清除 *.o 对象。 Why?为什么?

I don't see any usage of -I$(HEADER_DIR) in compiler flags.我在编译器标志中看不到-I$(HEADER_DIR)的任何用法。

IMPL_DIR := src
HEADER_DIR := include
BIN_DIR := bin

output: main.o filemanager.o
    g++ -std=c++0x -Wall -I$(HEADER_DIR) main.o filemanager.o -o $(BIN_DIR)/login

main.o: main.cpp
    g++ -I$(HEADER_DIR) -c main.cpp

filemanager.o: $(HEADER_DIR)/CFileManager.hpp $(IMPL_DIR)/CFileManager.cpp
    g++ -I$(HEADER_DIR) -c CFileManager.cpp 

$(BIN_DIR):
    mkdir -p $@

If you want automatic clean, add the rule before any other rule如果要自动清理,请在任何其他规则之前添加该规则

all: output clean

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM