Issue
I'm trying to build the simplest qt project using cmake and opengl. I created the default project and make little modification.
When I try to build the project I get 9 errors like this: main.cpp.obj:-1: error: LNK2019: ññûëêà íà íåðàçðåøåííûé âíåøíèé ñèìâîë __imp__glBegin@4 â ôóíêöèè "private: virtual void __thiscall GLWidget::paintGL(void)" (?paintGL@GLWidget@@EAEXXZ)
What should I do to build the project?
My code: CMakeLists.txt:
cmake_minimum_required(VERSION 3.5)
project(untitled2 LANGUAGES CXX)
set(CMAKE_INCLUDE_CURRENT_DIR ON)
set(CMAKE_AUTOUIC ON)
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTORCC ON)
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
add_executable(untitled2
main.cpp
)
find_package(OpenGL)
find_package(Qt5 COMPONENTS Widgets OpenGL REQUIRED)
target_link_libraries(untitled2 PRIVATE Qt5::Widgets Qt5::OpenGL)
main.cpp
#include "QApplication"
#include "QGLWidget"
#include "QDebug"
#include "cmath"
class GLWidget : public QGLWidget{
void initializeGL(){
/// In modelview hand is at origin
glClearColor(1.0, 1.0, 1.0, 1.0);
}
void qgluPerspective(GLdouble fovy, GLdouble aspect, GLdouble zNear, GLdouble zFar){
const GLdouble ymax = zNear * tan(fovy * 3.14 / 360.0);
const GLdouble ymin = -ymax;
const GLdouble xmin = ymin * aspect;
const GLdouble xmax = ymax * aspect;
glFrustum(xmin, xmax, ymin, ymax, zNear, zFar);
}
/// @note camera decides renderer size
void resizeGL(int width, int height){
if (height==0) height=1;
glViewport(0,0,width,height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
qgluPerspective(45.0f,(GLfloat)width/(GLfloat)height,0.1f,100.0f);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
void paintGL(){
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glClear(GL_COLOR_BUFFER_BIT);
glBegin(GL_POLYGON);
glVertex2f(-0.5, -0.5);
glVertex2f(-0.5, 0.5);
glVertex2f(0.5, 0.5);
glVertex2f(0.5, -0.5);
glEnd();
}
};
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
GLWidget widget;
widget.resize(640,480);
widget.show();
return app.exec();
}
Solution
In Linux (Arch Linux) GLUT must be linked (use this answer as a base):
cmake_minimum_required(VERSION 3.5)
project(untitled2 LANGUAGES CXX)
set(CMAKE_INCLUDE_CURRENT_DIR ON)
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
add_executable(untitled2
main.cpp
)
find_package(OpenGL REQUIRED)
find_package(GLUT REQUIRED)
find_package(Qt5 COMPONENTS Widgets OpenGL REQUIRED)
target_link_libraries(untitled2 PRIVATE Qt5::Widgets Qt5::OpenGL ${OPENGL_LIBRARIES} ${GLUT_LIBRARY})
Also when you include system or third-party library you must use <>
instead of ""
so you must change to:
#include <QApplication>
#include <QGLWidget>
#include <QDebug>
#include <cmath>
// ...
Answered By - eyllanesc Answer Checked By - Robin (WPSolving Admin)