Simon Kissane
Simon Kissane

Reputation: 5308

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.

Upvotes: 0

Views: 298

Answers (3)

peri cycle
peri cycle

Reputation: 23

You can try use the follow below to use virtdisk.h, i've tried this and work pretty fine.

#include <initguid.h>
#include <virtdisk.h>
#pragma comment(lib,"virtdisk.lib")

The whole project looks like this

#include "pch.h"

#include <Windows.h>

#include <initguid.h>
#include <virtdisk.h>
#pragma comment(lib,"virtdisk.lib")

using namespace winrt;
using namespace Windows::Foundation;

DWORD SimpleCreateVirtualDisk(
    _In_ PCWSTR Path,
    _In_ UINT64 Size,
    _Out_ PHANDLE Handle)
{
    VIRTUAL_STORAGE_TYPE StorageType;
    StorageType.DeviceId = VIRTUAL_STORAGE_TYPE_DEVICE_UNKNOWN;
    StorageType.VendorId = VIRTUAL_STORAGE_TYPE_VENDOR_UNKNOWN;

    CREATE_VIRTUAL_DISK_PARAMETERS Parameters;
    std::memset(&Parameters, 0, sizeof(CREATE_VIRTUAL_DISK_PARAMETERS));
    Parameters.Version = CREATE_VIRTUAL_DISK_VERSION_2;
    Parameters.Version2.MaximumSize = Size;

    return ::CreateVirtualDisk(
        &StorageType,
        Path,
        VIRTUAL_DISK_ACCESS_NONE,
        nullptr,
        CREATE_VIRTUAL_DISK_FLAG_NONE,
        0,
        &Parameters,
        nullptr,
        Handle);
}


int main()
{
    init_apartment();
    
    
}


Upvotes: 0

Jeaninez - MSFT
Jeaninez - MSFT

Reputation: 4040

I suggest you could follow the following steps to solve the issue:

1, You should link the virtdisk.lib: #pragma comment(lib, "virtdisk.lib")

2, According to the Doc:https://learn.microsoft.com/en-us/windows/win32/api/virtdisk/ns-virtdisk-virtual_storage_type

VendorId

Vendor-unique identifier.

VIRTUAL_STORAGE_TYPE_VENDOR_MICROSOFT (EC984AEC-A0F9-47e9-901F-71415A66345B)

You should provide the identifier of VIRTUAL_STORAGE_TYPE_VENDOR_MICROSOFT:

static const GUID VIRTUAL_STORAGE_TYPE_VENDOR_MICROSOFT = { 0xEC984AEC, 0xA0F9, 0x47e9, 0x90, 0x1F, 0x71, 0x41, 0x5A, 0x66, 0x34, 0x5B };

Upvotes: 1

Brecht Sanders
Brecht Sanders

Reputation: 7305

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

  • -lvirtdisk for OpenVirtualDisk
  • -luuid for VIRTUAL_STORAGE_TYPE_VENDOR_MICROSOFT

Upvotes: 1

Related Questions