在c++和OpenGL中使用Sublime Text 2
Using Sublime Text 2 for C++ and OpenGL
我是一个编程新手,通过c++学习在Sublime Text 2中使用OpenGL编码。我有javascript的经验,我现在使用Sublime Text 2与MinGW编译c++代码。它在正常的c++代码中运行得很好,但在OpenGL中它会返回问题。我已经在MinGW中安装了GLUT .
例如:我从youtube视频中找到了这个简单的代码:http://www.youtube.com/watch?v=SAmD_Aq1Un4
#include <GL/GLUT.h>
#include <iostream>
void render(void);
void keyboard(unsigned char c, int x, int y);
void mouse(int button, int state, int x, int y);
int main(int argc, char** argv) {
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA);
glutInitWindowPosition(100, 100);
glutInitWindowSize(640, 480);
glutCreateWindow("Simple GLUT Application");
glutDisplayFunc(render);
glutKeyboardFunc(keyboard);
glutMouseFunc(mouse);
glutMainLoop();
}
void keyboard(unsigned char c, int x, int y) {
if (c == 27) {
exit(0);
}
}
void mouse(int button, int state, int x, int y) {
if (button == GLUT_RIGHT_BUTTON) {
exit(0);
}
}
void render(void) {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glBegin(GL_TRIANGLES);
glColor3f(1, 0, 0);
glVertex2f(-0.5, -0.5);
glColor3f(0, 1, 0);
glVertex2f(0.5, -0.5);
glColor3f(0, 0, 1);
glVertex2f(0.0, 0.5);
glEnd();
glutSwapBuffers();
}
当我构建文件时,它说:
C:UsersHARIOM~1AppDataLocalTempcc9WOgBg.o:openGL_Glut.cpp:(.text+0x1c): undefined reference to `__glutInitWithExit'
C:UsersHARIOM~1AppDataLocalTempcc9WOgBg.o:openGL_Glut.cpp:(.text+0x37): undefined reference to `__glutCreateWindowWithExit'
C:UsersHARIOM~1AppDataLocalTempcc9WOgBg.o:openGL_Glut.cpp:(.text+0x52): undefined reference to `__glutCreateMenuWithExit'
C:UsersHARIOM~1AppDataLocalTempcc9WOgBg.o:openGL_Glut.cpp:(.text+0x80): undefined reference to `glutInitDisplayMode'
C:UsersHARIOM~1AppDataLocalTempcc9WOgBg.o:openGL_Glut.cpp:(.text+0x94): undefined reference to `glutInitWindowPosition'
C:UsersHARIOM~1AppDataLocalTempcc9WOgBg.o:openGL_Glut.cpp:(.text+0xa8): undefined reference to `glutInitWindowSize'
C:UsersHARIOM~1AppDataLocalTempcc9WOgBg.o:openGL_Glut.cpp:(.text+0xc0): undefined reference to `glutDisplayFunc'
C:UsersHARIOM~1AppDataLocalTempcc9WOgBg.o:openGL_Glut.cpp:(.text+0xcc): undefined reference to `glutKeyboardFunc'
C:UsersHARIOM~1AppDataLocalTempcc9WOgBg.o:openGL_Glut.cpp:(.text+0xd8): undefined reference to `glutMouseFunc'
C:UsersHARIOM~1AppDataLocalTempcc9WOgBg.o:openGL_Glut.cpp:(.text+0xdd): undefined reference to `glutMainLoop'
C:UsersHARIOM~1AppDataLocalTempcc9WOgBg.o:openGL_Glut.cpp:(.text+0x130): undefined reference to `_imp__glClear'
C:UsersHARIOM~1AppDataLocalTempcc9WOgBg.o:openGL_Glut.cpp:(.text+0x13e): undefined reference to `_imp__glBegin'
C:UsersHARIOM~1AppDataLocalTempcc9WOgBg.o:openGL_Glut.cpp:(.text+0x15f): undefined reference to `_imp__glColor3f'
C:UsersHARIOM~1AppDataLocalTempcc9WOgBg.o:openGL_Glut.cpp:(.text+0x177): undefined reference to `_imp__glVertex2f'
C:UsersHARIOM~1AppDataLocalTempcc9WOgBg.o:openGL_Glut.cpp:(.text+0x198): undefined reference to `_imp__glColor3f'
C:UsersHARIOM~1AppDataLocalTempcc9WOgBg.o:openGL_Glut.cpp:(.text+0x1b0): undefined reference to `_imp__glVertex2f'
C:UsersHARIOM~1AppDataLocalTempcc9WOgBg.o:openGL_Glut.cpp:(.text+0x1d1): undefined reference to `_imp__glColor3f'
C:UsersHARIOM~1AppDataLocalTempcc9WOgBg.o:openGL_Glut.cpp:(.text+0x1e9): undefined reference to `_imp__glVertex2f'
C:UsersHARIOM~1AppDataLocalTempcc9WOgBg.o:openGL_Glut.cpp:(.text+0x1f0): undefined reference to `_imp__glEnd'
C:UsersHARIOM~1AppDataLocalTempcc9WOgBg.o:openGL_Glut.cpp:(.text+0x1f7): undefined reference to `glutSwapBuffers'
c:/mingw/bin/../lib/gcc/mingw32/4.7.2/../../../../mingw32/bin/ld.exe: C:UsersHARIOM~1AppDataLocalTempcc9WOgBg.o: bad reloc address 0x0 in section `.ctors'
c:/mingw/bin/../lib/gcc/mingw32/4.7.2/../../../../mingw32/bin/ld.exe: final link failed: Invalid operation
collect2.exe: error: ld returned 1 exit status
[Finished in 0.8s with exit code 1]
在c++中,如果你想使用一个库(在这个例子中是静态的),你必须链接到它。在普通英语中,您必须告诉编译器在哪里可以找到库函数的实现。我不知道如何在编辑器中做到这一点,但通常有一个叫做链接器设置的东西,你可以在其中链接项目所需的所有库。有关更多信息,请参阅此wiki页面
相关文章:
- 为 g++ build 设置超时,为 Sublime Text 运行?
- Sublime Text 3 在我尝试编译C++代码时在状态栏上显示"No Build System"。为什么?
- 在 Mac OSX 上运行 Sublime Text 3 中的 C++
- Sublime Text 3 Build System for Ubuntu
- 我正在尝试通过CreateProcess()在Sublime Text中打开文件,但是构建显示错误
- 在Sublime Text 2中运行cpp文件
- 在 Mac 上使用 Sublime Text 2 对 fstream 使用相对路径
- 无法使用Sublime Text 2构建Allegro计划
- Sublime Text 2用作C IDE是否有用?[VS.视觉工作室]
- 如何在Sublime Text 2,Windows 8中构建和运行C 程序
- g++ 使用 Sublime Text 2 构建C++代码时的致命错误
- 默认情况下,在Sublime Text 3中编译程序为c++14
- std::ifstream 在将文本编辑器从记事本++切换到Sublime Text 2以使用它正在读取的文件后无法读入?
- 如何使用sublimeREPL在Ubuntu 14.04上的Sublime Text 3中获取C++用户输入
- 在Sublime Text中构建系统
- Sublime Text 2 and MinGW
- 如何在Windows系统中使用Sublime Text在外部控制台中运行程序
- 无法使用 Sublime Text 2 构建C++程序
- 使用Sublime Text和cmake(构建系统)
- SDL 2.0 将问题与 Sublime Text 2 和 MinGW 联系起来