Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I use Vulkan with MinGW? (R_X86_64_32 error)

Tags:

c++

vulkan

I am trying to setup a bare bones program to use Vulkan. I installed the LunarG SDK. I have a tiny program that basically just calls vkCreateInstance. I compiled with this line:

g++ -std=c++11 -I/c/VulkanSDK/1.0.3.1/Include -L/c/VulkanSDK/1.0.3.1/Bin main.cpp -lvulkan-1

I get this compiler error using 64-bit mingw (MSYS2):

 relocation truncated to fit||R_X86_64_32 against symbol `__imp_vkCreateInstance' defined in .idata$5 section in C:\VulkanSDK\1.0.3.1\Bin/vulkan-1.lib(vulkan-1.dll.b)|

What do I do? Am I linking against the right library?

like image 218
TheBuzzSaw Avatar asked Feb 20 '16 21:02

TheBuzzSaw


4 Answers

I was able to compile a simple program, with just a call to vkCreateInstance with MinGW-64.

Maybe the error you're getting is related to the -m64 flag.

Follow bellow my configuration:

  • Windows 8.1
  • NetBeans IDE 8.1
  • Vulkan SDK 1.0.3.1
  • gcc version 5.3.0 (x86_64-posix-seh-rev0, Built by MinGW-W64 project)

With g++:

Compile:

g++ -m64 -std=c++11 -c -g -I/C/VulkanSDK/1.0.3.1/Include -MMD -MP -MF "build/Debug/MinGW-Windows/main.o.d" -o build/Debug/MinGW-Windows/main.o main.c

Link:

g++ -m64 -std=c++11 -o dist/Debug/MinGW-Windows/vulkanfirsttest build/Debug/MinGW-Windows/main.o -L/C/VulkanSDK/1.0.3.1/Bin -lvulkan-1

With gcc:

Compile:

gcc -m64 -c -g -I/C/VulkanSDK/1.0.3.1/Include -std=c11 -MMD -MP -MF "build/Debug/MinGW-Windows/main.o.d" -o build/Debug/MinGW-Windows/main.o main.c

Link:

gcc -m64 -o dist/Debug/MinGW-Windows/vulkanfirsttest build/Debug/MinGW-Windows/main.o -L/C/VulkanSDK/1.0.3.1/Bin -lvulkan-1

Source code:

#include <stdio.h>
#include <stdlib.h>
#include <vulkan/vulkan.h>

int main(int argc, char *argv[]) {

    VkInstanceCreateInfo vk_info;
    VkInstance inst = 0;
    VkResult res;

    vk_info.sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO;

    vk_info.pNext = NULL;

    vk_info.pApplicationInfo = NULL;

    vk_info.enabledLayerCount = 0;

    vk_info.ppEnabledLayerNames = NULL;

    vk_info.enabledExtensionCount = 0;

    vk_info.ppEnabledExtensionNames = NULL;

    res = vkCreateInstance(&vk_info, NULL, &inst);

    if (res != VK_SUCCESS) {
        // Error!
        printf("Error %d\n", res);        
        return 1;
    };

    printf("Device created: %p\n", inst);

    vkDestroyInstance(inst, NULL);
    return (EXIT_SUCCESS);
}

Output:

Device created: 0000000000534FD0

like image 78
Gomiero Avatar answered Nov 02 '22 14:11

Gomiero


You guys were both more lucky than me, but then again, I was trying to build the cube example. I kept getting the relocation truncated problem and after some digging, managed to connect it to an old bug report/support request: https://sourceforge.net/p/mingw-w64/support-requests/19/

My solution was to use dlltool and extract the symbols from vulkan-1.dll (another old howto - http://www.mingw.org/wiki/createimportlibraries). This didn't work fully as it couldn't extract any symbols so I had to manually fill them in (luckily, gcc outputs 1 line per undefined symbol). Basically, here's the beginning of my vulkan-1.def file (append your functions, one per line to the end of it):

LIBRARY vulkan-1.dll
vkAllocateCommandBuffers
vkAllocateDescriptorSets
vkAllocateMemory
; add functions as needed, one per line

After preparing this file, run

dlltool -d vulkan-1.def -l libvulkan-1.a

Now you can use -L. -lvulkan-1 and avoid the relocation issues. My full gcc command line is:

gcc -g cube.c -o cube.exe -I /c/VulkanSDK/1.0.8.0/Include/ -D_WIN32 -DVK_USE_PLATFORM_WIN32_KHR -L . -lvulkan-1 -mwindows

And voila, cube works.

Note: I also needed to replace wcstombs_s to wcstombs for it to compile. The resulting line is now:

numConverted = wcstombs(argv[iii], commandLineArgs[iii], wideCharLen + 1);
like image 34
borancar Avatar answered Nov 02 '22 14:11

borancar


Instead of using -lvulkan-1 or going through the trouble with ddltool, you can try explictly listing the vulkan-1.dll and it should resolve the symbols.

gcc -std=c99 -m64 -g -Wall -Ic:\VulkanSDK\1.0.39.1\Include\vulkan vktest.c -o vktest c:\Windows\System32\vulkan-1.dll

like image 36
eloj Avatar answered Nov 02 '22 15:11

eloj


I was able to get it working with TDM-GCC 64-bit by copying vulkan-1.dll to the current directory, and linking it to that. The -m64 doesn't seem to be necessary, but if vulkan-1.dll is not in current working directory, ld.exe crashes.

CMake configuration:

...
FIND_PACKAGE(Vulkan REQUIRED)
IF(WIN32 AND NOT MSVC)
    GET_FILENAME_COMPONENT(Vulkan_LIBRARY_DIR ${Vulkan_LIBRARY} DIRECTORY)
    IF(NOT "${Vulkan_LIBRARY_DIR}" STREQUAL "${CMAKE_BINARY_DIR}")
        MESSAGE(WARNING "If linking to Vulkan fails, try copying vulkan-1.dll to the ${CMAKE_BINARY_DIR} and then set Vulkan_LIBRARY to ${CMAKE_BINARY_DIR}/vulkan-1.dll")
    ENDIF(NOT "${Vulkan_LIBRARY_DIR}" STREQUAL "${CMAKE_BINARY_DIR}")
ENDIF(WIN32 AND NOT MSVC)
TARGET_LINK_LIBRARIES(myprogram ${Vulkan_LIBRARY} ... )
...
like image 2
Chris Avatar answered Nov 02 '22 15:11

Chris