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



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

  1. 安装CMake。
  2. 下载glfw源代码。
  3. 在源代码文件夹中创建一个构建文件夹。
  4. do "cmake ./"在构建文件夹
  5. 做"老爷
  6. do "make install">

然后在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. do "cmake ./"在构建文件夹
  5. 进入构建文件夹,使用管理员权限在visual studio中打开GLFW项目。
  6. 在visual studio中构建ALL_BUILD。

结果,我得到了C:Program Files (x86)GLFW文件夹。有include, lib, config文件。
然后我创建了一个新的CMake项目。

CMake文件:

cmake_minimum_required (VERSION 3.8)
set (CMAKE_PREFIX_PATH "C:Program Files (x86)GLFWlibcmakeglfw3")
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:Usershomesourcereposglfw_testCMakeLists.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)GLFWlibcmakeglfw3
Invalid character escape 'P'.    glfw_test   C:Usershomesourcereposglfw_testCMakeLists.txt 3   

问题。

  1. 为什么包括,lib文件直接安装在程序文件(x86)?
  2. 我怎么做"安装"?在windows ?

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)

相关内容