C:标题中的原型声明和实现的函数声明之间的区别?

时间:2022-01-21 15:06:51

I was wondering about little differences between declaration of function prototypes in headers and in .c files. I have a header with some prototype functions and a some .c files with real implementation of these functions. I made some changes in the header, only added "__restrict" qualifier (recognized by gcc). My question is do I have to put the "__restrict" qualifier in .c files to (currently the code compile so I guess the answer is no, but some precision would be appreciated).

我想知道在头文件和.c文件中声明函数原型之间的细微差别。我有一个带有一些原型函数的头文件和一些带有这些函数实际实现的.c文件。我在标题中做了一些更改,只添加了“__restrict”限定符(由gcc识别)。我的问题是,我必须将._文件中的“__restrict”限定符放入(目前代码编译,所以我猜答案是否定的,但有些精确性会受到赞赏)。

Does this work for every C qualifier? Can I add some "const" or "volatile" in header without having to do the same in .c files?

这适用于每个C限定符吗?我可以在标题中添加一些“const”或“volatile”而不必在.c文件中执行相同操作吗?

currently in header :

目前在标题中:

int myfunc_gettype (const mytype *__restrict, int *__restrict);

and in implementation file :

并在实施文件中:

int myfunc_gettype(const mytype *attr, int *type)

3 个解决方案

#1


You must. The mismatch invokes undefined behavior. Is there some reason why you want to have separate declarations in the header and at definition?

你必须。不匹配调用未定义的行为。您是否有理由在标题和定义中包含单独的声明?

Note well, that the keyword is restrict as opposed to __restrict which is a vendor extension (hint: look at the _'s before the keyword name). You should stick to the standard version for portability.

请注意,关键字是restrict,而不是__restrict,它是供应商扩展(提示:查看关键字名称前的_)。你应该坚持使用标准版本来实现可移植性。

#2


With gcc 4.0.1, it depends on whether the const is pointless:

使用gcc 4.0.1,它取决于const是否毫无意义:

// Who cares, compiles fine, but irks the maintenance programmer.

// f.h
int f(const int i);

// f.c
int f(int i) { return i += 42; }


// No no no no Your Types Conflict gcc will not stand for it

// f.h
int f(const int *pi);

// f.c
int f(int *pi) { return (*pi)+= 42; }

#3


NOTE: You haven't actually added the 'restrict' qualifier. You just have different (optional) variable names in the prototype.

注意:您实际上没有添加'restrict'限定符。您只需在原型中使用不同的(可选)变量名称。

As to your question, most good C compilers will catch this bug and throw a warning/error if the non-matching prototype is #included with the implementation. If you have mismatching prototypes, you may see problems ranging from subtle to instant crash.

至于你的问题,如果不匹配的原型与实现#included一致,大多数好的C编译器都会捕获这个bug并抛出警告/错误。如果您的原型不匹配,您可能会发现从细微崩溃到即时崩溃的各种问题。

#1


You must. The mismatch invokes undefined behavior. Is there some reason why you want to have separate declarations in the header and at definition?

你必须。不匹配调用未定义的行为。您是否有理由在标题和定义中包含单独的声明?

Note well, that the keyword is restrict as opposed to __restrict which is a vendor extension (hint: look at the _'s before the keyword name). You should stick to the standard version for portability.

请注意,关键字是restrict,而不是__restrict,它是供应商扩展(提示:查看关键字名称前的_)。你应该坚持使用标准版本来实现可移植性。

#2


With gcc 4.0.1, it depends on whether the const is pointless:

使用gcc 4.0.1,它取决于const是否毫无意义:

// Who cares, compiles fine, but irks the maintenance programmer.

// f.h
int f(const int i);

// f.c
int f(int i) { return i += 42; }


// No no no no Your Types Conflict gcc will not stand for it

// f.h
int f(const int *pi);

// f.c
int f(int *pi) { return (*pi)+= 42; }

#3


NOTE: You haven't actually added the 'restrict' qualifier. You just have different (optional) variable names in the prototype.

注意:您实际上没有添加'restrict'限定符。您只需在原型中使用不同的(可选)变量名称。

As to your question, most good C compilers will catch this bug and throw a warning/error if the non-matching prototype is #included with the implementation. If you have mismatching prototypes, you may see problems ranging from subtle to instant crash.

至于你的问题,如果不匹配的原型与实现#included一致,大多数好的C编译器都会捕获这个bug并抛出警告/错误。如果您的原型不匹配,您可能会发现从细微崩溃到即时崩溃的各种问题。