이재원
이재원

Reputation: 87

install CMake package on windows visual studio problem (find_package)

I made a simple program using glfw in Linux. and I want to build it in windows now.
when I install glfw in Linux, I did the following steps.

  1. install CMake.
  2. download glfw source code.
  3. create a build folder in the source code folder.
  4. do "cmake ../" in the build folder
  5. do "make"
  6. do "make install"

Then in CMakeLists.txt file:

find_package( glfw3 3.3 REQUIRED )

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

in source code:

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

//use glfw

So I want to do the same thing in windows visual studio. I did the following steps.

  1. install CMake
  2. download glfw source file.
  3. create a build folder in the source code folder.
  4. do "cmake ../" in the build folder
  5. go to build folder, open GLFW project in visual studio using Administrator rights.
  6. build ALL_BUILD in visual studio.

As a result, I got C:\Program Files (x86)\GLFW folder. there is include, lib, config files.
And then I created a new CMake project.

CMake File:

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")

And error message saying:

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   

Questions.

  1. Why does include, lib files are installed directly in program files (x86)?
  2. How can I do "make install" in windows?

Upvotes: 0

Views: 3692

Answers (1)

vre
vre

Reputation: 6744

TL;DR answers:

  1. Because you did not specified an installation prefix. Add CMAKE_INSTALL_PREFIX to your GLFW CMake command, e.g.

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

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

If you do not specify an installation prefix to your cmake command on Windows it is set to C:\Program Files (x86) for 32bit builds and to C:\Program Files for 64bit builds.

Do not hardcode CMAKE_PREFIX_PATH into your CMakeLists.txt. Explicitly specify what generator and architecture you want to use for your build. Add it to your CMake command line as argument, e.g.

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

And your CMakeLists.txt file should look as follows:

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)

Upvotes: 1

Related Questions