gpt4 book ai didi

c - 如何让 GCC 警告函数调用和函数定义中参数数量不匹配?

转载 作者:太空宇宙 更新时间:2023-11-04 00:24:25 25 4
gpt4 key购买 nike

我有两个文件。

第一个文件包含函数原型(prototype),主函数仅使用一个参数调用 myfunc:

int myfunc (int x);
int main ()
{
int x =5;
myfunc(x);
}

第二个文件包含函数定义,但有两个参数:

int myfunc (int x, int y)
{
return x+y;
}

当我尝试使用 GCC 编译这两个文件时,我没有收到任何错误或警告。

如何强制 GCC 对此类事件发出警告??

最佳答案

将您的原型(prototype)放在头文件中,并在所有使用这些函数的源文件中#include 头文件。

GCC 独立编译每个文件,所以它无法知道函数的定义与声明不对应,除非声明也包含在定义的文件中。

它应该是这样的:

myfunc.h

#ifndef MYFUNC_H
#define MYFUNC_H

int myfunc (int x);

#endif

myfunc.c

#include "myfunc.h"

int myfunc (int x, int y)
{
return x+y;
}

主.c

#include "myfunc.h"

int main ()
{
int x =5;
myfunc(x);
}

关于c - 如何让 GCC 警告函数调用和函数定义中参数数量不匹配?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30366222/

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