GLFW + Vulkan on OSX

3.3k Views Asked by At

I am setting up GLFW and Vulkan for a project of mine. For Vulkan I am using MoltenVk to get Vulkan compat and GLFW for the window creation. The IDE I am using is CLion, which uses the CMake system.

Judging from some github issues, there seems to be support for this setup, but nobody there mentions how it is done.

GLFW is installed through homebrew and MoltenVK manually by adding the MoltenVK and vulkan folders to usr/local/include and the contents of the MacOS folder to usr/local/lib (even though I am quite sure MoltenVK.framework should not be there).

At this point, Clion can see the GLFW and Vulkan headers, but I still need to link them up properly.

the full CMakeLists.txt file is this now:

cmake_minimum_required(VERSION 3.8)
project(VulkanEngine)

#set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} “${CMAKE_SOURCE_DIR}/cmake/Modules”)

set(CMAKE_CXX_STANDARD 17)

set(SOURCE_FILES src/main.cpp)
add_executable(VulkanEngine ${SOURCE_FILES})

#Finding and linking GLFW3

find_package(glfw3 3.2 REQUIRED)
if (glfw3_FOUND)
    include_directories(${glfw3_INCLUDE_DIRS})
    target_link_libraries (VulkanEngine ${glfw3_LIBRARIES})
endif (glfw3_FOUND)

#Finding and linking Vulkan

find_package (Vulkan)
if (Vulkan_FOUND)
    include_directories(${Vulkan_INCLUDE_DIRS})
    target_link_libraries (VulkanEngine ${Vulkan_LIBRARIES})
endif (Vulkan_FOUND)

the console tells the following when the cmake project has been reloading:

-- Could NOT find Vulkan (missing:  Vulkan_LIBRARY) 
-- Configuring done
-- Generating done
-- Build files have been written to: /Users/mtesseract/dev/cpp/VulkanEngine/cmake-build-debug

to me this indicates that GLFW has been found and linked, but when I try to build the "hello world" for GLFW, I get the following message:

Undefined symbols for architecture x86_64:
  "_glfwInit", referenced from:
      _main in main.cpp.o

I got FindVulkan.cmake from the GLFW github page https://git.io/v5ggN, since it has support for MoltenVK, but I am not sure if it is picked up by CMake at all. (I put the file in (projectroot)/cmake/modules/)

At this point, I am out of ideas as to why things are not linking properly, so help is appreciated.

2

There are 2 best solutions below

2
On

I have found a solution that at least till some degree seems to work. At this point I have another problem to take care of (GLFW doesn't seem to pick up on MoltenVK's presence).

The process thus far is as follows (and can be found on my github page (github.com/mtesseracttech/VulkanEngine):

Vulkan + GLFW + GLM Setup Process with CMake and Package Managers

Windows:

Preparation:
Create CLion Application Project 
Install MSYS2 (Cygwin-like package manager that includes a windows port of Arch's PacMan)
Install LunarG VK SDK
Download FindVulkan.cmake from GFLW's Github (it includes a path for MoltenVK(for OSX Later)) and put it in (proj_root/cmake/modules)

MSYS2 Commands:

$ pacman -Su //Updates pacman
$ pacman -Ss {packageName} //Is used for searching for exact package names

Installed Packages through msys2:

$ pacman -S mingw-w64-x86_64-toolchain //Installs the CLion toolchain that includes CMake, Make, GCC, etc.
$ pacman -S mingw-w64-x86_64-glfw //Installs GLFW
$ pacman -S mingw-w64-x86_64-glm //Installs GLM
$ pacman -S mingw-w64-x86_64-vulkan //Vulkan can also be installed through this method, but I went with LunarG

Enter the following in the CMakeLists.txt file in the root of the project:

######################################################################################

cmake_minimum_required(VERSION 3.8)
project(VulkanEngine)

set(CMAKE_CXX_STANDARD 17)

set(SOURCE_FILES src/main.cpp)
add_executable(VulkanEngine ${SOURCE_FILES})

#Setting up PkgConfig

find_package(PkgConfig REQUIRED)

#Finding and linking GLFW3

pkg_search_module(GLFW3 3.2 REQUIRED glfw3)
if(GLFW3_FOUND)
    message(STATUS "Found GLFW, Including and Linking now")
    include_directories(${GLFW3_INCLUDE_DIRS})
    target_link_libraries(VulkanEngine ${GLFW3_STATIC_LIBRARIES})
endif(GLFW3_FOUND)

set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/cmake/modules/")

#Finding and linking Vulkan

find_package (Vulkan)
if (VULKAN_FOUND)
    message(STATUS "Found Vulkan, Including and Linking now")
    include_directories(${VULKAN_INCLUDE_DIR})
    target_link_libraries (VulkanEngine ${VULKAN_LIBRARY})
endif (VULKAN_FOUND)

######################################################################################




OSX:

Preparation

Create CLion Application Project 
Install HomeBrew (package manager)
Download MoltenVK
Download FindVulkan.cmake from GFLW's Github (it includes a path for MoltenVK(for OSX Later)) and put it in (proj_root/cmake/modules)

Homebrew:
brew update //Updates homebrew
brew install glfw //Installs GLFW
brew install glm //Installs GLM

MoltenVK:
Place MoltenVK/macOS's contents into /usr/local/lib
Place MoltenVK/macOS/MoltenVK.framework/headers's contents into /usr/local/inc/MoltenVK

CMake: 
Same as on windows, but in the FindVulkan.cmake file, add at the very end of the elseif(APPLE) block the line:
set(VULKAN_INCLUDE_DIR "/usr/local/include/MoltenVK")
This is not a pretty or flexible solution, but for now it will do.

I am pretty sure this solution is not ideal since GLFW isn't picking up on MoltenVK with this tactic, but at least things compile, so I see that as progress.

0
On

Seems your issue is that you haven’t defined the VULKAN_SDK variable. Make sure you point this to the parent folder of /lib & /include inside the SDK and FindVulkan should work

VULKAN_SDK=/opt/vulkan/x86_64 cmake.