2013-06-28 29 views
0

我有一个C程序(基于链表的商店数据库,每个商店都有一个子链接列表的产品)。我把我的项目分割成.h和.c文件。这是我应该这样做的正确方法吗?

我有4个.c文件:

-> interface.c //functions for interface | #include interface.h 
-> products.c // funcs for managing product list | #include products.h 
-> shop.c // funcs for managing shop list | #include shop.h 
-> main.c // int main(); only | #include main.h 

我有4个.h文件:

-> interface.h // prototypes from interface.c | #include main.h 
-> products.h // prototypes from products.c | #include main.h 
-. shop.h // prototypes from shop.c | #include main.h 
-> main.h (I put there my global constants from #define, enum and structs from shop linked list and product sub linked list and standard libraries like stdio etc.) 

BTW。项目工作,但在main.c()Visual Studio强调每个函数。 这是我将项目分成.c和.h文件的正确方法吗?

+0

每个头文件应该是第一个包含在每个.c文件中......这将测试它们的一致性,并确保在使用它们之前定义所有(非静态)函数。 –

+0

@JumBalter,好吧,现在是否应该将main.h添加到每个.c文件?否则它不工作:/ – tomdavies

+0

确定它是正确的,但它已包含在其他头文件中。想必你使用的是守卫。如果某些东西不能正常工作,则需要更清楚地知道发生了什么问题。 –

回答

1

是的,你可以用这种方式拆分那个项目,但是在main.c文件中你需要包含每个头文件,这就是VS显示行的原因。

+0

好吧,现在适合添加main。 h到每个.c文件? 否则它不起作用 – tomdavies

+1

只需要包含正在使用的功能的头文件。 –

1

还必须将以下类型内关闭您的.h文件中的代码..

#ifndef _HEADERFILE_NAME_H_ 
#define _HEADERFILE_NAME_H_ 
..... <your code will be here> 
#endif 

+0

是的,我也是这样:) 谢谢! :) – tomdavies

+0

建议放弃领先_,即使领先_是一种普遍的做法。 C保留各种标识符供将来使用,包括以_ +'大写字母'开始的所有标识符。细节在C11 7.1.3中。 – chux

3

这里有一些不错的幻灯片约organizing C programs

至于标题以及如何使用和管理它们,你可以阅读these guidelines

Visual Studio强调您的功能可能是因为您没有在IDE中设置适当的包含路径,或者您没有设置所有必需的标题。

相关问题