1

I'm trying to get OpenGL to work in CLion, downloaded the bin/include/library files from freeglut and copied them to minGW. They are there, and CLion is using that minGW folder when compiling, however when it tries to compile the test code that freeglut provides, it returns with "fatal error: GL/glut.h: No such file or directory #include <GL/glut.h>"

I have already tried different versions of CMakeLists, all of them return with the same issue. If I copy the GL folder next to the project, I get loads of undefined references.

After trying this CMakeList, I get the same error. (from Running Opengl program with CLion IDE)

cmake_minimum_required(VERSION 3.14)
project(gltest)

set(CMAKE_CXX_STANDARD 14)
set (CMAKE_CXX_FLAGS "-lglut")
set (CMAKE_CXX_FLAGS "-lGL")
set (CMAKE_CXX_FLAGS "-lGLU")
include_directories(GL/)

add_executable(gltest Callbacks.c HelloGLUT.c)

target_link_libraries(gltest -lglut -lGL -lGLU)

UPDATE

After using a new CMakeList(as suggested in the comments, im more than grateful to you wonderful people here) I get some new errors, but CLion seems to be able to reach the GL libraries now.

The CMakeList:

cmake_minimum_required(VERSION 3.14)
project(gltest)

set(SOURCE_FILES Callbacks.c HelloGLUT.c)
set(OPENGL_INCLUDE_DIR C:/minGW/mingw64/include)
set(OPENGL_LIBRARY C:/minGW/mingw64/lib)

find_package(OPENGL REQUIRED)

add_executable(gltest ${SOURCE_FILES})
include_directories(${OPENGL_INCLUDE_DIR})
target_link_libraries(gltest ${OPENGL_LIBRARY} )

Errors: CMakeFiles\gltest.dir/objects.a(Callbacks.c.obj): In function "glutInit_ATEXIT_HACK': C:/minGW/mingw64/include/GL/freeglut_std.h:637: undefined reference to "__imp___glutInitWithExit" CMakeFiles\gltest.dir/objects.a(Callbacks.c.obj): In function "glutCreateWindow_ATEXIT_HACK": And several more undefined references in the same manner to various "__imp___glut" functions. Any ideas?

If I use target_link_libraries(gltest ${OPENGL_LIBRARY} glut GL GLU) I get "C:/minGW/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/../../../../x86_64-w64-mingw32/bin/ld.exe: cannot find -lglut " , same for lGL and lGLU

M.Zergi
  • 17
  • 4
  • Is the mismatch of `${OPENGL_gl_LIBRARY}` and `${OPENGL_glu_LIBRARY}` intentional? – πάντα ῥεῖ Jun 20 '19 at 08:38
  • I copied it from here https://www.alexwidener.com/opengl-with-cmake.html as I figured it would come in handy if I saw some actual feedback about what is happening, so I guess it's intentional (I'm still new to making CMakes). Now i have tried another CMakeList, which gives the same error. – M.Zergi Jun 20 '19 at 08:45
  • OPENGL_gl_LIBRARY is a legacy thing, may be its not present? https://cmake.org/cmake/help/v3.10/module/FindOpenGL.html – Swift - Friday Pie Jun 20 '19 at 09:03
  • *"copied them to minGW"* - you should've copied them elsewhere and then specify their locations. – user7860670 Jun 20 '19 at 09:04
  • 1
    I'm no expert but the obvious questions are what's the value of `${OPENGL_INCLUDE_DIR}` and what's the path where you've placed the OpenGL header files? Your error can only be caused by having one or other of those things wrong, but you don't say what they are in your question. – john Jun 20 '19 at 09:10
  • Thank you, specifying the paths in ```set(OPENGL_INCLUDE_DIR C:/minGW/mingw64/include)``` and ```set(OPENGL_LIBRARY C:/minGW/lib)``` solved a part of the problem. Now I'm getting "undefined reference to "__imp___glutInitWithExit' CMakeFiles\gltest.dir/objects.a(Callbacks.c.obj): In function "glutCreateWindow_ATEXIT_HACK': " and 7 more undefined references to ___imp ... functions – M.Zergi Jun 20 '19 at 09:22
  • You might want to get rid of the '-l' in the target_link_libraries. CMake adds them automagically, i.e. instead of '-lglut' write 'glut'. – nada Jun 20 '19 at 09:25
  • @M.Zergi Could be a 32/64 bit issue. Try linking with `glut64` – john Jun 20 '19 at 09:27
  • Variable `OPENGL_LIBRARY` should be set to the **library** file, not to the *directory*. Actually, this variable is set by the `find_package(OpenGL)` call. There is no reason to set this variable manually and do not use it in the code. – Tsyvarev Jun 20 '19 at 09:30
  • @john same issue with glut64:/ – M.Zergi Jun 20 '19 at 10:01
  • @Tsyvarev what do you mean under "library file"? – M.Zergi Jun 20 '19 at 10:01
  • Library is a **file**. E.g. `C:/minGW/lib/libGL.dll`. When link with a library, exactly the library file is used. And the variable `OPENGL_LIBRARY` is expected to be assigned with a **file**. But you attempt to assign a **directory** `C:/minGW/lib` to that variable. This is wrong: it is impossible to link with a directory. – Tsyvarev Jun 20 '19 at 10:07
  • @Tsyvarev I edited it to `set(OPENGL_LIBRARY C:/minGW/mingw64/bin/freeglut.dll)` , but i still get the same errors (that's the only dll i found, the rest are .a files for example libglu32.a) – M.Zergi Jun 20 '19 at 10:32
  • `.a` files are libraries too. Moreover, on Windows linking with shared libraries is performed via `.lib` or `.dll.a` or, maybe, via `.a` files but not via `.dll` one. The example in my previous comment wasn't so good. But in any case, setting `OPENGL_LIBRARY` variable without using it has a little sence. For the purpose for linking 3d-party libraries you may find this question (and its answers) useful: https://stackoverflow.com/questions/8774593/cmake-link-to-external-library – Tsyvarev Jun 20 '19 at 11:25
  • It is now working, thank you for all your comments! – M.Zergi Jun 20 '19 at 15:26

1 Answers1

-1

SOLUTION FOR THOSE WHO RUN INTO THE SAME ERRORS

Make sure you follow the readme and have freeglut installed in a DIFFERENT folder inside mingw (for me it is C:/minGW/mingw64/freeglut) and copy freeglut's include and lib there. Make sure you have a FindFREEGLUT.cmake file (i pulled mine from https://git.omkov.net/Jamozed/Chip8Emulator/releases , if you have a download where it is only the FindFREEGLUT.cmake feel free to post it). Copy the FindFREEGLUT.cmake into your project's directory like "C:/CLionProjects/gltest/cmake/FindFREEGLUT.cmake". After this, I used the following CMakeLists.txt

cmake_minimum_required(VERSION 3.14)
project(gltest)

set(SOURCE_FILES Callbacks.c HelloGLUT.c)
set(GLUT_INCLUDE_DIR "C:/minGW/mingw64/freeglut/include")
set(OPENGL_LIBRARY_DIR "C:/minGW/mingw64/freeglut/lib")

# GLUT
find_package(GLUT REQUIRED)
include_directories(${GLUT_INCLUDE_DIR})
if(NOT GLUT_FOUND)
    message(ERROR "GLUT not found!")
endif(NOT GLUT_FOUND)

# OpenGL
find_package(OpenGL REQUIRED)
include_directories(${OpenGL_INCLUDE_DIRS})
if(NOT OPENGL_FOUND)
    message(ERROR "OPENGL not found!")
endif(NOT OPENGL_FOUND)


add_executable(gltest ${SOURCE_FILES})
target_link_libraries(gltest ${OPENGL_LIBRARIES} ${GLUT_LIBRARY})
M.Zergi
  • 17
  • 4
  • [Add additional information at your question](https://stackoverflow.com/posts/56682058/edit) please. This doesn't answer your question. – πάντα ῥεῖ Jun 20 '19 at 09:43
  • @πάνταῥεῖ 's reply is to an old version of this comment (which I posted when I still didn't know that you shouldn't post updates in separate comments, sorry I'm new to this:) ) .The new comment is working. – M.Zergi Jun 20 '19 at 15:19