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?
CodePudding user response:
Yeah, it helps a little.
Now I have something like this:
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 $(IMPL_DIR)/CFileManager.cpp
$(BIN_DIR):
mkdir -p $@
and I receive this kind of error:
g -Iinclude -c src/CFileManager.cpp
g -std=c 0x -Wall -Iinclude main.o filemanager.o -o bin/login
g : error: filemanager.o: No such file or directory
make: *** [Makefile:6: output] Error 1
CodePudding user response:
I don't see any usage of -I$(HEADER_DIR)
in compiler flags.
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