创建简单项目时,CMake无法在Windows中找到SFML目录 [英] CMake is unable to find SFML directories in Windows when creating a simple project

查看:118
本文介绍了创建简单项目时,CMake无法在Windows中找到SFML目录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近下载了适用于Windows的CMake,因为我想为SFML开发一个跨平台的插件,并决定遵循 FindSFML.cmake . CMakeLists.txt 如下所示:

I recently downloaded CMake for Windows, since I wanted to develop a cross-platform plugin for SFML, and decided to test out how to do so by following a guide, which is here. As per the guide, I created a folder in my Documents directory called sfml_test_cmake (Windows 10) with a CMakeLists.txt, main.cpp (which has the SFML code), and a folder cmake_modules which would hold a file FindSFML.cmake. The CMakeLists.txt looks as follows:

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 (这是一个简单的测试程序)如下:

and the main.cpp, which is a simple test program looks as follows:

#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 .. .我得到的错误是:

As for where SFML is installed, it is installed in both my Documents directory, and my C:\ drive. To finally build the code, I opened cmd.exe, created a new directory inside my folder for the build, and ran cmake ... The errors I got were:

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 时,我发现了这一点:

Upon looking in FindSFML.cmake, I found this:

# 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所在的目录,并解释为什么找不到目录.为了解决该问题,我将功能更改为以下内容:

Which tells my that Cmake is only searching in the directories it SFML would be in if I was on Linux, explaining why the directories couldn't be found. To fix the problem, I changed the function to the following:

# 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:/)

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

However, this time rebuilding resulted in the same error, leaving my confused. What am I doing wrong?

推荐答案

请勿修改原始的FindSFML.cmake.而是定义一个变量 CMAKE_PREFIX_PATH ,该变量包含路径,其中SFML应该安装在您的计算机上.在搜索到的路径列表中添加"C:\"将不起作用,cmake不会递归搜索指定路径中的所有目录.

Do not modify the original FindSFML.cmake. Instead, define a variable CMAKE_PREFIX_PATH which contains the paths, where SFML installed on your computer should be. Adding "C:\" to the list of searched paths would not work, cmake won't search recursively all directories in the specified path.

这篇关于创建简单项目时,CMake无法在Windows中找到SFML目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆