C ++不编译,因为它说文件不存在但它在同一目录中
C++ not compiling because its say the file doesn't exist but it is in the same directory
在一个文件夹中我得到了以下文件:
book.h, book.cpp, card.h, card.cpp
这些是book.h
的内容
class book {
public:
string title;
string author;
string get_title();
string get_author();
}
book::book(string title, string author){
title = title;
author = author;
}
这是我的 book.cpp:
#include <book.h>
#include <iostream>
#include <string>
using namespace std;
string book::get_title(){
return title;
}
string book::get_author(){
return author;
}
int main(){
cout << ¨it works! \n¨
}
我尝试使用 g++ -c book.cpp 进行编译,我不断收到一条错误消息:
book.cpp:1:18: 致命错误: book.h: 没有那个文件或目录
编译终止。
当包含的文件是您的 "Include" 路径的一部分时,#include
语句使用 <>
括号,通常(但不限于)当文件来自库时,或者C++ 标准库。
""
当文件是项目本地文件和源代码本地文件时使用 ""
。
如果你把它们混在一起,你会过得很糟糕。
两者的区别
#include <book.h>
和
#include "book.h"
是第一个只在INCLUDE路径中查找文件book.h
。后者首先在源文件所在的目录中查找文件,如果未找到,则在 INCLUDE 路径中查找。
因此,您可以通过用引号替换尖括号或将您的目录添加到 INCLUDE 来解决您的问题。你可以像这样编译
g++ -I . -c book.cpp
在一个文件夹中我得到了以下文件:
book.h, book.cpp, card.h, card.cpp
这些是book.h
的内容class book {
public:
string title;
string author;
string get_title();
string get_author();
}
book::book(string title, string author){
title = title;
author = author;
}
这是我的 book.cpp:
#include <book.h>
#include <iostream>
#include <string>
using namespace std;
string book::get_title(){
return title;
}
string book::get_author(){
return author;
}
int main(){
cout << ¨it works! \n¨
}
我尝试使用 g++ -c book.cpp 进行编译,我不断收到一条错误消息:
book.cpp:1:18: 致命错误: book.h: 没有那个文件或目录 编译终止。
#include
语句使用 <>
括号,通常(但不限于)当文件来自库时,或者C++ 标准库。
""
当文件是项目本地文件和源代码本地文件时使用 ""
。
如果你把它们混在一起,你会过得很糟糕。
两者的区别
#include <book.h>
和
#include "book.h"
是第一个只在INCLUDE路径中查找文件book.h
。后者首先在源文件所在的目录中查找文件,如果未找到,则在 INCLUDE 路径中查找。
因此,您可以通过用引号替换尖括号或将您的目录添加到 INCLUDE 来解决您的问题。你可以像这样编译
g++ -I . -c book.cpp