繁体   English   中英

从Linux环境中用C编写的另一个文件中调用用C编写的外部函数

[英]Call external function written in C from another file written in C in a Linux enviroment

我在调用在Linux中的Pico编辑器中编写的外部函数时遇到麻烦。 该程序应调用一个外部函数,并且它们都用C编写。

#include????

void calcTax()
float calcfed()
float calcssi()

//存根程序

#include"FILE2"???or do I use"./FILE2"// not sure which to use here or what        extension     the file should have. .c or .h and if it is .h do I simply change the file     name to file.h?
#include<stdio.h>
#define ADDR(var) &var

extern void calctaxes()
int main()
{
}

我正在使用gcc进行编译,但无法编译。 这两个文件都在同一目录中,并具有.c扩展名

我是新来的学生,请多多包涵。

通常,当您希望一个编译单元中的函数调用另一编译单元中的函数时,应创建一个包含函数原型的头文件。 头文件可以包含在两个编译单元中,以确保双方都同意该接口。

calctax.h

#ifndef calctax_h_included
#define calctax_h_included

void calctaxes(void);
/* any other related prototypes we want to include in this header */

#endif

请注意,在函数上不需要extern ,只需要全局数据。 另外,由于该函数不接受任何参数,因此应在参数列表中放入void ,并且在原型末尾还需要使用分号。

接下来,我们可以将此头文件包含在实现该功能的.c文件中:

calctax.c

#include "calctax.h"

void calctaxes(void)
{
    /* implementation */
}

最后,我们在调用该函数的主.c文件中包含相同的头文件:

main.c中

#include "calctax.h"

int main(int argc, char **argc)
{
    calctax();
    return 0;
}

您可以将它们编译并链接在一起

% gcc -o main main.c calctax.c

通常,您不希望包含.c实现文件。 通常要做的是将两个源文件构建到对象中:

cc -o file1.o file1.c
cc -o file2.o file2.c

然后将它们链接到可执行文件中:

cc -o example file1.o file2.o

要允许在两个.c文件之间调用函数,请创建一个标头( .h )文件,该文件具有函数声明,可用于您有兴趣共享的所有内容。 对您来说,可能类似于header.h ,其中包含:

void calcTax(void);
float calcfed(void);
float calcssi(void);

然后,从实现文件中仅#include "header.h" ,您就需要了解这些功能。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM