从另一个文件中调用一个函数...... [英] Callling a function from another file...

查看:83
本文介绍了从另一个文件中调用一个函数......的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我有一个C文件(add.c),其中我有一个名为add.now的函数

i想从另一个调用相同的add函数file sub.c。可以任意1

告诉怎么做......

提前致谢

马克

解决方案

9月24日晚上11点01分,sagar< mvksa ... @ gmail.comwrote:




我有一个C文件(add.c),其中我有一个名为add.now的函数

i想要调用来自另一个文件sub.c的相同添加功能。可以任意1

告诉怎么做...



请不要在这里使用傻txt-speak。


确保add有外部链接,在sub.c的

开头放一个原型然后去。


>

提前致谢

Mark


2007年9月24日22:01,sa gar写道:




我有一个C文件(add.c),其中我有一个名为add.now的函数

i想从另一个文件sub.c调用相同的add函数。可以任意1

告诉怎么做...



我会这样做:


/ * add.c * /


add(a,b)

int a; int b;

{

返回(a + b);

}

/ * sub.c * /


sub(a,b)

int a; int b;

{

int add();

b = ~b;

return(add(a ,++ b));

}


9月24日下午5:01,sagar< mvksa .. 。@ gmail.comwrote:




我有一个C文件(add.c),其中我有一个名为add的函数.now

i想从另一个文件sub.c调用相同的add函数。可以任意1

告诉怎么做...


提前致谢

马克



这样做的规范方法是创建一个包含
的头文件
a声明原型语法中的函数,#include $

定义函数的文件(add.c)和

调用的文件它(sub.c)。


/ *

* add.h

* /

#ifndef ADD_H

#define ADD_H

extern int add(int a,int b); / *我假设添加内容* /

/ *并返回一个int * /


#endif

/ *

* add.c

* /

#include" add.h"


int add(int a,int b)

{

/ *做一些有趣的事情并返回一个值* /

}


/ *

* sub.c

* /

#include" ; add.h"


int sub(int a,int b)

{

int x;

...

x =添加(a,b);

...

返回x;

}


现在,您需要以这样的方式编译和链接,即两个文件都内置在同一个可执行文件中。我不知道你是什么环境

使用,但这里有一个使用gcc的简单例子:


gcc -o test sub.c add .c


Hi,
I have a C file(add.c) in which i have a function called add.now
i want to call the same add function from another file sub.c .Can any1
tell how to do that...

Thanks in advance
Mark

解决方案

On Sep 24, 11:01 pm, sagar <mvksa...@gmail.comwrote:

Hi,
I have a C file(add.c) in which i have a function called add.now
i want to call the same add function from another file sub.c .Can any1
tell how to do that...

Please don''t use silly txt-speak here.

Make sure add has external linkage, put a prototype for it at the
start of sub.c and away to go.

>
Thanks in advance
Mark


On 24 Sep 2007 at 22:01, sagar wrote:

Hi,
I have a C file(add.c) in which i have a function called add.now
i want to call the same add function from another file sub.c .Can any1
tell how to do that...

I would do this:

/* add.c */

add(a, b)
int a; int b;
{
return (a+b);
}
/* sub.c */

sub(a, b)
int a; int b;
{
int add();
b=~b;
return ( add(a, ++b) );
}


On Sep 24, 5:01 pm, sagar <mvksa...@gmail.comwrote:

Hi,
I have a C file(add.c) in which i have a function called add.now
i want to call the same add function from another file sub.c .Can any1
tell how to do that...

Thanks in advance
Mark

The canonical way to do this is to create a header file that contains
a declaration for the function in prototype syntax, and #include it in
both the file that defines the function (add.c) and the file that
calls it (sub.c).

/*
* add.h
*/
#ifndef ADD_H
#define ADD_H

extern int add(int a, int b); /* I''m assuming that add takes ints */
/* and returns an int */

#endif

/*
* add.c
*/
#include "add.h"

int add(int a, int b)
{
/* do something interesting and return a value */
}

/*
* sub.c
*/
#include "add.h"

int sub(int a, int b)
{
int x;
...
x = add(a,b);
...
return x;
}

Now, you need to compile and link in such a way that both files are
built into the same executable. I don''t know what environment you are
using, but here''s a quick example using gcc:

gcc -o test sub.c add.c


这篇关于从另一个文件中调用一个函数......的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆