如何在一个文件中定义C函数,然后从另一个文件中调用它?

时间:2021-05-06 19:48:34

Say I define a function in the file func1.c, and I want to call it from the file call.c, how would I accomplish this? Thanks in advance!

假设我在文件func1.c中定义了一个函数,我想从call.c文件中调用它,我该怎么做到这一点?提前致谢!

2 个解决方案

#1


30  

You would put a declaration for the function in the file func1.h, and add #include "func1.h" in call.c. Then you would compile or link func1.c and call.c together (details depend on which C system).

你可以在func1.h文件中放入函数的声明,并在call.c中添加#include“func1.h”。然后你将编译或链接func1.c和call.c一起(详细信息取决于哪个C系统)。

#2


12  

Use a Forward Declaration

使用远期声明

For example:

例如:

typedef struct
{
    int SomeMemberValue;
    char* SomeOtherMemberValue;
} SomeStruct;

int SomeReferencedFunction(int someValue, SomeStruct someStructValue);

int SomeFunction()
{
   SomeStruct s;
   s.SomeMemberValue = 12;
   s.SomeOtherMemberValue = "test string";

   return SomeReferencedFunction(5, s) > 12;
}

There is a feature that allows you to reuse these forward declarations called Header Files. Simply take the forward declarations, place them in the header file, then use #include to add them to each C source file you reference the forward declarations in.

有一个功能允许您重用这些名为Header Files的前向声明。只需获取前向声明,将它们放在头文件中,然后使用#include将它们添加到您引用前向声明的每个C源文件中。

/* SomeFunction.c */

#include "SomeReferencedFunction.h"

int SomeFunction()
{
   SomeStruct s;
   s.SomeMemberValue = 12;
   s.SomeOtherMemberValue = "test string";

   return SomeReferencedFunction(5, s) > 12;
}

/* SomeReferencedFunction.h */

typedef SomeStruct
{
    int SomeMemberValue;
    char* SomeOtherMemberValue;
} SomeStruct;

int SomeReferencedFunction(int someValue, SomeStruct someStructValue);

/* SomeReferencedFunction.c */

/* Need to include SomeReferencedFunction.h, so we have the definition for SomeStruct */
#include "SomeReferencedFunction.h"

int SomeReferencedFunction(int someValue, SomeStruct someStructValue)
{
    if(someStructValue.SomeOtherMemberValue == NULL)
        return 0;

    return someValue * 12 + someStructValue.SomeMemberValue;
}

Of course, to be able to compile both source files, and therefore the entire library or executable program, you'll need to add the output of both .c files to the linker command line, or include them in the same "project" (depending on your IDE/compiler).

当然,为了能够编译两个源文件,从而编译整个库或可执行程序,您需要将两个.c文件的输出添加到链接器命令行,或者将它们包含在同一个“项目”中(取决于您的IDE /编译器)。

Many people suggest that you make header files for all your forward declarations, even if you don't think you'll need them. When you (or other people) go to modify your code, and change the signature of functions, it will save them time from having to modify all of the places where the function is forward-declared. It may also help save you from some subtle bugs, or at least confusing compiler errors.

许多人建议您为所有前向声明制作头文件,即使您认为不需要它们也是如此。当您(或其他人)去修改您的代码并更改函数的签名时,它将节省他们修改函数前向声明的所有位置的时间。它还可以帮助您避免一些微妙的错误,或至少令人困惑的编译器错误。

#1


30  

You would put a declaration for the function in the file func1.h, and add #include "func1.h" in call.c. Then you would compile or link func1.c and call.c together (details depend on which C system).

你可以在func1.h文件中放入函数的声明,并在call.c中添加#include“func1.h”。然后你将编译或链接func1.c和call.c一起(详细信息取决于哪个C系统)。

#2


12  

Use a Forward Declaration

使用远期声明

For example:

例如:

typedef struct
{
    int SomeMemberValue;
    char* SomeOtherMemberValue;
} SomeStruct;

int SomeReferencedFunction(int someValue, SomeStruct someStructValue);

int SomeFunction()
{
   SomeStruct s;
   s.SomeMemberValue = 12;
   s.SomeOtherMemberValue = "test string";

   return SomeReferencedFunction(5, s) > 12;
}

There is a feature that allows you to reuse these forward declarations called Header Files. Simply take the forward declarations, place them in the header file, then use #include to add them to each C source file you reference the forward declarations in.

有一个功能允许您重用这些名为Header Files的前向声明。只需获取前向声明,将它们放在头文件中,然后使用#include将它们添加到您引用前向声明的每个C源文件中。

/* SomeFunction.c */

#include "SomeReferencedFunction.h"

int SomeFunction()
{
   SomeStruct s;
   s.SomeMemberValue = 12;
   s.SomeOtherMemberValue = "test string";

   return SomeReferencedFunction(5, s) > 12;
}

/* SomeReferencedFunction.h */

typedef SomeStruct
{
    int SomeMemberValue;
    char* SomeOtherMemberValue;
} SomeStruct;

int SomeReferencedFunction(int someValue, SomeStruct someStructValue);

/* SomeReferencedFunction.c */

/* Need to include SomeReferencedFunction.h, so we have the definition for SomeStruct */
#include "SomeReferencedFunction.h"

int SomeReferencedFunction(int someValue, SomeStruct someStructValue)
{
    if(someStructValue.SomeOtherMemberValue == NULL)
        return 0;

    return someValue * 12 + someStructValue.SomeMemberValue;
}

Of course, to be able to compile both source files, and therefore the entire library or executable program, you'll need to add the output of both .c files to the linker command line, or include them in the same "project" (depending on your IDE/compiler).

当然,为了能够编译两个源文件,从而编译整个库或可执行程序,您需要将两个.c文件的输出添加到链接器命令行,或者将它们包含在同一个“项目”中(取决于您的IDE /编译器)。

Many people suggest that you make header files for all your forward declarations, even if you don't think you'll need them. When you (or other people) go to modify your code, and change the signature of functions, it will save them time from having to modify all of the places where the function is forward-declared. It may also help save you from some subtle bugs, or at least confusing compiler errors.

许多人建议您为所有前向声明制作头文件,即使您认为不需要它们也是如此。当您(或其他人)去修改您的代码并更改函数的签名时,它将节省他们修改函数前向声明的所有位置的时间。它还可以帮助您避免一些微妙的错误,或至少令人困惑的编译器错误。