Ld 对 OpenGL 的未定义引用

Ld undefined reference to OpenGL

经过多次尝试和大量研究,我仍然无法 link OpenGL。我正在尝试编译的软件是在 Ubuntu 18.04 上制作的,并且在我现在在 Ubuntu 20.04.

上编译得很好

这是在 Ubuntu 18.04:

上使用的 CMakeLists
cmake_minimum_required(VERSION 3.1 FATAL_ERROR)

# Project configuration
project(grid_3d)
set(PROJECT_VERSION_MAJOR 1)
set(PROJECT_VERSION_MINOR 0)
set(PROJECT_VERSION_PATCH 0)

# Define folders
set(EXT_DIR ${CMAKE_CURRENT_SOURCE_DIR}/ext)
set(INC_DIR ${CMAKE_CURRENT_SOURCE_DIR}/inc)
set(SRC_DIR ${CMAKE_CURRENT_SOURCE_DIR}/src)
set(TEST_DIR ${CMAKE_CURRENT_SOURCE_DIR}/test)
set(BIN_DIR ${CMAKE_CURRENT_BINARY_DIR}/bin)
set(LIB_DIR ${CMAKE_CURRENT_BINARY_DIR}/lib)

# Compiler settings
set(CMAKE_CXX_STANDARD 17)
set(CXX_OPTIONS "-DDEBUG_UI")
set(warnings "-Wall -Wextra -Werror -Wno-unused -Wno-deprecated-declarations")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${warnings} ${CXX_OPTIONS} -pthread")

# Output
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${LIB_DIR})
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${LIB_DIR})
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${BIN_DIR})
set(CMAKE_LEGACY_CYGWIN_WIN32 0)

# Find librealsense2 installed package
find_package(realsense2 REQUIRED)

# Find pcl installed package
find_package(PCL 1.8 REQUIRED COMPONENTS common io filters features kdtree segmentation)
include_directories(${PCL_INCLUDE_DIRS})
link_directories(${PCL_LIBRARY_DIRS})
add_definitions(${PCL_DEFINITIONS})

# Find GLFW3 (sudo apt install libglfw3-dev)
find_package(PkgConfig REQUIRED)
pkg_search_module(GLFW REQUIRED glfw3)
include_directories(${GLFW_INCLUDE_DIRS})

# Libraries
set(LIBS
        ${realsense2_LIBRARY}
        ${PCL_LIBRARIES}
    )

# Files
include_directories(${INC_DIR})
# include_directories(${EXT_DIR}/imgui-1.79)
file(GLOB SOURCES
    ${INC_DIR}/*.hpp
    ${INC_DIR}/ui/*.hpp
    ${SRC_DIR}/*.cpp
)

# Binaries
add_executable(${CMAKE_PROJECT_NAME} ${SOURCES})
target_link_libraries(${CMAKE_PROJECT_NAME} ${LIBS})
target_link_libraries(${CMAKE_PROJECT_NAME} ${GLFW_LIBRARIES})

这是我项目中唯一与 OpenGL 相关的包含:

#define GL_SILENCE_DEPRECATION
#define GLFW_INCLUDE_GLU
#include <GLFW/glfw3.h>

我有以下共享库:

libglib-2.0.so.0 (libc6,x86-64) => /lib/x86_64-linux-gnu/libglib-2.0.so.0
libglib-2.0.so (libc6,x86-64) => /lib/x86_64-linux-gnu/libglib-2.0.so
libglfw.so.3 (libc6,x86-64) => /lib/x86_64-linux-gnu/libglfw.so.3
libglfw.so (libc6,x86-64) => /lib/x86_64-linux-gnu/libglfw.so
ibOpenGL.so.0 (libc6,x86-64) => /lib/x86_64-linux-gnu/libOpenGL.so.0
libOpenGL.so (libc6,x86-64) => /lib/x86_64-linux-gnu/libOpenGL.so
libGLdispatch.so.0 (libc6,x86-64) => /lib/x86_64-linux-gnu/libGLdispatch.so.0
libGLdispatch.so (libc6,x86-64) => /lib/x86_64-linux-gnu/libGLdispatch.so
libGLX_nvidia.so.0 (libc6,x86-64) => /lib/x86_64-linux-gnu/libGLX_nvidia.so.0
libGLX_mesa.so.0 (libc6,x86-64) => /lib/x86_64-linux-gnu/libGLX_mesa.so.0
libGLX.so.0 (libc6,x86-64) => /lib/x86_64-linux-gnu/libGLX.so.0
libGLX.so (libc6,x86-64) => /lib/x86_64-linux-gnu/libGLX.so
libGLU.so.1 (libc6,x86-64) => /lib/x86_64-linux-gnu/libGLU.so.1
libGLU.so (libc6,x86-64) => /lib/x86_64-linux-gnu/libGLU.so
libGLEW.so.2.1 (libc6,x86-64) => /lib/x86_64-linux-gnu/libGLEW.so.2.1
libGLEW.so (libc6,x86-64) => /lib/x86_64-linux-gnu/libGLEW.so
libGLESv2_nvidia.so.2 (libc6,x86-64) => /lib/x86_64-linux-gnu/libGLESv2_nvidia.so.2
libGLESv2.so.2 (libc6,x86-64) => /lib/x86_64-linux-gnu/libGLESv2.so.2
libGLESv2.so (libc6,x86-64) => /lib/x86_64-linux-gnu/libGLESv2.so
libGLESv1_CM_nvidia.so.1 (libc6,x86-64) => /lib/x86_64-linux-gnu/libGLESv1_CM_nvidia.so.1
libGLESv1_CM.so.1 (libc6,x86-64) => /lib/x86_64-linux-gnu/libGLESv1_CM.so.1
libGLESv1_CM.so (libc6,x86-64) => /lib/x86_64-linux-gnu/libGLESv1_CM.so
libGL.so.1 (libc6,x86-64) => /lib/x86_64-linux-gnu/libGL.so.1
libGL.so (libc6,x86-64) => /lib/x86_64-linux-gnu/libGL.so
libEGL_nvidia.so.0 (libc6,x86-64) => /lib/x86_64-linux-gnu/libEGL_nvidia.so.0
libEGL_mesa.so.0 (libc6,x86-64) => /lib/x86_64-linux-gnu/libEGL_mesa.so.0
libEGL.so.1 (libc6,x86-64) => /lib/x86_64-linux-gnu/libEGL.so.1
libEGL.so (libc6,x86-64) => /lib/x86_64-linux-gnu/libEGL.so

我尝试添加 -lGL-lglfw link 等选项,但均未成功。

如何在 Ubuntu 20.04 64 位上 link OpenGL 到我的程序?为什么它在 Ubuntu 18.04 和 Ubuntu 20.04 之间变化?

如果没有最少的可重现示例,很难诊断您的问题。但是我看到的是您的系统同时具有 libGLlibOpenGL。这可能意味着 libGL 只是 libglvnd 的代理,不包含任何 GL API 函数。

但是你不应该急于link直接到libOpenGL。不同的系统可能配置不同。相反,使用 CMake 定位 OpenGL 对象的正确方法是使用 find_package(OpenGL),然后在 target_link_libraries.

中包含 OpenGL::GL

虚拟项目示例:

  • CMakeLists.txt
cmake_minimum_required(VERSION 3.1 FATAL_ERROR)
project(demo)
find_package(OpenGL)
add_executable(prog test.cpp)
target_link_libraries(prog OpenGL::GL)
  • test.cpp
#include <GL/gl.h>

int main()
{
    glLoadIdentity();
}