I’m trying to port an open source project over to mingw. I’m getting a strange conversion error in one of the source files that compiles fine when under msvc.
Here’s the isolated test case reproducing the error:
#include <windows.h>
void * test(HMODULE h, const char *name)
{
return GetProcAddress(h, name);
}
int main() {}
And the corresponding error output when compiling with mingw:
g++.exe -Wall -g -pedantic -Wall -IG:\OSS\blender-dev\lib\mingw32\opencolorio\include -c G:\OSS\compile_test\main.cpp -o Debug\main.o
G:\OSS\compile_test\main.cpp: In function 'void* test(HMODULE, const char*)':
G:\OSS\compile_test\main.cpp:45:34: error: invalid conversion from 'FARPROC {aka int (__attribute__((__stdcall__)) *)()}' to 'void*' [-fpermissive]
Of course a simple way to push through this compile error is to do the explicit cast:
return (void *)GetProcAddress(h, name);
But before doing that I need to know the following:
- Why isn’t this a problem under MSVC but it is under Mingw?
- Is doing the cast like this appropriate for fixing this problem? Any side-effects I have to worry about?
- Is there an alternative? What’s recommended when porting code like this?
I’m currently building this with Mingw 4.7.2 but I’ve also tried earlier versions like 4.7.1 and 4.5.2 with similar errors.
For those curious here’s the actual source file:line in the repository where this is happening: https://github.com/OpenImageIO/oiio/blob/master/src/libutil/plugin.cpp#L96
Visual C++ has an “extension” that allows implicit conversion from a pointer-to-function to a
void*. This conversion is not part of the C++ language.On Windows, where pointers to nonmember functions and pointers to objects are the same size, there is no problem with using a cast here. Practically all code that uses
GetProcAddresswill need to cast the returned pointer at some point anyway, to convert it to the correct function pointer type for use in a call expression.