Haider
Haider

Reputation: 341

C program compiler warning only in Windows (MinGW-w64)

I built a multi-language software image processing program and made it generally available with binaries for Mac OS X and Ubuntu. The binaries have been tested on their respective operating systems and every thing works perfectly. I recently also tried to release binaries for Windows (64 bit) but the GCC (through MinGW-w64) compiler gave me warnings for one of the C programs when I create the shared library (dll) file. This did not happen in Mac OS X or Ubuntu. Here are the warnings and the corresponding line of code in the C file:

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

Line 464:

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

The second and stranger warning:

    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  >;

Line 34:

#include <process.h>

This belongs in this larger code block:

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

#include <stdbool.h>

The problem seems to emanate from #include <process.h> since for Mac OS X and Ubuntu #include <pthread.h> is used. Any help figuring this out? The full C program is here .

Upvotes: 2

Views: 354

Answers (1)

Peter
Peter

Reputation: 36617

The messages when compiling for windows but not for other systems are hardly surprising. The offending code will only be seen by the compiler when building for windows, due to usage of the _WIN32 macro which is only defined by the compiler when code is built for windows.

The "second and stranger warning" is describing the cause. The third argument of the (windows specific) _beginthreadex() function is specified to be a pointer to a function that returns an unsigned int. The actual ThreadFunc being passed is a function that returns a void *.

The fix to make the code acceptable to a windows compiler is to change the return type of ThreadFunc() to return unsigned int. That will break the code for other systems, so you need to do the changes conditionally (i.e. have two versions of the function, and select the right one by testing the _WIN32 macro).

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

Upvotes: 3

Related Questions