c - 对 `GlewInit' 的 undefined reference - OpenGL

标签 c linux opengl glut freeglut

我在尝试制作可执行文件时收到此错误,代码编译正确,但当我点击 make 命令时,我收到此错误消息:

gcc -c helloworld.c
lewis@lewis-desktop ~/Desktop/Dev/gl $ make
gcc -o hello-gl helloworld.o -L/usr/X11R6/lib -lGL -lglut -lGLEW -lm
helloworld.o: In function `Initialize':
helloworld.c:(.text+0x55): undefined reference to `GlewInit'
collect2: ld returned 1 exit status
make: *** [helloworld] Error 1

我正在运行 Linux Mint 13,我认为这与我的 makefile 有关,我对它们了解不多,所以我一起破解了一个并在网上找到了一些代码:

GL_INCLUDE = /usr/X11R6/include
GL_LIB = /usr/X11R6/lib
GL_GLEW = /user/include

helloworld: helloworld.o
    gcc -o hello-gl $^ -L$(GL_LIB) -lGL -lglut -lGLEW -lm

.c.o:
    gcc -c -o $@ $< -I$(GL_INCLUDE)

clean:
    rm -f hello-gl hello-gl-dummy hello-gl.o util.o hello-gl-dummy.o

我的代码:

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <GL/glew.h>
#include <GL/freeglut.h>
#define WINDOW_TITLE_PREFIX "Hello, World!"

int CurrentWidth = 600,
    CurrentHeight = 400,
    WindowHandle = 0;

void Initialize(int, char*[]);
void InitWindow(int, char*[]);
void ResizeFunction(int, int);
void RenderFunction(void);

int main(int argc, char* argv[])
{
    Initialize(argc, argv);

    glutMainLoop();

    exit(EXIT_SUCCESS);
        return 0;
}

void Initialize(int argc, char* argv[])
{
    GLenum GlewInitResult;
    InitWindow(argc, argv);
        GlewInitResult = GlewInit();
        if (GLEW_OK != GlewInitResult) {
            fprintf(stderr, "ERROR: %s\n", glewGetErrorString(GlewInitResult));
            exit(EXIT_FAILURE);
        }

    fprintf(
        stdout,
        "INFO: OpenGL Version: %s\n",
        glGetString(GL_VERSION)
    );

    glClearColor(0.2f, 1.0f, 0.8f, 0.3f);
}

void InitWindow(int argc, char* argv[])
{
    glutInit(&argc, argv);

    glutInitContextVersion(4, 0);
    glutInitContextFlags(GLUT_FORWARD_COMPATIBLE);
    glutInitContextProfile(GLUT_CORE_PROFILE);

    glutSetOption(
        GLUT_ACTION_ON_WINDOW_CLOSE,
        GLUT_ACTION_GLUTMAINLOOP_RETURNS
    );

    glutInitWindowSize(CurrentWidth, CurrentHeight);

    glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA);

        char *states[] = {"Hello", "My", "Name", "Is", "Lewis"};

    WindowHandle = glutCreateWindow(WINDOW_TITLE_PREFIX);

    if(WindowHandle < 1) {
        fprintf(
            stderr,
            "KERN_ERROR: Could not create a new rendering window.\n"
        );
        exit(EXIT_FAILURE);
    }

    glutReshapeFunc(ResizeFunction);
    glutDisplayFunc(RenderFunction);
}

void ResizeFunction(int Width, int Height)

    {
        /*captures the resize data from the OS and assigns it to the global
         variable CURRENT_WIDTH & CURRENT_HEIGHT */

    CurrentWidth = Width;
    CurrentHeight = Height;
    glViewport(0, 0, CurrentWidth, CurrentHeight);
}

void RenderFunction(void)
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    /*the next line takes the data from the back-buffer to the screen */

    glutSwapBuffers();
    glutPostRedisplay();
}

最佳答案

GLEW 函数的名称应以 'glew' 开头,而不是 'Glew'。

原来如此

GlewInitResult = glewInit();

C 是区分大小写的,所有与 OpenGL 相关的库通常以小写字母开头的函数名称。

关于c - 对 `GlewInit' 的 undefined reference - OpenGL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11275866/

相关文章:

c - 在 C 中读取 "pure"字节

linux - 如何在异常情况下强制 HTTP 转换为 HTTPS?

linux - 如何为 Linux : KDE, Gnome 等设置我的应用程序桌面图标?

c++ - 错误:不支持GLSL 3.30。支持的版本为:1.10、1.20、1.30、1.00 ES,3.00 ES,3.10 ES和3.20 ES

c++ - 运行时带有加载光标的 openGL GLFW 白屏

c - 使用 curses 绘制简单的盒子

c - int和char如果不赋值的话有没有固定的赋值?

r - .export 许多对象到 R foreach

c - 没有获得 OpenGL 的输出以使用鼠标单击打印行

将C程序转换为汇编代码