在 windows visual studio 问题上安装 CMake 包 (find_package)

install CMake package on windows visual studio problem (find_package)

我在 Linux 中使用 glfw 编写了一个简单的程序。我现在想在 windows 中构建它。
当我在 Linux 中安装 glfw 时,我执行了以下步骤。

  1. 安装 CMake。
  2. 下载 glfw 源代码。
  3. 在源代码文件夹中创建一个构建文件夹。
  4. 在构建文件夹中执行“cmake ../”
  5. 做“制作”
  6. 执行“安装”

然后在CMakeLists.txt文件中:

find_package( glfw3 3.3 REQUIRED )

add_executable(main main.cpp)
target_link_libraries(main glfw)

源代码中:

#define GLFW_INCLUDE_NONE
#include <GLFW/glfw3.h>

//use glfw

所以我想在 windows visual studio 中做同样的事情。 我做了以下步骤。

  1. 安装 CMake
  2. 下载glfw源文件。
  3. 在源代码文件夹中创建一个构建文件夹。
  4. 在构建文件夹中执行“cmake ../”
  5. 转到构建文件夹,使用管理员权限在 visual studio 中打开 GLFW 项目。
  6. 在 visual studio 中构建 ALL_BUILD。

结果,我得到了 C:\Program Files (x86)\GLFW 文件夹。有包含,库,配置文件。
然后我创建了一个新的 CMake 项目。

CMake 文件:

cmake_minimum_required (VERSION 3.8)

set (CMAKE_PREFIX_PATH "C:\Program Files (x86)\GLFW\lib\cmake\glfw3")

find_package( glfw3 3.3 REQUIRED )

include_directories( "C:\Program Files (x86)\GLFW" )

project ("glfw_test")

add_executable (glfw_test "glfw_test.cpp" "glfw_test.h")

错误消息说:

CMake Error at C:\Users\home\source\repos\glfw_test\CMakeLists.txt:3 (set):
  Syntax error in CMake code at

    C:/Users/home/source/repos/glfw_test/CMakeLists.txt:3

  when parsing string

    C:\Program Files (x86)\GLFW\lib\cmake\glfw3

  Invalid character escape '\P'.    glfw_test   C:\Users\home\source\repos\glfw_test\CMakeLists.txt 3   

问题。

  1. 为什么include、lib文件直接安装在program files (x86)中?
  2. 如何在 windows 中执行“make install”?

TL;DR 答案:

  1. 因为你没有指定安装前缀。 添加 CMAKE_INSTALL_PREFIX 到您的 GLFW CMake 命令,例如

cmake -S <sourcedir> -B <builddir> -DCMAKE_INSTALL_PRFIX=<yourinstalldir>

  1. cmake --build <builddir> --target install --config Release

如果您没有在 Windows 上为您的 cmake 命令指定安装前缀,则 32 位版本设置为 C:\Program Files (x86),64 位版本设置为 C:\Program Files

不要将 CMAKE_PREFIX_PATH 硬编码到您的 CMakeLists.txt 中。明确指定要用于构建的生成器和架构。将它作为参数添加到您的 CMake 命令行,例如

cmake -S <sourcedir> -B <builddir> -G "Visual Studio 16 2019" -A Win32 -DCMAKE_PREFIX_PATH=<yourglfwrootinstalldir>

您的 CMakeLists.txt 文件应如下所示:

cmake_minimum_required (VERSION 3.8)
project ("glfw_test")

find_package( glfw3 3.3 REQUIRED )
add_executable (glfw_test glfw_test.cpp glfw_test.h)
target_link_libraries(glfw_test PRIVATE glfw)