【问题标题】:Many undefined references with CLion C++ [duplicate]CLion C++ 的许多未定义引用 [重复]
【发布时间】:2021-04-01 22:38:58
【问题描述】:

我想学习如何将 OpenGL 库与 C++ 一起使用,但我正在努力导入外部库。我使用 CLion 作为我的 IDE,使用 MinGW 作为我的编译器。这是我的 CMake 代码:

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} )

这是我要测试的代码:

/*
 * 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;
}

这些是我遇到的错误。一旦链接 CXX 可执行文件,问题就会出现:

[ 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

【问题讨论】:

  • 我没有看到您将 OpenGL 链接到您的目标。还假设 OpenGL 包含正确的 cmake 导入脚本,手动添加包含目录应该是不必要的;只需使用target_link_libraries(compgeo PRIVATE OpenGL)(你也需要做同样的过剩)

标签: c++ opengl cmake clion


【解决方案1】:

您必须将库添加为链接依赖项。

target_link_libraries(compgeo ${OPENGL_LIBRARIES} ${GLUT_LIBRARY} )

【讨论】:

    猜你喜欢
    • 2019-10-12
    • 1970-01-01
    • 2018-09-01
    • 2016-08-21
    • 2017-04-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-04-07
    相关资源
    最近更新 更多