I've been learning c++ and encountered the following question: I have a directory structure like:
- current directory
- Makefile
- include
- header.h
- src
- main.cpp
my header.h :
#include <iostream>
using namespace std;
void print_hello();
my main.cpp:
#include "header.h"
int main(int argc, char const *argv[])
{
print_hello();
return 0;
}
void print_hello()
{
cout<<"hello world"<<endl;
}
my Makefile:
CC = g++
OBJ = main.o
HEADER = include/header.h
CFLAGS = -c -Wall
hello: $(OBJ)
$(CC) $(OBJ) -o $@
main.o: src/main.cpp $(HEADER)
$(CC) $(CFLAGS) $< -o $@
clean:
rm -rf *o hello
And the output of make is:
g++ -c -Wall src/main.cpp -o main.o src/main.cpp:1:20: fatal error: header.h: No such file or directory compilation terminated. Makefile:10: recipe for target 'main.o' failed make: *** [main.o] Error 1
What mistakes I have made in here. It's frustrating. Really appreciate any advice!
-Iinclude/to the compiler flags. - πάντα ῥεῖ