0

我正在尝试编译以下源代码,它可以在gccMicrosoft 和 Microsoft 的cl.exe.

void SomethingBeforeExit();

void SomethingBeforeExit()
{
    // some code
    _exit(0);
}

int main(int argc, char *argv[])
{
    // some code
    atexit(SomethingBeforeExit);
}

但是,我从以下消息中收到C4113 警告:cl.exe

SomeCode.c(10): warning C4113: 'void (__cdecl *)()' differs in parameter lists from 'void (__cdecl *)(void)'

正如我所说,源代码仍然可以成功编译并且似乎可以工作。我的目标是防止在 中发生此警告cl,因为gcc在编译时不会生成任何警告。

我假设该函数的声明没有被视为void SomethingBeforeExit(void),但是,我不知道如何专门将函数的参数列表声明为void

我正在使用VS14and C/C++ 19.00.23918 for x86forcl.exegcc v5.4.0编译器来比较生成的警告。

4

2 回答 2

6

在 C 中,函数声明中的空括号并不意味着“没有参数”。相反,它表示任意数量的参数(类似于...C++)。也许您要声明的是void SomethingBeforeExit(void).

于 2017-08-03T20:09:07.383 回答
2

OP 未使用兼容的 C99/C11 编译器。代码是有效的 C (C99/C11)。

// declares function SomethingBeforeExit and no info about it parameters.
void SomethingBeforeExit();

// declares/defines function SomethingBeforeExit and 
// indirectly specifies the parameter list is `(void)`
// This does not contradict the prior declaration and so updates the parameter signature.
void SomethingBeforeExit() {
   ...  
}

int main(int argc, char *argv[]) {
  ...
  // `atexit() expects a `void (*f)(void))`
  atexit(SomethingBeforeExit);

在 C89 中,void SomethingBeforeExit() { ...}定义仍然没有说明参数列表。这可能是OP问题的原因。

修理:

void SomethingBeforeExit(void) { 
  ...
}

先前的void SomethingBeforeExit();声明不需要更新,但最好也更新它,这样参数检查就可以在看不到定义的代码中进行。

于 2017-08-03T21:13:04.147 回答