How to build GLEW as part of the CMake project

3.9k views Asked by At

I'm making a cmake project that uses GLEW. I want to build GLEW on the fly (not just link against a compiled library). I use add_subdirectory to add 3rd party libraries to my project. Here's what my CMakeLists.txt looks like for now:

cmake_minimum_required (VERSION 2.6)
project (quokka)

#configure_file (
#  "${CMAKE_CURRENT_SOURCE_DIR}/TutorialConfig.h.in"
#  "${CMAKE_CURRENT_SOURCE_DIR}/TutorialConfig.h"
#  )

include_directories(${CMAKE_SOURCE_DIR}/../3rd_party/glew/include)
include_directories(${CMAKE_SOURCE_DIR}/../3rd_party/glfw/include)
include_directories(${CMAKE_SOURCE_DIR}/../3rd_party/freetype-2.6.2/include)
include_directories(${CMAKE_SOURCE_DIR})

set(CMAKE_BINARY_DIR ${CMAKE_SOURCE_DIR}/../../temp)
set(EXECUTABLE_OUTPUT_PATH ${CMAKE_BINARY_DIR})
set(LIBRARY_OUTPUT_PATH ${CMAKE_BINARY_DIR})

SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS_DEBUG} -Wall -std=c++11")

if(CMAKE_BUILD_TYPE STREQUAL "Debug")
    set(OUTPUT_LIB_DIRECTORIES ${CMAKE_CURRENT_SOURCE_DIR}/../../temp/Debug)
endif ()

if(CMAKE_BUILD_TYPE STREQUAL "Release")
    set(OUTPUT_LIB_DIRECTORIES ${CMAKE_CURRENT_SOURCE_DIR}/../../temp/Release)
endif ()

set(GLFW ${OUTPUT_LIB_DIRECTORIES}/glfw)
set(FREETYPE ${OUTPUT_LIB_DIRECTORIES}/freetype)
set(GLEW ${OUTPUT_LIB_DIRECTORIES}/glew)

add_subdirectory(${CMAKE_SOURCE_DIR}/../3rd_party/glfw/ ${CMAKE_CURRENT_SOURCE_DIR}/../../temp/lib/glfw) 
add_subdirectory(${CMAKE_SOURCE_DIR}/../3rd_party/glew/build/cmake ${CMAKE_CURRENT_SOURCE_DIR}/../../temp/lib/glew) 
add_subdirectory(${CMAKE_SOURCE_DIR}/../3rd_party/freetype-2.6.2/ ${CMAKE_CURRENT_SOURCE_DIR}/../../temp/lib/freetype) 
add_subdirectory(Input)
add_subdirectory(Process)
add_subdirectory(Application)
add_subdirectory(View)
add_subdirectory(GameLogic)
add_subdirectory(UID)
add_subdirectory(Actor)
add_subdirectory(AssetManager)
add_subdirectory(GUI)
add_subdirectory(GraphicsEngine)

add_executable(quokka main.cpp)

target_link_libraries(quokka Input ProcessLibrary Application View GameLogic UID Actor AssetManager GUI GraphicsEngine)

target_link_libraries(quokka glfw freetype)

if (UNIX)
   target_link_libraries(quokka glew Xrandr Xinerama Xi Xcursor X11 Xxf86vm pthread GL)
endif (UNIX)

if (WIN32)
   target_link_libraries(quokka glew32 opengl32)
endif (WIN32)

As you can see, I specify the output directory (${CMAKE_CURRENT_SOURCE_DIR}/../../temp/lib/glfw) for 3rd party libraries and it works well for GLFW and freetype but GLEW ends up somewhere else. So, I generate Visual Studio projects with this set up and for some reason output directory for GLEW is C:\_work\quokka_plus\temp\VSProjects\bin\Debug\. As a result, when I build my project it cannot find glew32.lib. Why does CMake act differently for GLEW than it does for GLFW and freetype and how to make it place compiled lib in a place I need without touching CMake sources of GLEW?

1

There are 1 answers

0
pixeloverflow On

To build the GLEW source code with cmake. You can create a folder on your cmake project folder named ./external and put the GLEW code into this folder.

For details please look at the tutorials from http://www.opengl-tutorial.org/

and runnable project source code: http://www.opengl-tutorial.org/download/

add_subdirectory (external)

include_directories(
    external/AntTweakBar-1.16/include/
    external/glfw-3.1.2/include/
    external/glm-0.9.7.1/
    external/glew-1.13.0/include/
    external/assimp-3.0.1270/include/
    external/bullet-2.81-rev2613/src/
    .
)

set(ALL_LIBS
    ${OPENGL_LIBRARY}
    glfw
    GLEW_1130
)

add_definitions(
    -DTW_STATIC
    -DTW_NO_LIB_PRAGMA
    -DTW_NO_DIRECT3D
    -DGLEW_STATIC
)

target_link_libraries(FOO
    ${ALL_LIBS}
)