cwinapimingw-w64virtual-disk

How to resolve link error "undefined reference to `VIRTUAL_STORAGE_TYPE_VENDOR_MICROSOFT`"?


Consider the following program example.c:

#include <windows.h>
#include <virtdisk.h>

int main() {
    VIRTUAL_STORAGE_TYPE storageType = {VIRTUAL_STORAGE_TYPE_DEVICE_ISO, VIRTUAL_STORAGE_TYPE_VENDOR_MICROSOFT};
    HANDLE virtualDiskHandle;
    OPEN_VIRTUAL_DISK_PARAMETERS openParameters;
    ULONG result;
    LPCWSTR isoPath = L"path_to_iso_file.iso";
    result = OpenVirtualDisk(&storageType, isoPath, VIRTUAL_DISK_ACCESS_READ, OPEN_VIRTUAL_DISK_FLAG_NONE, &openParameters, &virtualDiskHandle);
    if (result != ERROR_SUCCESS) {
        return 1;
    }
    CloseHandle(virtualDiskHandle);
    return 0;
}

I try compiling it using MinGW-w64, but I get this error:

# gcc -Wall -Wextra example.c -lvirtdisk
C:/tools/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/13.1.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\tools\msys64\tmp\cciUwbcG.o:example.c:(.rdata$.refptr.VIRTUAL_STORAGE_TYPE_VENDOR_MICROSOFT[.refpt
r.VIRTUAL_STORAGE_TYPE_VENDOR_MICROSOFT]+0x0): undefined reference to `VIRTUAL_STORAGE_TYPE_VENDOR_MICROSOFT'
collect2.exe: error: ld returned 1 exit status

What do I need to link against to get VIRTUAL_STORAGE_TYPE_VENDOR_MICROSOFT? -lvirtdisk is enough for OpenVirtualDisk but doesn't to work for that.

I tried manually defining the GUID in example.c:

DEFINE_GUID(VIRTUAL_STORAGE_TYPE_VENDOR_MICROSOFT, 0xec984aec, 0xa0f9, 0x47e9, 0x90, 0x1f, 0x71, 0x41, 0x5a, 0x66, 0x34, 0x5b);

But I still get the same error doing that.

I realise my OpenVirtualDisk invocation may well be wrong, and might not work without further changes. But all I'm asking about here is getting it to link.

gcc --version says:

gcc.exe (Rev6, Built by MSYS2 project) 13.1.0
Copyright (C) 2023 Free Software Foundation, Inc.

I tried clang instead but got the same error.


Solution

  • You will need both -lvirtdisk and -luuid linker flags.