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

0 c

我在调用我在 Linux 的 Pico 编辑器中编写的外部函数时遇到问题。程序应该调用外部函数,它们都是用 C 编写的。

#include????

void calcTax()
float calcfed()
float calcssi()
Run Code Online (Sandbox Code Playgroud)

// 存根程序

#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()
{
}
Run Code Online (Sandbox Code Playgroud)

我正在使用 gcc 进行编译,但它不会编译。两个文件位于同一目录中并且具有 .c 扩展名

我是一名新学生,所以请耐心等待。

pat*_*pat 5

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

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
Run Code Online (Sandbox Code Playgroud)

请注意,extern函数不需要,只需要全局数据。另外,由于该函数没有参数,因此您应该放入void参数列表,并且在原型末尾还需要一个分号。

接下来,我们可以在实现该函数的.c文件中包含这个头文件:

calctax.c

#include "calctax.h"

void calctaxes(void)
{
    /* implementation */
}
Run Code Online (Sandbox Code Playgroud)

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

主程序

#include "calctax.h"

int main(int argc, char **argc)
{
    calctax();
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

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

% gcc -o main main.c calctax.c
Run Code Online (Sandbox Code Playgroud)