在尝试将opengl安装(如果正确的话)到我的Codelite IDE上以创建c ++ opengl程序时,我在freeglut_std.h文件中遇到了多个未定义的参考错误,并且众所周知的函数如glutMainLoop()也“未定义”。
因此,我实际上在freeglut_std.h文件中找到了函数定义,并且尝试将头文件弄乱,以确保没有错误条件导致无法创建我的定义。 (例如,使#if为true ..... #endif(这样,确保代码可以正常运行))。我有C方面的经验,但以前从未链接过这样的文件。浏览多个网页,例如:https://www.ntu.edu.sg/home/ehchua/programming/opengl/HowTo_OpenGL_C.html,https://www.transmissionzero.co.uk/computing/using-glut-with-mingw/我链接了我的文件。 (还请注意,这个问题之前曾被问过,但是大多数帖子有不同的解决方案对我不起作用)
因此,这是Codelite编译我的文件的方式(没有错误的代码):
C:/mingw-w64/i686-7.3.0-posix-dwarf-rt_v5-rev0/mingw32/bin/g++.exe -c "C:/Users/admin/Documents/codelite/workspaces/c_plus_plus/cpp_openGL/main.cpp" -g -O0 -Wall -I"C:\mingw-w64\i686-7.3.0-posix-dwarf-rt_v5-rev0\mingw32\include" -o ./Debug/main.cpp.o -I. -I.
C:/mingw-w64/i686-7.3.0-posix-dwarf-rt_v5-rev0/mingw32/bin/g++.exe -o ./Debug/cpp_openGL @"cpp_openGL.txt" -L. -m32 -static-libgcc -static-libstdc++ -static -lpthread -L"C:\mingw-w64\i686-7.3.0-posix-dwarf-rt_v5-rev0\mingw32\lib" -Wl,--subsystem,windows -lfreeglut -lopengl32 -lglu32
mingw32-make.exe[1]: Leaving directory 'C:/Users/admin/Documents/codelite/workspaces/c_plus_plus/cpp_openGL'
====0 errors, 0 warnings====
但是对于这样的东西(实际测试代码):
/*
* 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;
}
我收到以下错误:
./Debug/main.cpp.o: In function `glutInit_ATEXIT_HACK':
C:/mingw-w64/i686-7.3.0-posix-dwarf-rt_v5-rev0/mingw32/include/GL/freeglut_std.h:623: undefined reference to `_imp____glutInitWithExit@12'
./Debug/main.cpp.o: In function `glutCreateWindow_ATEXIT_HACK':
C:/mingw-w64/i686-7.3.0-posix-dwarf-rt_v5-rev0/mingw32/include/GL/freeglut_std.h:625: undefined reference to `_imp____glutCreateWindowWithExit@8'
./Debug/main.cpp.o: In function `glutCreateMenu_ATEXIT_HACK':
C:/mingw-w64/i686-7.3.0-posix-dwarf-rt_v5-rev0/mingw32/include/GL/freeglut_std.h:627: undefined reference to `_imp____glutCreateMenuWithExit@8'
./Debug/main.cpp.o: In function `main':
C:/Users/admin/Documents/codelite/workspaces/c_plus_plus/cpp_openGL/main.cpp:32: undefined reference to `_imp__glutInitWindowSize@8'
C:/Users/admin/Documents/codelite/workspaces/c_plus_plus/cpp_openGL/main.cpp:33: undefined reference to `_imp__glutInitWindowPosition@8'
C:/Users/admin/Documents/codelite/workspaces/c_plus_plus/cpp_openGL/main.cpp:34: undefined reference to `_imp__glutDisplayFunc@4'
C:/Users/admin/Documents/codelite/workspaces/c_plus_plus/cpp_openGL/main.cpp:35: undefined reference to `_imp__glutMainLoop@0'
collect2.exe: error: ld returned 1 exit status
====7 errors, 0 warnings====
它抱怨的文件是freeglut_std.h,以下是从文件中复制的:
#if defined(_WIN32) && !defined(GLUT_DISABLE_ATEXIT_HACK) && !defined(__WATCOMC__)
FGAPI void FGAPIENTRY __glutInitWithExit(int *argcp, char **argv, void (__cdecl *exitfunc)(int));
FGAPI int FGAPIENTRY __glutCreateWindowWithExit(const char *title, void (__cdecl *exitfunc)(int));
FGAPI int FGAPIENTRY __glutCreateMenuWithExit(void (* func)(int), void (__cdecl *exitfunc)(int));
#ifndef FREEGLUT_BUILDING_LIB
#if defined(__GNUC__)
#define FGUNUSED __attribute__((unused))
#else
#define FGUNUSED
#endif
static void FGAPIENTRY FGUNUSED glutInit_ATEXIT_HACK(int *argcp, char **argv) { __glutInitWithExit(argcp, argv, exit); }//<--has problem with
#define glutInit glutInit_ATEXIT_HACK
static int FGAPIENTRY FGUNUSED glutCreateWindow_ATEXIT_HACK(const char *title) { return __glutCreateWindowWithExit(title, exit); }//<--has problem with
#define glutCreateWindow glutCreateWindow_ATEXIT_HACK
static int FGAPIENTRY FGUNUSED glutCreateMenu_ATEXIT_HACK(void (* func)(int)) { return __glutCreateMenuWithExit(func, exit); } //<--has problem with
#define glutCreateMenu glutCreateMenu_ATEXIT_HACK
#endif
#endif
#ifdef __cplusplus
}
#endif
请注意,我可以使用以下方法摆脱3个未定义的引用:
#define GLUT_DISABLE_ATEXIT_HACK
但是其余的错误仍然存在。
freeglut_std.h中的最后一条注释存在以下几行(这些不是定义)
FGAPI void FGAPIENTRY glutInit( int* pargc, char** argv );
FGAPI void FGAPIENTRY glutInitWindowPosition( int x, int y );
FGAPI void FGAPIENTRY glutInitWindowSize( int width, int height );
FGAPI void FGAPIENTRY glutInitDisplayMode( unsigned int displayMode );
FGAPI void FGAPIENTRY glutInitDisplayString( const char* displayMode );
并且某些东西必须工作,因为它不会抱怨
glutinit(param...)
glutCreateWindow(param...)
更新
我应该让你们知道我有一个freeglut.dll文件保存在与我的应用程序相同的位置。但是,这没关系,因为我还尝试了在complier命令中使用-DFREEGLUT_STATIC
的静态构建白色,并链接了-lfreeglut_static
,除了错误的_imp_
部分外,结果相同失去了。
总之,我在这里感到困惑,对我们的帮助总是感激不尽。
(附:请不要让我改装)。