Why I compile the following very simple app that is using the Mediainfo.dll on Windows using cgo:
package main
// #cgo CFLAGS: -DUNICODE
// #cgo CFLAGS: -I./MediaInfoDLL
// #cgo LDFLAGS: -L./MediaInfoDLL -lMediaInfo
// #include "MediaInfoDLL.h"
import "C"
func main() {
C.MediaInfoDLL_Load()
}
it generates the following warnings about problems with the MediaInfoDLL.h file:
In file included from .\test.go:6:
.\mediainfo\MediaInfoDLL/MediaInfoDLL.h: In function 'MediaInfoDLL_Load':
.\mediainfo\MediaInfoDLL/MediaInfoDLL.h:127:39: warning: passing argument 1 of 'LoadLibraryW' from incompatible pointer type [-Wincompatible-pointer-types]
127 | #define MEDIAINFODLL_NAME "MediaInfo.dll"
| ^~~~~~~~~~~~~~~
| |
| char *
.\mediainfo\MediaInfoDLL/MediaInfoDLL.h:350:40: note: in expansion of macro 'MEDIAINFODLL_NAME'
350 | MediaInfo_Module = LoadLibrary(MEDIAINFODLL_NAME);
| ^~~~~~~~~~~~~~~~~
In file included from C:/TDM-GCC-64/x86_64-w64-mingw32/include/winbase.h:24,
from C:/TDM-GCC-64/x86_64-w64-mingw32/include/windows.h:70,
from .\mediainfo\MediaInfoDLL/MediaInfoDLL.h:243,
from .\test.go:6:
C:/TDM-GCC-64/x86_64-w64-mingw32/include/libloaderapi.h:142:48: note: expected 'LPCWSTR' {aka 'const short unsigned int *'} but argument is of type 'char *'
142 | WINBASEAPI HMODULE WINAPI LoadLibraryW(LPCWSTR lpLibFileName);
| ~~~~~~~~^~~~~~~~~~~~~
In file included from .\test.go:6:
At top level:
.\mediainfo\MediaInfoDLL/MediaInfoDLL.h:449:17: warning: 'MediaInfoDLL_UnLoad' defined but not used [-Wunused-function]
449 | static void MediaInfoDLL_UnLoad()
| ^~~~~~~~~~~~~~~~~~~
.\mediainfo\MediaInfoDLL/MediaInfoDLL.h:441:19: warning: 'MediaInfoDLL_IsLoaded' defined but not used [-Wunused-function]
441 | static size_t MediaInfoDLL_IsLoaded()
| ^~~~~~~~~~~~~~~~~~~~~
.\mediainfo\MediaInfoDLL/MediaInfoDLL.h:333:38: warning: 'MediaInfo_Count_Get_Files' defined but not used [-Wunused-variable]
333 | static MEDIAINFO_Count_Get_Files MediaInfo_Count_Get_Files;
| ^~~~~~~~~~~~~~~~~~~~~~~~~
If I compile the exact same "C" code as pure "C" I don't get any warning messages:
#include "MediaInfoDLL.h"
int main()
{
MediaInfoDLL_Load();
}
Why does cgo generate warning (which I think should be generated) but compiling the same code as a .c file produces no warning messages?
This is due to an issue with
MediaInfoDLL.hThe instruction
defines C preprocessor symbol
UNICODE. The WIN32 headers are designed to build applications with wide chars (wchar_t). WhenUNICODEis defined, API names are expanded withWending:LoadLibrary->LoadLibraryWLoadLibraryWrequires the argument to be of typeLPWCHAR, that iswchar_t*. But the warning tells that the actual argument is of typechar*.The issue is due to the bug in
MediaInfoDLL.h:They don't check
UNICODEsymbol that you defined forcgo.There are two ways to fix:
-DUNICODEfromCFLAGS. The binary will be ASCII.-D_UNICODEtoCFLAGS:// #cgo CFLAGS: -DUNICODE -D_UNICODE- to build a Unicode binary.Both solutions compile and run.