【发布时间】:2016-11-04 20:22:45
【问题描述】:
我一直在学习c++,遇到以下问题: 我有一个像这样的目录结构:
- current directory
- Makefile
- include
- header.h
- src
- main.cpp
我的 header.h :
#include <iostream>
using namespace std;
void print_hello();
我的 main.cpp:
#include "header.h"
int main(int argc, char const *argv[])
{
print_hello();
return 0;
}
void print_hello()
{
cout<<"hello world"<<endl;
}
我的 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
而make的输出是:
g++ -c -Wall src/main.cpp -o main.o src/main.cpp:1:20:致命错误:header.h:没有这样的文件或目录 编译终止。 Makefile:10:目标“main.o”的配方失败 make: *** [main.o] 错误 1
我在这里犯了什么错误。这令人沮丧。非常感谢任何建议!
【问题讨论】:
-
将
-Iinclude/添加到编译器标志。 -
@πάνταῥεῖ 答案在答案部分。