簡體   English   中英

g ++找不到頭文件

[英]g++ cannot find header file

我正在從Java遷移到C ++。 似乎C ++在單獨的文件中進行類聲明很困難。 所以我需要你的幫助,

在我的main.cpp中:

#include "Sphere.h"

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

int main( void ) {
   Sphere *earth = new Sphere(sphere_start ,sphere_end);
...
..
.

在我的Sphere.h中

class Sphere
{

    public:
        Sphere(int,int);

}

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

}

這是我認為導致以下錯誤的非常基本的代碼:

main.cpp:14:20: fatal error: Sphere.h: No such file or directory
compilation terminated.

為什么?

您需要在編譯命令中添加一個可以找到頭文件的路徑。

如果您的標題位於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.

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

Sphere.h必須與包含它的每個文件位於同一目錄中,或者必須指示編譯器搜索Sphere.h所在的目錄。

您應該發布命令行,但我的猜測是您應該告訴編譯器的頭文件的路徑。 如果你正在使用linux試試這個:

g++ main.cpp shpere.cpp -I<path_to_Sphere.h> -o main

兩個潛在的錯誤:

  • Sphere.h與main.cpp位於同一目錄中嗎?
  • Sphere.h是否命名為Sphere.h而不是sphere.h?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM