C语言 如何从 C 中的外部 .c 文件调用函数?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21260735/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 10:39:15  来源:igfitidea点击:

How to invoke function from external .c file in C?

cfunctionexternal

提问by ProtoTyPus

My files are

我的文件是

// main.c  

#include "add.c"

int main(void) {
    int result = add(5,6);
    printf("%d\n", result);
}  

and

// add.c  

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

采纳答案by Gaurav Jain

use #include "ClasseAusiliaria.c"[Dont use angle brackets (< >) ]

使用#include "ClasseAusiliaria.c"[不要使用尖括号 (< >) ]

and I prefer save file with .hextensionin the same Directory/folder.

我更喜欢保存文件,.h扩展名在同一目录/文件夹中。

#include "ClasseAusiliaria.h"

#include "ClasseAusiliaria.h"

回答by Elliott Frisch

Change your Main.clike so

改变你的Main.c样子

#include <stdlib.h>
#include <stdio.h>
#include "ClasseAusiliaria.h"

int main(void)
{
  int risultato;
  risultato = addizione(5,6);
  printf("%d\n",risultato);
}

Create ClasseAusiliaria.hlike so

ClasseAusiliaria.h像这样创建

extern int addizione(int a, int b);

I then compiled and ran your code, I got an output of

然后我编译并运行你的代码,我得到了一个输出

11

回答by Alexandr

You must declare int add(int a, int b);(note to the semicolon) in a header file and include the file into both files. Including it into Main.c will tell compiler how the function should be called. Including into the second file will allow you to check that declaration is valid (compiler would complain if declaration and implementation were not matched).

您必须int add(int a, int b);在头文件中声明 (注意分号)并将该文件包含在两个文件中。将它包含在 Main.c 中将告诉编译器应该如何调用该函数。包含在第二个文件中将允许您检查声明是否有效(如果声明和实现不匹配,编译器会抱怨)。

Then you must compile both *.c files into one project. Details are compiler-dependent.

然后,您必须将两个 *.c 文件编译为一个项目。详细信息取决于编译器。

回答by Kraken

make a file classAusiliaria.hand in there provide your method signatures.

制作一个文件classAusiliaria.h并在其中提供您的方法签名。

Now instead of including the .c file include this .h file.

现在,不是包含 .c 文件,而是包含此 .h 文件。

回答by Precious George

There are many great contributions here, but let me add mine non the less.

这里有很多伟大的贡献,但让我继续补充我的贡献。

First thing i noticed is, you did not make any promises in the main file that you were going to create a function known as add(). This count have been done like this in the main file:

我注意到的第一件事是,您没有在主文件中做出任何承诺,即您将创建一个名为 add() 的函数。这个计数在主文件中是这样完成的:

    int add(int a, int b); 

before your main function, that way your main function would recognize the add function and try to look for its executable code. So essentially your files should be

在 main 函数之前,这样你的 main 函数会识别 add 函数并尝试寻找它的可执行代码。所以基本上你的文件应该是

Main.c

主文件

    int add(int a, int b);

    int main(void) {
        int result = add(5,6);
        printf("%d\n", result);
    }  

and // add.c

和 // add.c

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

回答by Varo

You can include the .c files, no problem with it logically, but according to the standard to hide the implementation of the function but to provide the binaries, headers and source files techniques are used, where the headers are used to define the function signatures where as the source files have the implementation. When you sell your project to outside you just ship the headers and binaries(libs and dlls) so that you hide the main logic behind your function implementation.

您可以包含 .c 文件,逻辑上没有问题,但根据标准隐藏函数的实现但提供二进制文件,使用头文件和源文件技术,其中头文件用于定义函数签名因为源文件有实现。当您将项目出售给外部时,您只需发送标头和二进制文件(libs 和 dll),以便隐藏函数实现背后的主要逻辑。

Here the problem is you have to use "" instead of <> as you are including a file which is located inside the same directory to the file where the inclusion happens. It is common to both .c and .h files

这里的问题是您必须使用 "" 而不是 <> ,因为您要包含一个位于与发生包含的文件相同的目录内的文件。.c 和 .h 文件都是通用的

回答by susheel pandey

 write main.c like this - 
 caution : while linking both main.0 and ClasseAusiliaria.o should be 
 available to linker.

 #include <stdlib.h>
 #include <stdio.h>
 extern int addizione(int a, int b)

 int main(void)
 {
     int risultato;
     risultato = addizione(5,6);
     printf("%d\n",risultato);
 }

回答by UsYer

you shouldn't include c-files in other c-files. Instead create a header file where the function is declared that you want to call. Like so: file ClasseAusiliaria.h:

您不应该在其他 c 文件中包含 c 文件。而是创建一个头文件,在其中声明要调用的函数。像这样:文件 ClasseAusiliaria.h:

int addizione(int a, int b); // this tells the compiler that there is a function defined and the linker will sort the right adress to call out.

In your Main.c file you can then include the newly created header file:

在 Main.c 文件中,您可以包含新创建的头文件:

#include <stdlib.h>
#include <stdio.h>
#include <ClasseAusiliaria.h>

int main(void)
{
    int risultato;
    risultato = addizione(5,6);
    printf("%d\n",risultato);
}