“SDL2d.dll not found on windows with CMake using fetchcontent.”

huangapple go评论74阅读模式
英文:

SDL2d.dll not found on windows with CMake using fetchcontent

问题

我正在使用CMake开发一个跨平台应用程序。项目可以在Linux上构建,但在Windows上无法构建。任何帮助都将不胜感激。

我遇到的错误是一个弹出窗口,上面显示:

代码执行无法继续,因为找不到SDL2d.dll。重新安装程序可能会解决此问题。

由于某种原因,CMake没有像在Linux上一样将这个目录设置到CMakeCache中。

include(FetchContent)
message("Fetching SDL2...")
FetchContent_Declare(
    SDL2
    GIT_REPOSITORY https://github.com/libsdl-org/SDL.git
    GIT_TAG release-2.26.3
)
FetchContent_MakeAvailable(SDL2)

正如你所看到的,我正在使用FetchContent从仓库获取SDL2。我还认为需要注意的是,我正在使用pkgconfiglite包。我是通过chocolatey软件包管理器安装的。在获取SDL2内容时,我在输出中看到了这个消息:

"pkg-config --static --libs sdl2" 将返回无效的信息

然后它继续列出一堆SDL标志,开关状态为开启或关闭。我认为这可能是问题的原因,但由于CMake似乎在底层使用pkgconfig,这一切似乎都像一个我不知道如何处理的黑匣子。

以下是我的创建可执行文件和链接库的命令:

add_executable(${Target}
    ${SRC_FILES}
)

target_link_libraries(${Target}
    Vulkan::Vulkan
    SDL2::SDL2
)
英文:

I am working on a cross platform application with CMake. The project builds on Linux, but not on windows. Any help would be appreciated.

The error I get is a popup that says:

> The code execution cannot proceed because SDL2d.dll was not found. Reinstalling the program may fix this problem.

For some reason CMake doesn't set this directory to CMakeCache like it does for linux.

include(FetchContent)
message("Fetching SDL2...")
FetchContent_Declare(
    SDL2
    GIT_REPOSITORY https://github.com/libsdl-org/SDL.git
    GIT_TAG release-2.26.3
)
FetchContent_MakeAvailable(SDL2)

As you can see I am getting this from the repository using fetchcontent. I also think it may be important to note that I am using pkgconfiglite package. I installed it using chocolatey package manager. While fetching SDL2 content I see this message in the output

> pkg-config --static --libs sdl2" will return invalid information

It then goes on to list a bunch of SDL flags as on or off. I am assuming this is probably the reason for it, but as CMake seems to be using pkgconfig under the hood, this all seems like a blackbox that I don't know how to handle.

Below are my commands to make the executable and to link libraries.

add_executable(${Target}
    ${SRC_FILES}
)

target_link_libraries(${Target}
    Vulkan::Vulkan
    SDL2::SDL2
)

答案1

得分: 0

问题已解决。问题与pkg-config无关。

在开发跨平台应用程序时,我必须记住,与Linux不同,Windows没有专门的库和可执行文件位置。

在我的探索过程中,我提出了2个解决方案。第二种解决方案更简单且跨平台友好,但为了完整起见,我将包括两种方法。

第一种方法需要将以下内容添加到调用***add_executable()***函数的位置。它使用生成器表达式“$< >”将SDL2 dll的位置复制到可执行文件的位置。生成器表达式在构建时计算。我的项目既有库又有可执行文件。当我尝试将它添加到我的dll位置时,它不起作用。

if (CMAKE_SYSTEM_NAME STREQUAL "Linux")
    add_custom_command(
        TARGET yourgame POST_BUILD
        COMMAND "${CMAKE_COMMAND}" -E copy_if_different "$<TARGET_FILE:SDL2::SDL2>" "$<TARGET_FILE_DIR:your_executable_name>"
        VERBATIM
    )
endif()

我的项目既有库又有可执行文件,但上面的方法在我尝试将复制添加到库时不起作用。

第二种方法要求我将所有二进制文件移到它们自己的文件夹*"${CMAKE_BINARY_DIR}/bin"。这种方法将在不考虑平台的情况下将二进制文件放在同一个文件夹中,从而实现更具跨平台友好性的体验。您的程序资源的路径将保持不变。无需生成器表达式。我将以下内容添加到我的基本CMakeLists.txt*文件中。

set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
英文:

Problem solved. Problem has nothing to do with pkg-config.

When developing cross-platform applications I must remember to keep in mind that Windows doesn't have dedicated locations for libraries and executables unlike Linux.

Through my journey I have come up with 2 solutions. The second solution is much easier and cross-platform friendly but for completeness I will include both.

The first requires adding the following to wherever your add_executable() function is called. It copies the location of the SDL2 dll to the location of your executable usinggenerator expressions "$<>". Generator expressions are evaluated at build time. My project has both a library and an executable. It didn't work when I tried to add it to the location of my dll.

if (CMAKE_SYSTEM_NAME STREQUAL &quot;Linux&quot;)
    add_custom_command(
        TARGET yourgame POST_BUILD
        COMMAND &quot;${CMAKE_COMMAND}&quot; -E copy_if_different &quot;$&lt;TARGET_FILE:SDL2::SDL2&gt;&quot; &quot;$&lt;TARGET_FILE_DIR:your_executable_name&gt;&quot;
        VERBATIM
    )
endif()

My project has both a library and an executable, but the above didn't work when I tried to add copy to my library.

The second method requires me to move all my binaries to their own folder "${CMAKE_BINARY_DIR}/bin". This method will put your binaries in the same folder regardless of platform, allowing for a more cross-platform friendly experience. Your path to your program's assets will be identitcal. No generator expressions required. I added the below to my base CMakeLists.txt file.

set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)

huangapple
  • 本文由 发表于 2023年4月10日 19:36:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/75976743.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定