【问题标题】:CMake is unable to find SFML directories in Windows when creating a simple projectCMake在创建简单项目时无法在Windows中找到SFML目录
【发布时间】:2018-01-20 05:44:52
【问题描述】:

我最近下载了适用于 Windows 的 CMake,因为我想为 SFML 开发一个跨平台插件,并决定按照here 的指南来测试如何去做。根据指南,我在我的 Documents 目录中创建了一个名为 sfml_test_cmake(Windows 10)的文件夹,其中包含 CMakeLists.txtmain.cpp(具有 SFML 代码)和一个文件夹 cmake_modules,其中包含一个文件 @ 987654322@。 CMakeLists.txt 如下所示:

cmake_minimum_required(VERSION 3.9)
project(SFML-CmakeTest)

set(EXECUTABLE_NAME "sfml_test_cmake")

add_executable(${EXECUTABLE_NAME} main.cpp)

set(CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}/cmake_modules" ${CMAKE_MODULE_PATH})
find_package(SFML 2.4 REQUIRED system window graphics network audio)
target_link_libraries(${EXECUTABLE_NAME} ${SFML_LIBRARIES})

install(TARGETS ${EXECUTABLE_NAME} DESTINATION bin)

main.cpp,这是一个简单的测试程序,如下所示:

#include <SFML/Graphics.hpp>

int main()
{
    sf::RenderWindow testWindow{ sf::VideoMode{ 640, 480 }, "Test Window", sf::Style::Default };

    while (testWindow.isOpen())
    {
        sf::Event winEvent;
        while (testWindow.pollEvent(winEvent))
        {
            if (winEvent.type == sf::Event::Closed)
            {
                testWindow.close();
            }
        }
        testWindow.clear();
        testWindow.display();
    }
}

至于 SFML 的安装位置,它安装在我的 Documents 目录和我的 C:\ 驱动器中。为了最终构建代码,我打开了cmd.exe,在我的文件夹中为构建创建了一个新目录,然后运行了cmake ..。我得到的错误是:

CMake Error at cmake_modules/FindSFML.cmake:357 (message):
  Could NOT find SFML (missing: SFML_SYSTEM_LIBRARY SFML_WINDOW_LIBRARY
  SFML_GRAPHICS_LIBRARY SFML_NETWORK_LIBRARY SFML_AUDIO_LIBRARY)

在查看FindSFML.cmake 时,我发现了这个:

# define the list of search paths for headers and libraries
set(FIND_SFML_PATHS
    ${SFML_ROOT}
    $ENV{SFML_ROOT}
    ~/Library/Frameworks
    /Library/Frameworks
    /usr/local
    /usr
    /sw
    /opt/local
    /opt/csw
    /opt)

这告诉我,如果我在 Linux 上,Cmake 只会在 SFML 所在的目录中搜索,解释了为什么找不到目录。为了解决这个问题,我将函数更改为以下内容:

# define the list of search paths for headers and libraries
set(FIND_SFML_PATHS
    ${SFML_ROOT}
    $ENV{SFML_ROOT}
    ~/Library/Frameworks
    /Library/Frameworks
    /usr/local
    /usr
    /sw
    /opt/local
    /opt/csw
    /opt
    C:/)

但是,这次重建导致了同样的错误,让我感到困惑。我做错了什么?

【问题讨论】:

    标签: c++ windows cmake sfml


    【解决方案1】:

    不要修改原来的 FindSFML.cmake。相反,定义一个变量CMAKE_PREFIX_PATH,其中包含路径,安装在您的计算机上的SFML 应该在哪里。将“C:\”添加到搜索路径列表中不起作用,cmake 不会递归搜索指定路径中的所有目录。

    【讨论】:

    • 但是,我遇到了另一个错误,这次是在构建的解决方案中。我按如下方式运行 CMake:cmake .. -DCMAKE_PREFIX_PATH=C:/SFML-2.4.2。但是,当我构建解决方案时,我收到错误 Cannot open include file SFML/Graphics.hpp: No such file or directory`。不过你的答案是正确的。
    • 你应该在你的cmake中添加target_include_directories(${EXECUTABLE_NAME} ${SFML_INCUDE_DIR})命令..
    • 那行不通。我收到一个错误,传递给target_include_directories 的参数数量不正确。查看文档,我发现它可能应该只是include_directories,尽管我不确定。现在代码构建了,但我得到了与第二条评论相同的错误。
    • target_include_directoriesinclude_directories 是不同的命令。你可能会用后者实现你的目标,但我认为这里推荐target_include_directories。查看文档:cmake.org/cmake/help/v3.3/command/…
    • 我仍然不确定命令是什么。 (我对此很陌生)
    猜你喜欢
    • 2017-09-13
    • 2011-04-11
    • 2010-09-21
    • 1970-01-01
    • 1970-01-01
    • 2020-03-12
    • 1970-01-01
    相关资源
    最近更新 更多