0

I have recently returned to the world of C++ and CMake.

Having started a project on MacOS that makes use of SDL2 I found it relatively straightforward to include the SDL2 library in the project by simply "installing" the library on the OS. My project would build and launch without an issue.

I have since had to move over to a Windows machine, and not found the experience to be the same. There are a swath of methodologies, approaches and recommendations. While searching for how to maintain dependencies, such as SDL2, I came across FetchContent_Declare which provided the ability to download dependencies as part of the CMake build. I liked this as it felt very much like NPM style package management. The build would take care of fetching dependencies.

enter image description here

After much messing around, I've managed to setup my build so that it downloads SDL2 and the extensions that I need without any issue and the exe is built. However, when I try to run the exe nothing happens. Running the exe manually (not through Visual Studio Code) I was provided a message stating that SDL2.dll could not be found. I have install commands in my CMake, which copy the dll files to the install directly, but these only take effect when running an install build specifically.

enter image description here

I am guessing that because FetchContent_Declare downloads SDL2 into a location within the build directory, it is explicitly telling the the complier where the require files are for build but then when the exe runs the DLLs are neither in the main directory or available in the path so nothing happens.

With the exception of adding a custom command, to copy the DLLs into the Debug directory, is there an elegant way to let the exe know where the DLLs are?

Note: I will likely want to switch between machines regularly including both Windows & MacOS and want to cross-compile so having this automated and not having to worry about installing libraries is essential.

James
  • 2,609
  • 3
  • 20
  • 27
  • Have you tried adding `x64`/`x86` for your library path? – Ranoiaetep Nov 25 '22 at 11:42
  • Stack Overflow discourages using **images** for **code**. As a user with over 2k reputation you are expected to be aware of such basic thing... – Tsyvarev Nov 25 '22 at 12:10
  • @Tsyvarev - My question doesn't revolve around the code, it's simply there to outline the context and therefore I see no reason why it shouldn't be an image. If my question revolved around the code and I was expecting people to execute it, then I would have added it as a code block but thank you for your opinion. – James Nov 25 '22 at 12:37
  • Well, if omit the code and read only your question - "With the exception of adding a custom command, to copy the DLLs into the Debug directory, is there an elegant way to let the exe know where the DLLs are?" - The answer is: "**No**, there is no such way". It is not a limitation of `FetchContent_Declare` command or of CMake in whole. This is a limitation of the Windows. CMake cannot avoid limitations of the platform when you run your program. – Tsyvarev Nov 25 '22 at 12:52
  • About loading dll from the directory, different from the one containing the executable, see [this question](https://stackoverflow.com/questions/70747958/make-windows-executable-search-into-a-custom-directory-for-dlls) or [that one](https://stackoverflow.com/questions/5765986/how-can-i-run-an-exe-with-dlls-in-separate-directory). They are not about CMake, but again, the limitation is not from CMake but from the Windows. – Tsyvarev Nov 25 '22 at 13:02
  • To elaborate on Tsyvarev's comment on images of text, please [edit] to change your image of code into a formatted code block (which is actual text). [See here](https://meta.stackoverflow.com/a/285557/11107541) for why. – starball Nov 25 '22 at 21:55
  • You don't have to tell us your personal story. Just ask a clear, well-defined question. Anything that isn't necessary or useful to answer the question could distract from your question. – starball Nov 25 '22 at 22:00
  • You may be interested in ["_Is there a Windows/MSVC equivalent to the -rpath linker flag?_"](https://stackoverflow.com/a/230425/11107541). – starball Nov 25 '22 at 22:04

0 Answers0