CMake: improve linking OpenGL3 example programs (#959)

- Remove find_library(LIB_GLEW ...) from 'resources.cmake'

- Use the variable FLTK_GLEW_DIR to "find" a user supplied GLEW library
  for OpenGL3 example programs (examples/OpenGL3*.cxx).
  Both static and shared libs (DLLs) are supported, but the static
  library is preferred to avoid DLL dependencies.

- Define preprocessor macro 'GLEW_STATIC' by CMake compile definitions
  if and only if the static glew library (glew32s) was found and linked.
  The previous code defined 'GLEW_STATIC' unconditionally in the source
  code which led to undefined references if a shared lib (DLL) was used.
This commit is contained in:
Albrecht Schlosser 2024-05-01 17:31:17 +02:00
parent d1ea57bc7a
commit bc63ea7629
4 changed files with 71 additions and 29 deletions

View File

@ -152,14 +152,13 @@ endif((NOT APPLE) OR FLTK_BACKEND_X11)
find_library(LIB_freetype freetype)
find_library(LIB_GL GL)
find_library(LIB_MesaGL MesaGL)
find_library(LIB_GLEW NAMES GLEW glew32)
find_library(LIB_jpeg jpeg)
find_library(LIB_png png)
find_library(LIB_zlib z)
find_library(LIB_m m)
mark_as_advanced(LIB_dl LIB_fontconfig LIB_freetype)
mark_as_advanced(LIB_GL LIB_MesaGL LIB_GLEW)
mark_as_advanced(LIB_GL LIB_MesaGL)
mark_as_advanced(LIB_jpeg LIB_png LIB_zlib)
mark_as_advanced(LIB_m)

View File

@ -80,7 +80,7 @@ set(FLUID_SOURCES
)
############################################################
# examples requiring fltk_images
# examples requiring fltk::images
############################################################
set(IMAGE_SOURCES
@ -100,7 +100,7 @@ set(CAIRO_SOURCES
)
############################################################
# examples requiring OpenGL + libGLEW
# examples requiring OpenGL3 + GLEW
############################################################
set(OPENGL_SOURCES
@ -141,33 +141,79 @@ foreach(src ${CAIRO_SOURCES})
endforeach(src)
############################################################
# create example programs with OpenGL + libGLEW
# create example programs with OpenGL3 + GLEW
############################################################
#
# Note 1: macOS (Quartz) does not need GLEW, it's included in OpenGL
# Note 2: find_package(GLEW) finds either shared or static libs or both.
# Note 3: on Windows we set the variable GLEW_USE_STATIC_LIBS=TRUE because
# we *want* to find static libs but we *can* also use shared libs.
# Note 4: FindGLEW.cmake changed the library suffixes for MinGW in CMake 3.28.0,
# obviously "assuming" that ".lib" is Visual Studio only. There have been
# discussions about finding the "wrong" libraries since CMake 3.25 or so.
# Therefore the static lib "glew32s.lib" is not found if CMake 3.28 or
# higher is used (current version, as of this writing: 3.29.3). However,
# this assumption is probably not true for a pure C library (glew32s).
# This library *is* found and works well with CMake 3.15.0 - 3.27.9.
# Users may need to copy or rename "glew32s.lib" to "glew32s.a"
# if CMake 3.28 or higher is used.
# Albrecht-S, May 13, 2024
# Note: macOS does not need libGLEW
if(APPLE AND NOT FLTK_BACKEND_X11)
if(NOT LIB_GLEW)
set(LIB_GLEW TRUE)
if(OPENGL_FOUND)
if(WIN32)
set(GLEW_USE_STATIC_LIBS TRUE)
endif()
set(REQUIRED_LIBS fltk::gl)
set(_glew_lib GLEW::glew)
set(_glew_static FALSE)
if(APPLE AND NOT FLTK_BACKEND_X11) # macOS Quartz
set(_glew_lib)
set(GLEW_FOUND TRUE)
else()
set(REQUIRED_LIBS fltk::gl ${LIB_GLEW})
# set(GLEW_VERBOSE TRUE) # make `find_package(GLEW)` verbose
set(_CMAKE_PREFIX_PATH_SAVED ${CMAKE_PREFIX_PATH})
set(CMAKE_PREFIX_PATH ${FLTK_GLEW_DIR} ${CMAKE_PREFIX_PATH})
find_package(GLEW MODULE)
set(CMAKE_PREFIX_PATH ${_CMAKE_PREFIX_PATH_SAVED})
unset(_CMAKE_PREFIX_PATH_SAVED)
# Did we find the static lib? If yes, use it
if(TARGET GLEW::glew_s)
set(_glew_lib GLEW::glew_s)
set(_glew_static TRUE)
endif()
endif()
if(OPENGL_FOUND AND LIB_GLEW)
if(0) # Debug
fl_debug_var(OPENGL_FOUND)
fl_debug_var(GLEW_FOUND)
fl_debug_var(GLEW_DIR)
fl_debug_var(_glew_lib)
fl_debug_target(GLEW::glew)
fl_debug_target(GLEW::glew_s)
fl_debug_target(GLEW::GLEW)
endif() # /Debug
endif(OPENGL_FOUND)
if(OPENGL_FOUND AND (TARGET "${_glew_lib}" OR APPLE))
# GLEW was found, create the OpenGL3 targets:
foreach(tgt ${OPENGL_SOURCES})
fl_create_example(${tgt} ${tgt}.cxx "${REQUIRED_LIBS}")
fl_create_example(${tgt} ${tgt}.cxx "fltk::gl;${_glew_lib}")
set_property(TARGET ${tgt} PROPERTY CXX_STANDARD 11)
set_property(TARGET ${tgt} PROPERTY CXX_STANDARD_REQUIRED TRUE)
# define preprocessor macro GLEW_STATIC only if we link to the static lib
if(_glew_static)
target_compile_definitions(${tgt} PRIVATE "GLEW_STATIC")
endif()
endforeach(tgt)
else()
message(STATUS
"OpenGL or libGLEW not present: OpenGL example programs will not be built.")
fl_debug_var(OPENGL_FOUND)
fl_debug_var(LIB_GLEW)
message("")
endif(OPENGL_FOUND AND LIB_GLEW)
unset(REQUIRED_LIBS)
message(STATUS
"OpenGL or GLEW not present: OpenGL3 example programs will not be built.")
fl_debug_var(OPENGL_FOUND)
fl_debug_var(GLEW_FOUND)
message("")
endif() # (OPENGL_FOUND AND TARGET "${_glew_lib}")

View File

@ -19,11 +19,10 @@
# define GL_DO_NOT_WARN_IF_MULTI_GL_VERSION_HEADERS_INCLUDED 1
# include <OpenGL/gl3.h> // defines OpenGL 3.0+ functions
#else
# if defined(_WIN32)
# define GLEW_STATIC 1
# endif
// Note: GLEW_STATIC is defined by CMake if the static lib is linked
# include <GL/glew.h>
#endif
#include <FL/glut.H>
#include <FL/fl_ask.H>
#include <stdio.h>

View File

@ -25,9 +25,7 @@
#if defined(__APPLE__)
# include <OpenGL/gl3.h> // defines OpenGL 3.0+ functions
#else
# if defined(_WIN32)
# define GLEW_STATIC 1
# endif
// Note: GLEW_STATIC is defined by CMake if the static lib is linked
# include <GL/glew.h>
#endif
#include <FL/gl.h> // for gl_texture_reset()