-1

I'm attempting to build a CMakeLists.txt for my project. It needs to include SDL, OpenGL, and GLEW as well as all of my *.cpp and *.h files in different directories. All files used to build the project are in the Project Source Directory that cmake is called in.

The error:

In file included from /home/me/repos/AlchemyEngine/alchemy/alcEngine.cpp:1:0:
/home/me/repos/AlchemyEngine/alchemy/alcEngine.h:69:18: fatal error: glew.h: No such file or directory
 #include <glew.h>

I think it'd be easiest to show the steps for setting this project up in Visual Studio (which works as I used to develop from there) and hope I can get help recreating the setup in my CMakeLists.txt file.

Setting up the project in Visual Studio

1. Includes

Project -> *ProjectName* Properties -> VC++ Directories
Click Include Directories -> Click down-arrow- > edit

Browse for the include files (...\SDL2-OpenGL-GLEW-VC++\include)

2. Libraries

Project -> *ProjectName* Properties -> VC++ Directories
Click Library Directories -> Click down-arrow -> edit

Browse for the library files (...\SDL2-OpenGL-GLEW-VC++\lib)

3. Linkers

Project -> *ProjectName* Properties -> Linker -> Input
Click Additional Dependencies -> Click down arrow -> edit

Copy into box:
SDL2.lib
SDL2main.lib
SDL2_image.lib
OpenGL32.lib
glu32.lib
glew32.lib

4. DLLs

Build project with chosen entry point to create Debug folder with executable.

Copy the .dll files from the ...\SDL-OpenGL-GLEW-VC++\dropInExe folder and paste into the
Debug folder with the project executable
(...\*Project Folder*\Debug)

Here is my CMakeLists.txt file as it stands

cmake_minimum_required (VERSION 3.5)
project (AlchemyEngine)

set(CMAKE_BINARY_DIR ${CMAKE_SOURCE_DIR}/build/bin)
set(EXECUTABLE_OUTPUT_PATH ${CMAKE_BINARY_DIR})
set(LIBRARY_OUTPUT_PATH ${CMAKE_BINARY_DIR})

include_directories (
        alchemy/
        alchemy/event/
        alchemy/scene/
        alchemy/resource/
        alchemy/render/
        alchemy/util/
        game/
        #SDL2_OpenGL_GLEW_VC++/include
)

set( srcs
        alchemy/alcEngine.cpp
        alchemy/event/alcEvent.cpp
        alchemy/render/alcRenderer.cpp
        alchemy/render/alcSprite.cpp
        alchemy/resource/alcResource.cpp
        alchemy/resource/alcShader.cpp
        alchemy/resource/alcTexture.cpp
        alchemy/scene/alcObject.cpp
        alchemy/scene/alcScene.cpp
        alchemy/util/alcBlockAllocator.cpp
        alchemy/util/alcStackAllocator.cpp
        game/Player.cpp
        Main.cpp
)

add_library (libraries STATIC SDL2_OpenGL_GLEW_VC++/include/)

add_executable (alc ${PROJECT_SOURCE_DIR}/${srcs})

target_link_libraries(alc libraries)

set_target_properties(libraries PROPERTIES LINKER_LANGUAGE CXX)
set_target_properties(alc PROPERTIES LINKER_LANGUAGE CXX)

After configuration and generation, $CMAKE_SOURCE_DIR/build/bin contains:

liblibraries.a  
glew32.dll  
libpng16-16.dll  
SDL2.dll  
SDL2_image.dll  
zlib1.dll

It seems to me that my method for including the header files is incorrect, but poking around the CMake documentation and other stack exchange posts hasn't produced any better results. I have a feeling it has to do with the linker step, but I cannot figure out how to specify the dependencies in CMake.

I can tree and post the output too if the project structure is that important for this issue.

FINAL EDIT:

The error was fixed in the answer below, here is my final CMakeLists.txt for anyone interested

# Outputs executable(s) in {PROJECT_SOURCE_DIR}/build/bin/

# specify min version and project name
cmake_minimum_required (VERSION 3.5)
project (AlchemyEngine)

# set output paths for binaries/executable
set(CMAKE_BINARY_DIR ${CMAKE_SOURCE_DIR}/build/bin)
set(EXECUTABLE_OUTPUT_PATH ${CMAKE_BINARY_DIR})
set(LIBRARY_OUTPUT_PATH ${CMAKE_BINARY_DIR})

# include project header files
include_directories (
        alchemy/
        alchemy/event/
        alchemy/scene/
        alchemy/resource/
        alchemy/render/
        alchemy/util/
        game/
)

# create source files definition
set( srcs
        alchemy/alcEngine.cpp
        alchemy/event/alcEvent.cpp
        alchemy/render/alcRenderer.cpp
        alchemy/render/alcSprite.cpp
        alchemy/resource/alcResource.cpp
        alchemy/resource/alcShader.cpp
        alchemy/resource/alcTexture.cpp
        alchemy/scene/alcObject.cpp
        alchemy/scene/alcScene.cpp
        alchemy/util/alcBlockAllocator.cpp
        alchemy/util/alcStackAllocator.cpp
        game/Player.cpp
        Main.cpp
)

# add a library target to be built from the source files
add_library (libraries STATIC SDL2_OpenGL_GLEW_VC++/lib/)
# specify include directories
target_include_directories(libraries PUBLIC SDL2_OpenGL_GLEW_VC++/include/)
# specify libraries to use when linking the library
target_link_libraries (libraries 
        ${PROJECT_SOURCE_DIR}/SDL2_OpenGL_GLEW_VC++/lib/glew32.lib
        ${PROJECT_SOURCE_DIR}/SDL2_OpenGL_GLEW_VC++/lib/glew32s.lib
        ${PROJECT_SOURCE_DIR}/SDL2_OpenGL_GLEW_VC++/lib/SDL2_image.lib
        ${PROJECT_SOURCE_DIR}/SDL2_OpenGL_GLEW_VC++/lib/SDL2.lib
        ${PROJECT_SOURCE_DIR}/SDL2_OpenGL_GLEW_VC++/lib/SDL2main.lib
)

# create the executable using the source files defined above
add_executable (alc ${PROJECT_SOURCE_DIR}/${srcs})
# specify the headers
target_include_directories(alc PUBLIC ${PROJECT_SOURCE_DIR})
# link the library to the project
target_link_libraries(alc libraries)

# force the linker language because there's some c stuff in the library files
set_target_properties(libraries PROPERTIES LINKER_LANGUAGE CXX)
set_target_properties(alc PROPERTIES LINKER_LANGUAGE CXX)
Ausche
  • 139
  • 2
  • 12
  • 1
    Do you want to **include directory** in CMake? - Look [this question](https://stackoverflow.com/questions/13703647/how-to-properly-add-include-directories-with-cmake). Do you want to work with glew library in an elegant way? Look [that question](https://stackoverflow.com/questions/27472813/linking-glew-with-cmake). – Tsyvarev Jun 13 '19 at 20:18
  • @Tsyvarev I spent a few hours looking at the first link you posted earlier today, namely working with the `target_include_directories` and `target_link_libraries` for getting the library files set up right, but nothing worked same error. – Ausche Jun 13 '19 at 20:29
  • I think my issue has less to do with glew specifically and more to do with the way my files are being linked/included. Looking at my `depend.make` shows that only my files are included -- none of the libraries are listed as dependencies. – Ausche Jun 13 '19 at 20:33
  • 1
    Please, show your attempt when following the first question and the error you got. You current code is such ... a nonsence. – Tsyvarev Jun 13 '19 at 21:16
  • Did you remember to define `srcs` in the updated `CMakeLists.txt`? Did you create a function called `main()`? Why are you setting the `LINKER_LANGUAGE`? CMake will usually figure out what to do unless you are overriding it for a reason. – fdk1342 Jun 16 '19 at 17:40
  • @Fred Added srcs back and got the same error -- my mistake. My program does indeed have a main. `LINKER_LANGUAGE` was set because my libraries include some c files and would throw an error regarding the language. – Ausche Jun 17 '19 at 13:32

1 Answers1

2

target_include_directories(libraries PRIVATE SDL2_OpenGL_GLEW_VC++/include/) should be using PUBLIC so that this path is known to any executable target that links against libraries such as target_link_libraries(alc libraries).

This is assuming that glew.h resides in the SDL2_OpenGL_GLEW_VC++/include/ directory.

Since target_include_directories is using a relative path (I'm pretty sure it is relative to the source tree) you need to make sure that this is correct for your directory tree.

fdk1342
  • 3,274
  • 1
  • 16
  • 17
  • Thank you that solved the `fatal error: glew.h: No such file or directory` issue! I had read about using `PRIVATE` but did not realize `PUBLIC` essentially does both `PRIVATE` and `INTERFACE`. – Ausche Jun 17 '19 at 16:48