2014-04-09 64 views
2

因此,我的老師希望我的項目的函數原型和typedef存儲在單獨的.c文件中。
所以main.c需要做的事情,another.c具有protypes和typedefs,而另一個.h具有聲明。如何在代碼塊中包含另一個c文件

我該怎麼做?我使用GNU GCC編譯器,因爲它似乎是一個編譯器特定的問題。我正在嘗試gcc another.c,但編譯器無法識別gcc,所以我認爲我做錯了。

我感覺如此密集的....我有整個項目的工作很好,但是這是應該的一切是在another.c在another.h ....

感謝

+0

你的意思是'#include'?聽起來你應該在課堂上多聽! – John3136

+0

她從來沒有在課堂上講過,實驗室的TA拒絕幫助我,因爲我不想學習如何在Linux中編寫代碼。我試過#include another.c沒有工作,我嘗試了gcc another.c –

+0

那麼爲什麼你用「so」開始語句?那麼你爲什麼不把句子的第一個字母和「我」的大寫?所以請至少和潛在的僱主一樣對待我們。 –

回答

4

的main.c中會是這個樣子:

// bring in the prototypes and typedefs defined in 'another' 
#include "another.h" 

int main(int argc, char *argv[]) 
{ 
    int some_value = 1; 

    // call a function that is implemented in 'another' 
    some_another_function(some_value); 
    some_another_function1(some_value); 
    return 1;   
} 

的another.h應包含another.c文件中找到的類型定義和功能函數原型:

// add your typdefs here 

// all the prototypes of public functions found in another.c file 
some_another_function(int some_value); 
some_another_function1(int some_value); 

的another.c應包含another.h找到的所有函數原型的功能實現:

// the implementation of the some_another_function 
void some_another_function(int some_value) 
{ 
    //.... 
} 

// the implementation of the some_another_function1 
void some_another_function1(int some_value) 
{ 
    //.... 
} 
+2

非常感謝,我想我只是把所有東西都倒退了。 –

+1

作爲一般規則,請記住c文件應包含運行的代碼並且頭文件包含該代碼的定義。因此,應該在c文件中找到運行的代碼(即可以放置斷點的代碼),而不是h文件(大多數情況下)。你也包含h文件,並且你編譯和鏈接c文件。 – jussij

相關問題