问题描述
假设我有一个定义了两个函数的文件insert.c:
1.insert_after
2.insert_before
定义这些func是这样的:
insert_after(arg1)
{
if(condition 1)
{.........}
else
insert_before(arg1);
insert_before(arg)
{
if(条件1)
{.........}
else
insert_after(arg);
}
现在,如果此文件的insert.c包含在main.c和insert_after函数中被称为
#includeinsert.c
int main()
{
insert_after(ARG);
返回0;
}
在使用gcc编译main.c时,遇到以下错误: p>
这里有什么问题以及如何避免它?
这是因为你没有为你的函数声明原型。默认情况下,没有原型的函数有一组未知的参数并返回一个int。但对于 insert_before
而言,情况并非如此。
创建一个文件 insert.h
您可以在其中声明它们:
#ifndef INSERT_H
#define INSERT_H
void insert_before(type_of_arg);
void insert_after(type_of_arg);
#endif / * INSERT_H * /
insert.c
。
您应该使用以下内容进行编译:
gcc -Wall -Wstrict-prototypes -Wmissing-prototypes -o progname insert.c main.c
Suppose I have a file insert.c in which two functions are defined:1.insert_after2.insert_before
The definitions of these func are something like this:
insert_after(arg1)
{
if(condition 1)
{ ......... }
else
insert_before(arg1);
}
insert_before(arg)
{
if(condition 1)
{ ......... }
else
insert_after(arg);
}
Now if this file insert.c is included in main.c and insert_after function is called
# include "insert.c"
int main()
{
insert_after(arg);
return 0;
}
On compiling main.c using gcc,the following error is encountered:
What is wrong here and how to avoid it?
This is because you don't declare prototypes for your functions. A function which has no prototype, by default, has an unknown set of arguments and returns an int. But this is not the case for insert_before
.
Create a file insert.h
in which you declare them:
#ifndef INSERT_H
#define INSERT_H
void insert_before(type_of_arg);
void insert_after(type_of_arg);
#endif /* INSERT_H */
and include this file at the top of insert.c
.
You should then compile with:
gcc -Wall -Wstrict-prototypes -Wmissing-prototypes -o progname insert.c main.c
这篇关于xx的c程序冲突类型错误和以前的xx隐式声明在这里的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!