How to compile GLUT + OpenGL project with CMake and Kdevelop in linux?

前端 未结 3 663
隐瞒了意图╮
隐瞒了意图╮ 2020-12-04 23:32

As the titles says I can\'t seem to build the project with OpenGL and Glut.

I get Undefined reference errors for OpenGL functions.

I tried doing :

         


        
相关标签:
3条回答
  • 2020-12-05 00:17

    In recent versions of CMake (3.10+) there is a new way to use OpenGL using a so-called IMPORTED target:

    cmake_minimum_required(VERSION 3.10)
    
    project(testas)
    add_executable(testas main.cpp)
    find_package(OpenGL REQUIRED COMPONENTS OpenGL)
    find_package(GLUT REQUIRED)
    
    add_dependencies(testas OpenGL::OpenGL)
    include_directories(${GLUT_INCLUDE_DIRS} )
    
    target_link_libraries(testas OpenGL::OpenGL ${GLUT_LIBRARY} )
    

    At the moment the only practical difference seems to be on Linux (where GLVND is used if available), but presumably this solution should be more future-proof, as CMake has more information about your intentions and other tools parsing your CMakeFiles will better understand the dependency tree.

    0 讨论(0)
  • 2020-12-05 00:19

    I use these two cmake files to build my OpenGL projects, and they all work well.

    I only test these two cmake files under Deepin Linux. Deepin Linux is a Chinese grown Linux system like Ubuntu or from Debian.

    First, the main CMakeLists.txt

    cmake_minimum_required(VERSION 3.1.0)
    project(project_name)
    
    set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_CURRENT_SOURCE_DIR}/CMakeModules/")
    
    find_package(OpenGL REQUIRED)
    find_package(FREEGLUT REQUIRED)
    find_package(GLEW REQUIRED)
    
    if(NOT ${OPENGL_FOUND})
        message("OPENGL not found")
    endif()
    
    include_directories(
        ${PROJECT_SOURCE_DIR}
        ${FREEGLUT_INCLUDE_DIR}
        ${GLEW_INCLUDE_DIR}
        ${OPENGL_INCLUDE_DIR}
        )
    
    message(${OPENGL_INCLUDE_DIR})
    add_executable(${PROJECT_NAME}  ${PROJECT_SOURCE_DIR}/filename.cpp) 
    target_link_libraries(${PROJECT_NAME} 
    ${OPENGL_LIBRARY}
    ${FREEGLUT_LIBRARY}
    ${GLEW_LIBRARY}
    )
    

    Second, the find GLUT cmake file under CMakeModules directory

    # Try to find the FREEGLUT library
    #
    # FREEGLUT_INCLUDE_DIR
    # FREEGLUT_LIBRARY
    # FREEGLUT_FOUND
    
    FIND_PATH(
      FREEGLUT_INCLUDE_DIR GL/freeglut.h GL/gl.h GL/glu.h GL/glew.h
      ${CMAKE_INCLUDE_PATH}
      $ENV{include}
      ${OPENGL_INCLUDE_DIR}
      /usr/include
      /usr/local/include
    )
    
    SET(STORE_CMAKE_FIND_FRAMEWORK ${CMAKE_FIND_FRAMEWORK})
    SET(CMAKE_FIND_FRAMEWORK NEVER)
    
    FIND_LIBRARY(
      FREEGLUT_LIBRARY
      NAMES freeglut_static freeglut glut GL
      PATH
        /opt/local/lib
        ${CMAKE_LIBRARY_PATH}
        $ENV{lib}
        /usr/lib
        /usr/local/lib
    )
    
    SET(CMAKE_FIND_FRAMEWORK ${STORE_CMAKE_FIND_FRAMEWORK})
    
    IF (FREEGLUT_INCLUDE_DIR AND FREEGLUT_LIBRARY)
       SET(FREEGLUT_FOUND TRUE)
    ENDIF (FREEGLUT_INCLUDE_DIR AND FREEGLUT_LIBRARY)
    
    IF (FREEGLUT_FOUND)
       IF (NOT FREEGLUT_FIND_QUIETLY)
          MESSAGE(STATUS "Found FREEGLUT: ${FREEGLUT_LIBRARY}")
       ENDIF (NOT FREEGLUT_FIND_QUIETLY)
    ELSE (FREEGLUT_FOUND)
       IF (FREEGLUT_FIND_REQUIRED)
          MESSAGE(FATAL_ERROR "Could not find FREEGLUT")
       ENDIF (FREEGLUT_FIND_REQUIRED)
    ENDIF (FREEGLUT_FOUND)
    
    0 讨论(0)
  • 2020-12-05 00:31

    find_package(OpenGL) will find the package for you, but it doesn't link the package to the target.

    To link to a library, you can use target_link_libraries(<target> <item>). In addition, you also need to set the include directory, so that the linker knows where to look for things. This is done with the include_directories.

    An example CMakeLists.txt which would do this looks something like this:

    
    cmake_minimum_required(VERSION 2.8)
    
    project(testas)
    add_executable(testas main.cpp)
    find_package(OpenGL REQUIRED)
    find_package(GLUT REQUIRED)
    include_directories( ${OPENGL_INCLUDE_DIRS}  ${GLUT_INCLUDE_DIRS} )
    
    target_link_libraries(testas ${OPENGL_LIBRARIES} ${GLUT_LIBRARY} )
    

    If OpenGL is a necessity for your project, you might consider either testing OpenGL_FOUND after the find_package(OpenGL) or using REQUIRED, which will stop cmake if OpenGL is not found.

    For more information and better examples:

    • CMake 2.8 documentation, target_link_libraries
    • CMake 2.8 documentation, find_package
    • CMake wiki: how to find libraries
    • Forum post with solution: cmake and opengl
    • Tutorial for CMake by swarthmore.edu

    In particular, the CMake wiki and cmake and opengl links should give you enough to get things working.

    0 讨论(0)
提交回复
热议问题