【问题标题】:C program compiler warning only in Windows (MinGW-w64)仅在 Windows 中的 C 程序编译器警告 (MinGW-w64)
【发布时间】:2015-10-23 17:55:15
【问题描述】:

我构建了一个多语言软件图像处理程序,并使其与 Mac OS X 和 Ubuntu 的二进制文件一起普遍可用。这些二进制文件已在各自的操作系统上进行了测试,并且一切正常。我最近还尝试为 Windows(64 位)发布二进制文件,但是当我创建共享库 (dll) 文件时,GCC(通过 MinGW-w64)编译器给了我一个 C 程序的警告。这在 Mac OS X 或 Ubuntu 中没有发生。以下是 C 文件中的警告和相应的代码行:

warning: passing argument 3 of '_beginthreadex' from incompatible pointer type [enabled by default]

第 464 行:

ThreadList[i] = (HANDLE)_beginthreadex( NULL, 0, &ThreadFunc, &ThreadArgs[i] , 0, NULL ); 

第二个陌生人警告:

    c:\mingw\x86_64-w64-mingw32\include\process.h:31:29: note: 
expected 'unsigned int <*><void *>' but argument is of type 'void * <*><void *>'
    _CRTIMP uintptr_t _cdecl _beginthreadex<void *_Security,unsigned _Stacksize,unsigned <_stdcall *_StartAddress> <void *>,void *_ArgList,unsigned _InitFlag,unsigned *_ThrdAddr  >;

第 34 行:

#include <process.h>

这属于这个更大的代码块:

/* Multithreading stuff*/
#ifdef _WIN32
#include <windows.h>
#include <process.h>
#else
#include <pthread.h>
#endif

#include <stdbool.h>

问题似乎来自#include &lt;process.h&gt;,因为对于 Mac OS X 和 Ubuntu,使用了 #include &lt;pthread.h&gt;。任何帮助解决这个问题?完整的 C 程序是 here

【问题讨论】:

  • 这是一条警告信息,可能是无害的。如果您的程序在运行时意外退出,那么很可能是因为其他原因。
  • 我在没有任何警告的情况下编译的另一个 C 程序作为该软件的一部分成功运行。该软件的所有其他组件都运行良好。在 Ubuntu 和 Mac OS X 上,整个软件运行得非常好。剩下这个 C 文件和警告消息。我可以从打印语句中看到程序在这个特定的 C 文件期间退出。这不可能是巧合。
  • 在我看来,这就像一条红鲱鱼。但你是专家。
  • 警告信息来自#include &lt;process.h&gt;。这在 MacOS X 和 Windows 版本中没有使用。这就是我怀疑的原因。
  • 默认情况下,您必须单击左上角菜单并选择“编辑>标记”以复制出cmd窗口。您可以在属性中打开“快速编辑模式”来更改默认值

标签: c windows multithreading gcc mingw-w64


【解决方案1】:

为 windows 而不是为其他系统编译时的消息不足为奇。由于使用了 _WIN32 宏,该宏仅在为 windows 构建代码时由编译器定义。

“第二个和陌生的警告”描述了原因。 (特定于 Windows 的)_beginthreadex() 函数的第三个参数被指定为指向返回 unsigned int 的函数的指针。实际传递的ThreadFunc 是一个返回void * 的函数。

使代码为 Windows 编译器所接受的修复方法是将 ThreadFunc() 的返回类型更改为返回 unsigned int。这会破坏其他系统的代码,因此您需要有条件地进行更改(即有两个版本的函数,并通过测试_WIN32 宏选择正确的版本)。

#ifdef _WIN32
/*  use the windows version of the function here */
#else
/*  use the non-windows version of the function here */
#endif

【讨论】:

  • 谢谢。我认为你的答案是正确的。但是当我更改返回类型时,现在警告消息更改为expected 'unsigned int &lt;*&gt;&lt;void *&gt;' but argument is of type 'unsigned int * &lt;*&gt;&lt;void *&gt;'. 返回类型中有一个不应该存在的额外星号。还是我做错了什么?
  • 谢谢。当您将其更改为 unsigned int 时,它会起作用。你能在你的答案中编辑它吗(以防其他人来找)。
猜你喜欢
  • 1970-01-01
  • 2015-04-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-01-03
  • 1970-01-01
  • 1970-01-01
  • 2020-03-27
相关资源
最近更新 更多