Issue
my glfw3 was built from source and a FindGLFW.cmake was used to find the library:
find_path(
GLFW_INCLUDE_DIR
NAMES
GLFW
PATHS
include)
find_library(
GLFW_LIBRARY
NAMES
glfw glfw3 glfw3dll
PATHS
lib)
include(FindPackageHandleStandardArgs)
find_package_handle_standard_args(GLFW REQUIRED_VARS GLFW_LIBRARY GLFW_INCLUDE_DIR)
I am able to use find_package(GLFW REQUIRED)
and target_link_libraries(xxx glfw3)
in other projects with no build error. However, the statement inside if(TARGET glfw)
or if(TARGET glfw3)
always gives me false result. What could be the reason?
Example:
find_package(GLFW REQUIRED)
MESSAGE(STATUS "GLFW: ${GLFW_FOUND}")
if(TARGET glfw)
MESSAGE(STATUS "Found it")
else()
MESSAGE(STATUS "Not Found")
endif()
target_link_libraries(xxx glfw3)
This gives me GLFW: TRUE
and Not Found
output, but no build error.
Solution
Your FindGLFW.cmake
file is insufficient. In fact, it shouldn't be needed or exist at all. GLFW provides a config package already. Here's how you should build and use any well-behaved CMake package:
First, get the sources, build the package, and install it to a local prefix:
$ git clone https://github.com/glfw/glfw
Cloning into 'glfw'...
remote: Enumerating objects: 27050, done.
remote: Counting objects: 100% (96/96), done.
remote: Compressing objects: 100% (66/66), done.
remote: Total 27050 (delta 40), reused 54 (delta 24), pack-reused 26954
Receiving objects: 100% (27050/27050), 13.27 MiB | 2.87 MiB/s, done.
Resolving deltas: 100% (19047/19047), done.
$ cd glfw/
glfw$ cmake -G Ninja -DCMAKE_BUILD_TYPE=Release -S . -B build
...
-- Generating done
-- Build files have been written to: /path/to/glfw/build
glfw$ cmake --build build
...
glfw$ cmake --install build --prefix install
-- Install configuration: "Release"
-- Installing: /path/to/glfw/install/lib/libglfw3.a
-- Installing: /path/to/glfw/install/include/GLFW
-- Installing: /path/to/glfw/install/include/GLFW/glfw3.h
-- Installing: /path/to/glfw/install/include/GLFW/glfw3native.h
-- Installing: /path/to/glfw/install/lib/cmake/glfw3/glfw3Config.cmake
-- Installing: /path/to/glfw/install/lib/cmake/glfw3/glfw3ConfigVersion.cmake
-- Installing: /path/to/glfw/install/lib/cmake/glfw3/glfw3Targets.cmake
-- Installing: /path/to/glfw/install/lib/cmake/glfw3/glfw3Targets-release.cmake
...
Then create a folder for your example project and put your CMakeLists.txt in there:
glfw$ mkdir ../glfw-test ; cd ../glfw-test
glfw-test$ vim CMakeLists.txt
glfw-test$ cat CMakeLists.txt
cmake_minimum_required(VERSION 3.20)
project(example)
find_package(glfw3 REQUIRED)
message(STATUS "GLFW: ${glfw3_FOUND}")
if (TARGET glfw)
message(STATUS "Target exists")
else ()
message(STATUS "Target does not exist")
endif()
Then you can build it and use the <pkg>_ROOT
variable to tell CMake where to find glfw3
. If you had the package installed system-wide, you wouldn't need to set this variable.
glfw-test$ cmake -S . -B build -Dglfw3_ROOT=/path/to/glfw/install
...
-- GLFW: 1
-- Target exists
-- Configuring done
-- Generating done
-- Build files have been written to: /path/to/glfw-test/build
Answered By - Alex Reinking