g ++找不到头文件

Tes*_*est 2 c++

我正在从Java迁移到C++.似乎C++在单独的文件中进行类声明很困难.所以我需要你的帮助,

在我的main.cpp中:

#include "Sphere.h"

using namespace std;
.....
...
..

int main( void ) {
   Sphere *earth = new Sphere(sphere_start ,sphere_end);
...
..
.
Run Code Online (Sandbox Code Playgroud)

在我的Sphere.h中

class Sphere
{

    public:
        Sphere(int,int);

}
Run Code Online (Sandbox Code Playgroud)

在我的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) 
{
   ....
   ..
   .

}
Run Code Online (Sandbox Code Playgroud)

这是我认为导致以下错误的非常基本的代码:

main.cpp:14:20: fatal error: Sphere.h: No such file or directory
compilation terminated.
Run Code Online (Sandbox Code Playgroud)

为什么?

ste*_*anB 5

您需要在编译命令中添加一个可以找到头文件的路径.

如果您的标题位于headers目录add中-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.
Run Code Online (Sandbox Code Playgroud)

或者你的文件是什么......