2014-04-09 66 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

相关问题