Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Linking nvrhi and nvrhi_vk to a library produces errors when linking to an executable afterwards #48

Closed
tlegoc opened this issue Feb 27, 2024 · 5 comments

Comments

@tlegoc
Copy link

tlegoc commented Feb 27, 2024

Hi,
I followed the instruction to install nvrhi as a submodule and I'm trying to use it with cmake.
I use

set(NVRHI_WITH_VULKAN ON)
add_subdirectory(thirdparty/nvrhi)

To include the project, then later on I link it to one of my libraries.

target_link_libraries(Renderer Vulkan-Headers) # This line changed a lot
target_link_libraries(Renderer nvrhi_vk)
target_link_libraries(Renderer nvrhi)

This is what it currently looks like. At some point I had nvrhi first, then I used Vulkan::Vulkan instead of Vulkan-Headers, etc. But in the end the target Renderer always built itself correctly.

Now if I link Renderer to another target and use one of the functions I get a link error LNK2001 (The compiler can't find vulkan-hpp's dynamic loader).

I tried linking nvrhi to said target, I tried linking vulkan, didn't work. Note that I use vk-boostrap to speed up vulkan instance creation but even if I remove it it still doesn't work.

Does anyone have an idea as why it doesn't work ? Maybe I'm just dumb and forgot how c++ libraries and linking work.

@tlegoc
Copy link
Author

tlegoc commented Feb 27, 2024

Bonus code : how I include the headers

In my library

VulkanContext.h

#include <nvrhi/nvrhi.h>
#include <VkBootstrap.h>

VulkanContext.cpp

#include <Renderer/VulkanContext.h>

#include <nvrhi/vulkan.h>
#include <nvrhi/validation.h>
#include <Logging/Logging.h>

#define GLFW_INCLUDE_VULKAN // Might not be needed, or maybe use GLFW_INCLUDE_NONE
#include <GLFW/glfw3.h>

In my executable

TestContext.cpp

#include <Renderer/VulkanContext.h>

@apanteleev
Copy link
Contributor

I get a link error LNK2001 (The compiler can't find vulkan-hpp's dynamic loader).

That's the problem. When linking NVRHI statically, some other part of the final linked program needs to define the Vulkan dynamic loader that will be used by NVRHI. When NVRHI is built as a DLL, the loader is defined inside the library.

See https://github.com/NVIDIAGameWorks/donut/blob/main/src/app/vulkan/DeviceManager_VK.cpp and search for "dynamic" for an example.

(Also, maybe it makes sense to add a CMake option to let NVRHI define the loader even in static linking mode)

@tlegoc
Copy link
Author

tlegoc commented Feb 27, 2024

I get a link error LNK2001 (The compiler can't find vulkan-hpp's dynamic loader).

That's the problem. When linking NVRHI statically, some other part of the final linked program needs to define the Vulkan dynamic loader that will be used by NVRHI. When NVRHI is built as a DLL, the loader is defined inside the library.

See https://github.com/NVIDIAGameWorks/donut/blob/main/src/app/vulkan/DeviceManager_VK.cpp and search for "dynamic" for an example.

Makes sense, I think I'll go towards the shared dll route.

(Also, maybe it makes sense to add a CMake option to let NVRHI define the loader even in static linking mode)

What do you mean by that though?

Thanks a lot for the quick answer!

@apanteleev
Copy link
Contributor

What do you mean by that though?

I mean that I could add an option to change the way NVRHI is built in the static library mode. With the new option enabled, it would define the dynamic loader inside the library so that you won't have to. One downside here is that the application may need to use the dynamic loader before NVRHI is initialized, which would not be possible.

@tlegoc
Copy link
Author

tlegoc commented Feb 27, 2024

Ah I see. I'm not experienced enough to speak on the matter, I have to admit.
I'm closing the issue, thanks again !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants