在CLion中使用OpenGL的未定义参考错误 [英] Undefined Reference error using OpenGL in CLion

查看:138
本文介绍了在CLion中使用OpenGL的未定义参考错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我正在使用freeglut尝试做一些openGL的工作,但我不断收到错误消息,指出引用未定义:

So, I am using freeglut to try to do some openGL stuff, but I keep getting errors saying that references are undefined:

"C:\Program Files (x86)\JetBrains\CLion 2016.2.1\bin\cmake\bin\cmake.exe" --build C:\Users\Nick\.CLion2016.2\system\cmake\generated\cs455opengl-9b23e7f0\9b23e7f0\Debug --target all -- -j 4
Scanning dependencies of target cs455openGL
[ 50%] Building CXX object CMakeFiles/cs455openGL.dir/main.cpp.obj
[100%] Linking CXX executable cs455openGL.exe
CMakeFiles\cs455openGL.dir/objects.a(main.cpp.obj): In function `Z4initv':
C:/Users/Nick/ClionProjects/cs455opengl/main.cpp:12: undefined reference to `glClearColor@16'
C:/Users/Nick/ClionProjects/cs455opengl/main.cpp:13: undefined reference to `glMatrixMode@4'
C:/Users/Nick/ClionProjects/cs455opengl/main.cpp:14: undefined reference to `glLoadIdentity@0'
C:/Users/Nick/ClionProjects/cs455opengl/main.cpp:15: undefined reference to `glOrtho@48'
CMakeFiles\cs455openGL.dir/objects.a(main.cpp.obj): In function `Z7displayv':
C:/Users/Nick/ClionProjects/cs455opengl/main.cpp:20: undefined reference to `glClear@4'
C:/Users/Nick/ClionProjects/cs455opengl/main.cpp:21: undefined reference to `glColor3f@12'
C:/Users/Nick/ClionProjects/cs455opengl/main.cpp:22: undefined reference to `glVertex3f@12'
C:/Users/Nick/ClionProjects/cs455opengl/main.cpp:23: undefined reference to `glVertex3f@12'
C:/Users/Nick/ClionProjects/cs455opengl/main.cpp:24: undefined reference to `glVertex3f@12'
C:/Users/Nick/ClionProjects/cs455opengl/main.cpp:25: undefined reference to `glVertex3f@12'
C:/Users/Nick/ClionProjects/cs455opengl/main.cpp:26: undefined reference to `glEnd@0'
C:/Users/Nick/ClionProjects/cs455opengl/main.cpp:27: undefined reference to `glFlush@0'
collect2.exe: error: ld returned 1 exit status
CMakeFiles\cs455openGL.dir\build.make:95: recipe for target 'cs455openGL.exe' failed
CMakeFiles\Makefile2:66: recipe for target 'CMakeFiles/cs455openGL.dir/all' failed
mingw32-make.exe[2]: *** [cs455openGL.exe] Error 1
mingw32-make.exe[1]: *** [CMakeFiles/cs455openGL.dir/all] Error 2
mingw32-make.exe: *** [all] Error 2
Makefile:82: recipe for target 'all' failed

我正在将MinGW与CLion一起使用来进行此项目。我以为我一切都正确。我将适当的文件移至MinGW的include文件夹,bin文件夹以及lib文件夹中。然后,在我的CMakeLists.txt文件中输入:

I am using MinGW with CLion to do this project. I thought that I got everything correctly. I moved the appropriate files into the include folder in MinGW, as well as the bin folder, and also the lib folder. Then, I have this in my CMakeLists.txt:

cmake_minimum_required(VERSION 3.3)
project(cs455openGL)
find_package(OpenGL REQUIRED)
find_package(GLUT REQUIRED)

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=gnu++11")

set(SOURCE_FILES main.cpp)
add_executable(cs455openGL ${SOURCE_FILES})
link_directories(${OPENGL_gl_LIBRARY})
target_link_libraries(cs455openGL libfreeglut.a libfreeglut_static.a)

我链接的库是freeglut随附的唯一库文件。我一直在互联网上寻找答案,似乎没人遇到过这个问题。

The libraries I linked were the only library files that freeglut came with. I've been scouring the internet for answers and no one seems to have run into this.

那么,我想念的是什么? CLion在编译之前不会显示任何错误。我什至可以进入freeglut提供的头文件中的功能。为什么然后在我的程序中未定义这些函数?

So, what am I missing? CLion doesn't show any errors before it is compiled. I can even go into the functions in the header files provided by freeglut. Why then, are these functions not defined in my program?

推荐答案

这是一个链接错误,告诉您链接器确实

This is a linking error, and tells you that the linker does not find the functions defined by the OpenGL library.

您必须在<$ c中添加 $ {OPENGL_LIBRARIES} $ c> target_link_libraries 。

You have to add ${OPENGL_LIBRARIES} to target_link_libraries.

对于 glut -和其他任何库-您不应直接使用库名( libfreeglut.a ),而应始终使用 find_package 填充的变量:

And for glut - and any other library - you should not use the library name (libfreeglut.a) directly, but always use the variables populated by the find_package:

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

这篇关于在CLion中使用OpenGL的未定义参考错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆