gpt4 book ai didi

c - 包括另一个内部 .c 文件中的函数?

转载 作者:行者123 更新时间:2023-11-30 21:27:55 24 4
gpt4 key购买 nike

我使用结构和函数编写了一些程序,现在我必须将我的工作分成 3 个文件,如下所示:

header.h - declaring data types, functions, #define
func.c - function definition,
main.c - structure array initialization and calling functions

这样做有意义吗?我这样做了,在两个 .c 文件中添加了#include“header.h”,但它无法编译,似乎 func.c 有问题。我使用的是 VS 2017。

最佳答案

让我们考虑下面的例子,

header.h :通常用户定义的头文件包含宏、结构、预定义头文件等通用信息,在多文件编译中大多数文件都会使用这些信息。

#include<stdio.h>
#include<malloc.h>
int n;
#define MAX 5
void call();

typedef enum legends {
L_GANDHI,
L_MARTIN_LUTHER_KING
}legends_e;

ma​​in.c:

#include"header.h"
int main()
{
printf("in main() : %d \n",L_GANDHI);
call();// defined in other file
return 0;
}

fun.c:

#include"header.h"
void call()
{
printf("In call() function : %d \n",L_MARTIN_LUTHER_KING);
}

用于编译所有文件,

xyz@xyz-PC:~/s_flow/john$ gcc main.c func.c

请记住,在多文件编译中,您应该只有一个 main() 函数。希望以上信息能够帮助您解决问题。

关于c - 包括另一个内部 .c 文件中的函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47269118/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com