我构建了一个多语言的软件图像处理程序,并将其与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:mingwx86_64-w64-mingw32includeprocess.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 <process.h>
,因为Mac OS X和Ubuntu使用#include <pthread.h>
。有什么帮助吗?完整的C程序在这里。
为windows而不是为其他系统编译时的消息并不令人惊讶。由于_WIN32
宏的使用,只有在为windows构建代码时才由编译器定义,因此只有在为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