这是我用来试用OpenGL的程序:
#include <cstdlib>
#include <GL/glut.h>
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;
}
运行/构建时,我得到以下输出:
"/usr/bin/make" -f nbproject/Makefile-Debug.mk QMAKE= SUBPROJECTS= .build-conf
make[1]: Entering directory `/home/simon/NetBeansProjects/CppApplication_4'
"/usr/bin/make" -f nbproject/Makefile-Debug.mk dist/Debug/GNU-Linux-x86/cppapplication_4
make[2]: Entering directory `/home/simon/NetBeansProjects/CppApplication_4'
mkdir -p build/Debug/GNU-Linux-x86
rm -f build/Debug/GNU-Linux-x86/main.o.d
g++ -c -g -MMD -MP -MF build/Debug/GNU-Linux-x86/main.o.d -o build/Debug/GNU-Linux-x86/main.o main.cpp
mkdir -p dist/Debug/GNU-Linux-x86
g++ -o dist/Debug/GNU-Linux-x86/cppapplication_4 build/Debug/GNU-Linux-x86/main.o -L/usr/include/GL -lglut -lglfw -lGLEW -lGLU
/usr/bin/ld: build/Debug/GNU-Linux-x86/main.o: undefined reference to symbol 'glVertex2f'
/usr/lib/x86_64-linux-gnu/mesa/libGL.so.1: error adding symbols: DSO missing from command line
collect2: error: ld returned 1 exit status
make[2]: *** [dist/Debug/GNU-Linux-x86/cppapplication_4] Error 1
make[2]: Leaving directory `/home/simon/NetBeansProjects/CppApplication_4'
make[1]: *** [.build-conf] Error 2
make[1]: Leaving directory `/home/simon/NetBeansProjects/CppApplication_4'
make: *** [.build-impl] Error 2
BUILD FAILED (exit value 2, total time: 222ms)
为什么构建失败的任何想法?根据我的谷歌搜索它应该工作。
我对编程很新,但我能看到的是“对符号'glVertex2f'的未定义引用”,它可能是也可能不是所有问题的中心?
答案 0 :(得分:3)
g++ -o dist/Debug/GNU-Linux-x86/cppapplication_4 build/Debug/GNU-Linux-x86/main.o -L/usr/include/GL -lglut -lglfw -lGLEW -lGLU
将-lGL
添加到链接器行。