头文件问题 [英] header file question
问题描述
我有2个.c文件的小应用程序。一个文件包含main
,另一个文件包含main中使用的函数。我用这样的gcc编译这个
:gcc -c * c&& gcc -o app * o。但我想以另一种方式在
中这样做。我想使用头文件我已经google了但是没有找到
任何有用的东西都希望我的标题中应该有原型
文件可以有人帮助我吗?
I''ve got this small app , from 2 .c files. One file contains the main
and the other file contains functions used in the main. I compile this
using gcc like this: gcc -c *c && gcc -o app *o. But I want to do it in
an other way. I want to use header files I''ve googled but did not find
anything usefull expect that there should be prototypes in my header
file can anyone help me ?
推荐答案
2003年11月15日星期六11:52:28 +0000,Geiregat Jonas写道:
On Sat, 15 Nov 2003 11:52:28 +0000, Geiregat Jonas wrote:
我从2个.c文件中得到了这个小应用程序。一个文件包含main
,另一个文件包含main中使用的函数。我用这样的gcc编译这个
:gcc -c * c&& gcc -o app * o。但是我想用另一种方式来做。我想使用标题文件我已经google了但是没有发现
任何有用的东西都希望我的标题中应该有原型
文件可以有人帮助我吗?
I''ve got this small app , from 2 .c files. One file contains the main
and the other file contains functions used in the main. I compile this
using gcc like this: gcc -c *c && gcc -o app *o. But I want to do it in
an other way. I want to use header files I''ve googled but did not find
anything usefull expect that there should be prototypes in my header
file can anyone help me ?
目前尚不清楚你在说什么。
试试这个:
gcc -o app * .c
It''s not clear what you are talking about.
Try this:
gcc -o app *.c
Geiregat Jonas写道:
Geiregat Jonas wrote:
我有这个小应用程序,来自2个.c文件。一个文件包含main
,另一个文件包含main中使用的函数。我用这样的gcc编译这个
:gcc -c * c&& gcc -o app * o。但是我想用另一种方式来做。我想使用标题文件我已经google了但是没有发现
任何有用的东西都希望我的标题中应该有原型
文件可以有人帮助我吗?
I''ve got this small app , from 2 .c files. One file contains the main
and the other file contains functions used in the main. I compile this
using gcc like this: gcc -c *c && gcc -o app *o. But I want to do it in
an other way. I want to use header files I''ve googled but did not find
anything usefull expect that there should be prototypes in my header
file can anyone help me ?
我会做一个小例子。
main.c
------
#include< stdio.h>
#include" calc.h"
int main()
{
int x;
printf("输入一个整数:");
scanf("%d",& x);
printf("%d的平方是%ld \ n",x,square(x));
return(0);
}
calc.h
------
#ifndef CALC_H
#define CALC_H
长方形(整数);
#endif
calc.c
------
#include" calc.h"
长方形(整数x)
{
返回((长)x * x);
}
---- -----
注意
n包含calc.h时的引号,因为它与源文件在同一个
目录中
,如果你从未使用过预处理程序指令,#ifndef
CALC_H可能看起来很混乱。但是,您希望在头文件中使用它。
它可以防止该头文件的多个包含。只需将CALC_H
更改为任何内容..我认为只要它与下面的
#define语句相同就很重要。
HTH,
Aaron
I will do a small example.
main.c
------
#include <stdio.h>
#include "calc.h"
int main()
{
int x;
printf("Enter an integer: ");
scanf("%d", &x);
printf("The square of %d is %ld\n", x, square(x));
return (0);
}
calc.h
------
#ifndef CALC_H
#define CALC_H
long square(int);
#endif
calc.c
------
#include "calc.h"
long square(int x)
{
return ((long) x * x);
}
---------
NOTES
notice the quotes when including calc.h because it is in the same
directory as the source file
also, if you''ve never used preprocessor directives, the "#ifndef
CALC_H" may look confusing. You want this in the header file, though.
It prevents multiple inclusions of that header file. Just change CALC_H
to whatever.. I dont think it matters as long as its the same as the
#define statement below it.
HTH,
Aaron
查看此链接中的文章。
这是一个很好的关于C编程的简单教程,还有详细的
说明如何使用gcc
编译器在库链接上创建库。 (我已经完成了它并且有效)。
http://computer.howstuffworks.com/c.htm
tuchka
Look at an article at this link.
It is good simple tutorial on C programming and also there detailed
instructions how to make library at ''libraries'' link using gcc
compiler. (I''ve done that and it worked).
http://computer.howstuffworks.com/c.htm
tuchka
这篇关于头文件问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!