C++ g++ 找不到头文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8736106/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
g++ cannot find header file
提问by Test Test
I am migrating from Java to C++. It seems that C++ makes classes declaration in separate files, difficult. So I need your help,
我正在从 Java 迁移到 C++。似乎 C++ 使类声明在单独的文件中很困难。所以我需要你的帮助
in my main.cpp:
在我的 main.cpp 中:
#include "Sphere.h"
using namespace std;
.....
...
..
int main( void ) {
Sphere *earth = new Sphere(sphere_start ,sphere_end);
...
..
.
in my Sphere.h
在我的 Sphere.h
class Sphere
{
public:
Sphere(int,int);
}
and in my Sphere.cpp
在我的 Sphere.cpp 中
#include "Sphere.h"
using namespace std;
int sphere_start, sphere_end;
Sphere::Sphere (int a, int b)
{
sphere_start = a;
sphere_end = b;
}
void Sphere::render(int i)
{
....
..
.
}
This is the very basic code that I think causes the following error:
这是我认为会导致以下错误的非常基本的代码:
main.cpp:14:20: fatal error: Sphere.h: No such file or directory
compilation terminated.
why?
为什么?
回答by stefanB
You need to add to your compile command a path to where the header files can be found.
您需要在编译命令中添加一个可以找到头文件的路径。
If your header is in the headers
directory add -Iheaders
:
如果您的标题在headers
目录中,请添加-Iheaders
:
g++ -o main.o -c -Iheaders main.cpp
g++ -o sphere.o -c -Iheaders sphere.cpp
g++ -o app main.o sphere.o -L.
Or whatever your files are ...
或者无论你的文件是什么......
回答by 01d55
Sphere.h must either be in the same directory as each file that includes it, or the compiler must be directed to search the directory in which Sphere.h is located.
Sphere.h 必须与包含它的每个文件位于同一目录中,或者必须指示编译器搜索 Sphere.h 所在的目录。
回答by Flamínio Maranh?o
You should post your command line, but my guess is that you should tell the path to the header files to the compiler. If you're using linux try this:
您应该发布您的命令行,但我的猜测是您应该将头文件的路径告诉编译器。如果您使用的是 linux,请尝试以下操作:
g++ main.cpp shpere.cpp -I<path_to_Sphere.h> -o main
回答by Laserallan
Two potential errors:
两个潜在的错误:
- Is Sphere.h in the same directory as main.cpp?
- Is Sphere.h named Sphere.h and not sphere.h?
- Sphere.h 是否与 main.cpp 位于同一目录中?
- Sphere.h 是否命名为 Sphere.h 而不是 sphere.h?