I am trying to teach myself C Programming and I am using DevC++ for my IDE under Windows XP. I am a little confused on the correct way to call my own Header Files.
我正在尝试自学C编程,我在Windows XP系统下使用devc++。我对调用我自己的头文件的正确方式有点困惑。
I have my main source file called main.c and a separate file for functions called myFunctions.c which I include in main.c using 'include "myFunctions.h" with all my function prototypes residing in this Header file.
我的主源文件叫做main。和一个名为myfunction的函数的独立文件。c我主要包括在内。使用的包括“myfunction c。在这个头文件中包含所有函数原型。
myFunctions.c contains two functions one called showDate() and one called showScreen() and both functions can be called from main.c all well and good.
myfunction。c包含两个函数,一个名为showDate(),另一个名为showScreen(),两个函数都可以从main调用。c一切都很好。
My problems started when I tried to call showDate() from within showScreen() and during compilation/linking it was complaining because I did not have a prototype inside myFunctions.c for showDate().
当我试图从showScreen()中调用showDate()和编译/链接时,我的问题开始了,因为我在myfunction中没有原型。c showDate()。
What I want to know is which of the following do I need to do?
我想知道的是我需要做以下哪一个?
include "myFunctions.h" inside myFunctions.c
- 包括“myfunction。h“内部myFunctions.c
- Declare the Prototype in both myFunctions.h and myFunctions.c
- 在两个myfunction中声明原型。h和myFunctions.c
- Declare the prototype in just myFunctions.c only
- 在myfunction中声明原型。c只
All of the above seems to correct the compiler error and allow me to call the function bot from main.c and within myFunctions.c but I can not find a definitive source of which is the correct procedure.
所有这些似乎都可以纠正编译器错误,并允许我从main调用函数bot。c和myfunction之内。但是我找不到确切的来源,哪一个是正确的程序。
5 个解决方案
#1
24
Use #1 -- #include in many places.
在很多地方使用#1 - #include。
Never use #2 -- never declare anything more than once.
不要使用#2——不要声明任何东西超过一次。
Rarely use #3 -- declare something in a .c file as if you're never going to reuse it.
很少使用#3——在.c文件中声明一些东西,就好像你永远不会重用它一样。
#2
7
The header file should contain the prototypes. You then include it everywhere those prototypes are used, including the .c file that contains the function definitions.
头文件应该包含原型。然后在使用这些原型的任何地方都包含它,包括包含函数定义的.c文件。
BTW DecC++ is no longer being actively developed - you should consider switching to Code::Blocks instead.
顺便说一句,DecC++不再是积极开发的——您应该考虑切换到代码:::Blocks。
#3
3
Definitely the first option.
肯定第一个选项。
#4
2
You should choose option 1. Or order myfunctions.c so that the definition of the called function occurs before the function that calls it. By including the header in the file, you allow the compiler to catch any mismatch between the declaration and the definition.
您应该选择选项1。myfunction或秩序。这样被调用函数的定义就会出现在调用函数之前。通过在文件中包含头,编译器可以捕获声明和定义之间的任何不匹配。
#5
1
As everyone else had already said, you should use the first option. The general rule is that, function prototypes resides in .h files, and their implementations in .c files.
正如其他人已经说过的,您应该使用第一个选项。一般的规则是,函数原型驻留在.h文件中,它们的实现位于.c文件中。
#1
24
Use #1 -- #include in many places.
在很多地方使用#1 - #include。
Never use #2 -- never declare anything more than once.
不要使用#2——不要声明任何东西超过一次。
Rarely use #3 -- declare something in a .c file as if you're never going to reuse it.
很少使用#3——在.c文件中声明一些东西,就好像你永远不会重用它一样。
#2
7
The header file should contain the prototypes. You then include it everywhere those prototypes are used, including the .c file that contains the function definitions.
头文件应该包含原型。然后在使用这些原型的任何地方都包含它,包括包含函数定义的.c文件。
BTW DecC++ is no longer being actively developed - you should consider switching to Code::Blocks instead.
顺便说一句,DecC++不再是积极开发的——您应该考虑切换到代码:::Blocks。
#3
3
Definitely the first option.
肯定第一个选项。
#4
2
You should choose option 1. Or order myfunctions.c so that the definition of the called function occurs before the function that calls it. By including the header in the file, you allow the compiler to catch any mismatch between the declaration and the definition.
您应该选择选项1。myfunction或秩序。这样被调用函数的定义就会出现在调用函数之前。通过在文件中包含头,编译器可以捕获声明和定义之间的任何不匹配。
#5
1
As everyone else had already said, you should use the first option. The general rule is that, function prototypes resides in .h files, and their implementations in .c files.
正如其他人已经说过的,您应该使用第一个选项。一般的规则是,函数原型驻留在.h文件中,它们的实现位于.c文件中。