2

I am somewhat new to cmake and completely new to glew, glfw, and the like. I'm following a youtube channel to learn more about game engines and programming.

My problem is linking the static glew library in my project using cmake.

First, I start with the glew source code from http://mcs.une.edu.au/doc/glew-devel/index.html.

I compile it by:

cd build; cmake ./cmake; make glew_s

This adds a lib directory into the build directory with libGLEW.a

A shortened version of my CMakeLists.txt looks like:

cmake_minimum_required (VERSION 3.5 FATAL_ERROR)
project (TestProject CXX)

set(CMAKE_CXX_FLAGS "-std=c++11")

###########################
# GLEW
###########################
add_subdirectory(${CMAKE_SOURCE_DIR}/Dependencies/GLEW)
target_link_libraries(${PROJECT_NAME} glew)

and in Dependencies/GLEW I have another CMakeLists.txt:

# Add glew source and header files  
file(GLOB_RECURSE glew-lib ${CMAKE_CURRENT_SOURCE_DIR}/lib/*)  
file(GLOB_RECURSE glew-headers ${CMAKE_CURRENT_SOURCE_DIR}/include/GL/*)

add_library(glew ${glew-lib} ${glew-headers})  
target_include_directories(glew PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}/include/GL")

I put a copy of the libGLEW.a file into the lib directory and the include directory is copied from the glew source code include directory. It holds the GL directory, which contains the header files glew.h, wglew.h, eglew.h, and glxew.h.

When I run cmake I get the error:

CMake Error: Cannot determine link language for target "glew".  
CMake Error: CMake can not determine linker language for target: glew

The glew source code also has a src directory with glew.c in it, but if I put it in the libs directory and include it in the Dependencies/GLEW/CMakeLists.txt like:

# Add glew source and header files  
file(GLOB_RECURSE glew-lib ${CMAKE_CURRENT_SOURCE_DIR}/lib/*.c)
file(GLOB_RECURSE glew-headers ${CMAKE_CURRENT_SOURCE_DIR}/include/GL/*)
add_library(glew ${glew-lib} ${glew-headers})
target_include_directories(glew PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}/include")

I get the error:

CMake Error: Error required internal CMake variable not set, cmake may
be not be built correctly.  
Missing variable is: CMAKE_C_COMPILE_OBJECT  
CMake Error: Error required internal CMake variable not set, cmake may
be not be built correctly.  
Missing variable is: CMAKE_C_CREATE_STATIC_LIBRARY

Lastly, I have tried just including the glew.c and headers in the root CMakeLists.txt like:

#########################
# GLEW  
#########################
include_directories("${CMAKE_SOURCE_DIR}/Dependencies/GLEW/lib/")
include_directories("${CMAKE_SOURCE_DIR}/Dependencies/GLEW/include/GL/")

Here cmake will finish, but it won't be able to compile, saying classes do not name a type and/or are not declared in this scope.

Any help would be appreciated. I was under the impression that the libGLEW.a was the static library, and all I would have to do is link and compile it along with the headers, but that did not work.

Tsyvarev
  • 60,011
  • 17
  • 110
  • 153
mjp5595
  • 21
  • 1
  • 3
  • If the file is called `libGLEW.a` you should tell CMake to link `GLEW` not `glew`. – John Zwinck Sep 23 '18 at 09:35
  • @JohnZwinck: In the first code snipped `glew` is a **target name**, which is created by the second code snippet. In CMake targets names could be any. – Tsyvarev Sep 23 '18 at 09:37
  • 1
    @mjp5595: Do you **already** have file `libGLEW.a` or not? If yes, no needs to build GLEW once more. If no, use the build process provided by the GLEW project (`make`, `make install`), not needs to (re)implement it in CMake. After obtaining `libGLEW.a` all you need is to adjust include directories for **your** project (with `include_directories`) and link with an existing library, as it is described in this question: https://stackoverflow.com/questions/8774593/cmake-link-to-external-library. – Tsyvarev Sep 23 '18 at 09:43
  • What OS are you using? – John Zwinck Sep 23 '18 at 10:06
  • @Tsyvarev Thank you. I did not use "make install", which was most of my problem! – mjp5595 Sep 25 '18 at 23:10

1 Answers1

0

First of all, I forgot to use

make install

after using make the first time, so I was using the incorrect libGLEW.a file.

After including the proper libGLEW.a file, my directory structure had

./Dependencies/GLEW/include/GL/*.h //header files ./Dependencies/GLEW/lib/libGLEW.a //source file

Finally, the TopLevel CMakeLists.txt is changed to include:

add_library(glew STATIC IMPORTED GLOBAL)

set_target_properties(glew PROPERTIES IMPORTED_LOCATION ${CMAKE_SOURCE_DIR}/Dependencies/GLEW/lib/libGLEW.a )
set_target_properties(glew PROPERTIES INCLUDE_DIRECTORIES ${CMAKE_SOURCE_DIR}/Dependencies/GLEW/include )

If I want to include the library from a lower CMakeLists.txt such as /Dependencies/GLEW/CMakeLists.txt Then I would have to first export the library from there and then import it at the topLevel CMakeLists.txt file.

Now, in order to use glew headers in my project I can just use #include .

mjp5595
  • 21
  • 1
  • 3