-1

I want to learn how to use the OpenGL library with C++ but I am struggling getting the external libraries to import. I am using CLion as my IDE and MinGW as my compiler. Here is my CMake code:

cmake_minimum_required(VERSION 3.17)
project(compgeo)
set(CMAKE_CXX_STANDARD 14)
add_executable(compgeo main.cpp)
find_package(GLUT REQUIRED)
find_package(OpenGL REQUIRED)
include_directories( ${OPENGL_INCLUDE_DIRS} ${GLUT_INCLUDE_DIRS} )

And here is my code that I am trying to test:

/*
 * GL01Hello.cpp: Test OpenGL C/C++ Setup
 */
#include <windows.h>  // For MS Windows
#include <GL/glut.h>  // GLUT, includes glu.h and gl.h

/* Handler for window-repaint event. Call back when the window first appears and
   whenever the window needs to be re-painted. */
void display() {
    glClearColor(0.0f, 0.0f, 0.0f, 1.0f); // Set background color to black and opaque
    glClear(GL_COLOR_BUFFER_BIT);         // Clear the color buffer

    // Draw a Red 1x1 Square centered at origin
    glBegin(GL_QUADS);              // Each set of 4 vertices form a quad
    glColor3f(1.0f, 0.0f, 0.0f); // Red
    glVertex2f(-0.5f, -0.5f);    // x, y
    glVertex2f( 0.5f, -0.5f);
    glVertex2f( 0.5f,  0.5f);
    glVertex2f(-0.5f,  0.5f);
    glEnd();

    glFlush();  // Render now
}

/* Main function: GLUT runs as a console application starting at main()  */
int main(int argc, char** argv) {
    glutInit(&argc, argv);                 // Initialize GLUT
    glutCreateWindow("OpenGL Setup Test"); // Create a window with the given title
    glutInitWindowSize(320, 320);   // Set the window's initial width & height
    glutInitWindowPosition(50, 50); // Position the window's initial top-left corner
    glutDisplayFunc(display); // Register display callback handler for window re-paint
    glutMainLoop();           // Enter the infinitely event-processing loop
    return 0;
}

These are the errors I am getting. The issue comes around once it links the CXX executable:

[ 50%] Linking CXX executable compgeo.exe
CMakeFiles\compgeo.dir/objects.a(main.cpp.obj): In function `glutInit_ATEXIT_HACK':
c:/mingw/include/gl/glut.h:486: undefined reference to `__glutInitWithExit@12'
CMakeFiles\compgeo.dir/objects.a(main.cpp.obj): In function `glutCreateWindow_ATEXIT_HACK':
c:/mingw/include/gl/glut.h:503: undefined reference to `__glutCreateWindowWithExit@8'
CMakeFiles\compgeo.dir/objects.a(main.cpp.obj): In function `glutCreateMenu_ATEXIT_HACK':
c:/mingw/include/gl/glut.h:549: undefined reference to `__glutCreateMenuWithExit@8'
CMakeFiles\compgeo.dir/objects.a(main.cpp.obj): In function `Z7displayv':
A:/CppProjects/compgeo/main.cpp:10: undefined reference to `glClearColor@16'
A:/CppProjects/compgeo/main.cpp:11: undefined reference to `glClear@4'
A:/CppProjects/compgeo/main.cpp:14: undefined reference to `glBegin@4'
A:/CppProjects/compgeo/main.cpp:15: undefined reference to `glColor3f@12'
A:/CppProjects/compgeo/main.cpp:16: undefined reference to `glVertex2f@8'
A:/CppProjects/compgeo/main.cpp:17: undefined reference to `glVertex2f@8'
A:/CppProjects/compgeo/main.cpp:18: undefined reference to `glVertex2f@8'
A:/CppProjects/compgeo/main.cpp:19: undefined reference to `glVertex2f@8'
A:/CppProjects/compgeo/main.cpp:20: undefined reference to `glEnd@0'
A:/CppProjects/compgeo/main.cpp:22: undefined reference to `glFlush@0'
CMakeFiles\compgeo.dir/objects.a(main.cpp.obj): In function `main':
A:/CppProjects/compgeo/main.cpp:29: undefined reference to `glutInitWindowSize@8'
A:/CppProjects/compgeo/main.cpp:30: undefined reference to `glutInitWindowPosition@8'
A:/CppProjects/compgeo/main.cpp:31: undefined reference to `glutDisplayFunc@4'
A:/CppProjects/compgeo/main.cpp:32: undefined reference to `glutMainLoop@0'
collect2.exe: error: ld returned 1 exit status
mingw32-make.exe[3]: *** [compgeo.exe] Error 1
mingw32-make.exe[2]: *** [CMakeFiles/compgeo.dir/all] Error 2
mingw32-make.exe[1]: *** [CMakeFiles/compgeo.dir/rule] Error 2
mingw32-make.exe: *** [compgeo] Error 2
CMakeFiles\compgeo.dir\build.make:104: recipe for target 'compgeo.exe' failed
CMakeFiles\Makefile2:94: recipe for target 'CMakeFiles/compgeo.dir/all' failed
CMakeFiles\Makefile2:101: recipe for target 'CMakeFiles/compgeo.dir/rule' failed
Makefile:137: recipe for target 'compgeo' failed
code writer 3000
  • 329
  • 5
  • 19
  • I don't see you linking OpenGL to your target. Also assuming OpenGL contains proper import scripts for cmake manually adding the include directories shouldn't be necessary; just use `target_link_libraries(compgeo PRIVATE OpenGL)` (you need do the equivalent glut too) – fabian Apr 01 '21 at 22:41

1 Answers1

0

You must add the library as a link dependency.

target_link_libraries(compgeo ${OPENGL_LIBRARIES} ${GLUT_LIBRARY} )
Devolus
  • 21,661
  • 13
  • 66
  • 113