我一直在学习c并遇到了以下问题:我的目录结构如下:
- 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
而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
我在这里犯了什么错误 . 这令人沮丧 . 真的很感激任何建议!
3 回答
您可以在命令行中添加
-I
选项,以告诉编译器查找头文件 . 如果include/
目录中有头文件,则此命令应该适合您 .由于您使用的是
makefile
,因此可以在makefile中的CFLAGS
宏中包含此选项 .要么
您可以使用
#include "../include/header.h"
包含头文件 .你告诉Makefile必须存在
include/header.h
,你告诉C源文件它需要header.h
...但是你没有告诉编译器这些头文件在哪里(即在"include"目录中) .做这个:
也许更改您的包含行:
假设你的 Headers 存在 - 我正在从你的makefile做出这个假设