moltenvk project makefile- how to link via clang

Viewed 331

This file: MoltenVK Runtime User Guide offers a nice description of how to link MoltenVK to a project that uses it, within XCode.

I prefer using makefiles, as it removes the need to boot up XCode to build (+ is a consistent workflow with the same project on Linux).

The problem is there's quite a bit of OSX/XCode specific terminology in the instructions, and I'm not quite sure how it translates to running clang via command line.

If you assume I'm looking to link only with the minimum specified to use MoltenVK, what would such a command line argument look like?

Here is my minimal attempt:

MOLTENVKDIR = /blah/vulkansdk-macos-1.1.108.0

a.out: my_headers.h my_src_files.cpp
    clang -I$(MOLTENVKDIR)/MoltenVK/include -L$(MOLTENVKDIR)/MoltenVK/macOS/dynamic -lMoltenVK my_src_files.cpp

More specific questions:

How should I be setting LD_RUNPATH_SEARCH_PATHS? I assume this is a PATH-style variable that gets embedded in the executable with a list of places to look for the MoltenVK.dylib file?

In step 4 of the user guide, it says "drag (MoltenVK/macOS/dynamic/libMoltenVK.dylib) to the Link Binary With Libraries list"- is that essentially the -lMoltenVK line?

Where does step 5 come in? Should I append -framework Metal -framework Foundation -framework ...? What should I do with the .tbd file?

Is step 6 just ensuring that I copy the libMoltenVK.dylib file relative to a.out consistent with LD_RUNPATH_SEARCH_PATHS?

Is step 7 safe to ignore, as I'm not using XCode?

1 Answers

TLDR; I use g++, use the flags -lMoltenVK and -L$(MOLTENVK_DIR), and at the top define VK_ICD_FILENAMES using export VK_ICD_FILENAMES=$(ICD_DIR)

Header Search Paths is -I$(PATH) Library Search Paths is -L$(PATH) Runpath Search Paths is -rpath $(PATH) (with the space)

What I did was copy the .dylibs into a folder called lib in my project folder, set Library Search Path with -Llib, and link them with -lvulkan and -lMoltenVK. Since these dynamic libraries are hardwired to use @rpath when linking, I set -rpath lib.

You can see the vulkan tutorial for linux which uses a Makefile which shows a very basic version of making a Makefile for Vulkan on linux (no MoltenVK)

Most importantly, to not get a VkResult -9 when calling vkCreateInstance (MoltenVK not installed properly) make sure to define the VK_ICD_FILENAMES environment variable! I did this by writing export VK_ICD_FILENAMES=lib/MoltenVK_icd.json near the top of my Makefile, and copied the MoltenVK_icd.json file to my project! This is a file which points to where the dylib is, and is necesarry for MoltenVK to work. When installing MoltenVK, it was inside the same folder as libMoltenVK.dylib

Similarily, you must define VK_LAYER_PATH for validation layers to work. Minimally, all you need for MoltenVK is:

export VK_ICD_FILENAMES=lib/MoltenVK_icd.json 
main:
    g++ test.cpp -o App -Llib -rpath lib -lMoltenVK

I'm not an expert in best practices and conventions, I do what works. Hope this helped

Related