我一直在学习c ++并遇到了以下问题:我有一个目录结构,如:
- current directory
- Makefile
- include
- header.h
- src
- main.cpp
Run Code Online (Sandbox Code Playgroud)
我的header.h:
#include <iostream>
using namespace std;
void print_hello();
Run Code Online (Sandbox Code Playgroud)
我的main.cpp:
#include "header.h"
int main(int argc, char const *argv[])
{
print_hello();
return 0;
}
void print_hello()
{
cout<<"hello world"<<endl;
}
Run Code Online (Sandbox Code Playgroud)
我的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
Run Code Online (Sandbox Code Playgroud)
而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
我在这里犯了什么错误.这令人沮丧.真的很感激任何建议!
Lig*_*ica 10
你告诉Makefile include/header.h
必须存在,你告诉C++源文件它需要header.h
...但你没有告诉编译器这些头文件在哪里(即在"include"目录中).
做这个:
CFLAGS = -c -Wall -Iinclude
Run Code Online (Sandbox Code Playgroud)
您可以-I
在命令行中添加一个选项来告诉编译器在那里查找头文件。如果目录中有头文件include/
,那么此命令应该适合您。
gcc -Iinclude/
Run Code Online (Sandbox Code Playgroud)
由于您正在使用,因此您可以在 makefile 的宏中makefile
包含此选项。CFLAGS
CFLAGS = -Iinclude/ -c -Wall
Run Code Online (Sandbox Code Playgroud)
或者
您可以使用 包含头文件#include "../include/header.h"
。
归档时间: |
|
查看次数: |
32790 次 |
最近记录: |