opengl - SDL 2.0/OpenGL 代码无法编译

标签 opengl ubuntu sdl-2

我正在尝试在终端中使用 gcc 编译一个简单的 SDL/OpenGL 程序。这是代码(main.c):

#include <stdio.h>
#include <stdlib.h>
/* If using gl3.h */
/* Ensure we are using opengl's core profile only */
#include<GL/glew.h>
#define GL3_PROTOTYPES 1
#include <GL/gl.h>

 #include <SDL.h>
 #define PROGRAM_NAME "Tutorial1"

/* A simple function that prints a message, the error code returned by SDL,
 * and quits the application */
void sdldie(const char *msg)
{
    printf("%s: %s\n", msg, SDL_GetError());
    SDL_Quit();
    exit(1);
}


void checkSDLError(int line)
{
#ifndef NDEBUG
    const char *error = SDL_GetError();
    if (*error != '\0')
    {
        printf("SDL Error: %s\n", error);
        if (line != -1)
            printf(" + line: %i\n", line);
        SDL_ClearError();
    }
#endif
}


/* Our program's entry point */
int main(int argc, char *argv[])
{
    SDL_Window *mainwindow; /* Our window handle */
    SDL_GLContext maincontext; /* Our opengl context handle */

    if (SDL_Init(SDL_INIT_VIDEO) < 0) /* Initialize SDL's Video subsystem */
        sdldie("Unable to initialize SDL"); /* Or die on error */

    /* Request opengl 3.2 context.
         * SDL doesn't have the ability to choose which profile at this time of writing,
              * but it should default to the core profile */
    SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3);
    SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 2);

    /* Turn on double buffering with a 24bit Z buffer.
         * You may need to change this to 16 or 32 for your system */
    SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
    SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 24);

    /* Create our window centered at 512x512 resolution */
    mainwindow = SDL_CreateWindow(PROGRAM_NAME, SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED,
                                  512, 512, SDL_WINDOW_OPENGL | SDL_WINDOW_SHOWN);
    if (!mainwindow) /* Die if creation failed */
        sdldie("Unable to create window");

    checkSDLError(__LINE__);

    /* Create our opengl context and attach it to our window */
    maincontext = SDL_GL_CreateContext(mainwindow);
    checkSDLError(__LINE__);


    /* This makes our buffer swap syncronized with the monitor's vertical refresh */
    SDL_GL_SetSwapInterval(1);

    /* Clear our buffer with a red background */
    glClearColor ( 1.0, 0.0, 0.0, 1.0 );
    glClear ( GL_COLOR_BUFFER_BIT );
    /* Swap our back buffer to the front */
    SDL_GL_SwapWindow(mainwindow);
    /* Wait 2 seconds */
    SDL_Delay(2000);

    /* Same as above, but green */
    glClearColor ( 0.0, 1.0, 0.0, 1.0 );
    glClear ( GL_COLOR_BUFFER_BIT );
    SDL_GL_SwapWindow(mainwindow);
    SDL_Delay(2000);

    /* Same as above, but blue */
    glClearColor ( 0.0, 0.0, 1.0, 1.0 );
    glClear ( GL_COLOR_BUFFER_BIT );
    SDL_GL_SwapWindow(mainwindow);
    SDL_Delay(2000);

    /* Delete our opengl context, destroy our window, and shutdown SDL */
    SDL_GL_DeleteContext(maincontext);
    SDL_DestroyWindow(mainwindow);
    SDL_Quit();

    return 0;
}

当我尝试使用以下代码编译此代码时:
gcc -o ./bin/gl_nbody -I/usr/local/include/SDL2 -g -lGLEW -lGL -lX11 -lSDL2 -Wl,-rpath,/usr/local/lib -L/usr/local/lib ./src/main.c

我收到以下错误:
/tmp/ccg0glmq.o: In function `sdldie':
/home/jared/projects/gl_nbody/./src/main.c:16: undefined reference to `SDL_GetError'
/home/jared/projects/gl_nbody/./src/main.c:17: undefined reference to `SDL_Quit'
/tmp/ccg0glmq.o: In function `checkSDLError':
/home/jared/projects/gl_nbody/./src/main.c:25: undefined reference to `SDL_GetError'
/home/jared/projects/gl_nbody/./src/main.c:31: undefined reference to `SDL_ClearError'
/tmp/ccg0glmq.o: In function `main':
/home/jared/projects/gl_nbody/./src/main.c:43: undefined reference to `SDL_Init'
/home/jared/projects/gl_nbody/./src/main.c:49: undefined reference to `SDL_GL_SetAttribute'
/home/jared/projects/gl_nbody/./src/main.c:50: undefined reference to `SDL_GL_SetAttribute'
/home/jared/projects/gl_nbody/./src/main.c:54: undefined reference to `SDL_GL_SetAttribute'
/home/jared/projects/gl_nbody/./src/main.c:55: undefined reference to `SDL_GL_SetAttribute'
/home/jared/projects/gl_nbody/./src/main.c:58: undefined reference to `SDL_CreateWindow'
/home/jared/projects/gl_nbody/./src/main.c:66: undefined reference to `SDL_GL_CreateContext'
/home/jared/projects/gl_nbody/./src/main.c:71: undefined reference to `SDL_GL_SetSwapInterval'
/home/jared/projects/gl_nbody/./src/main.c:74: undefined reference to `glClearColor'
/home/jared/projects/gl_nbody/./src/main.c:75: undefined reference to `glClear'
/home/jared/projects/gl_nbody/./src/main.c:77: undefined reference to `SDL_GL_SwapWindow'
/home/jared/projects/gl_nbody/./src/main.c:79: undefined reference to `SDL_Delay'
/home/jared/projects/gl_nbody/./src/main.c:82: undefined reference to `glClearColor'
/home/jared/projects/gl_nbody/./src/main.c:83: undefined reference to `glClear'
/home/jared/projects/gl_nbody/./src/main.c:84: undefined reference to `SDL_GL_SwapWindow'
/home/jared/projects/gl_nbody/./src/main.c:85: undefined reference to `SDL_Delay'
/home/jared/projects/gl_nbody/./src/main.c:88: undefined reference to `glClearColor'
/home/jared/projects/gl_nbody/./src/main.c:89: undefined reference to `glClear'
/home/jared/projects/gl_nbody/./src/main.c:90: undefined reference to `SDL_GL_SwapWindow'
/home/jared/projects/gl_nbody/./src/main.c:91: undefined reference to `SDL_Delay'
/home/jared/projects/gl_nbody/./src/main.c:94: undefined reference to `SDL_GL_DeleteContext'
/home/jared/projects/gl_nbody/./src/main.c:95: undefined reference to `SDL_DestroyWindow'
/home/jared/projects/gl_nbody/./src/main.c:96: undefined reference to `SDL_Quit'

这似乎表明我错误地包含了 SDL2 库。但是,我认为这是使用 -lSDL2 选项处理的。关于这里有什么问题的任何想法?

最佳答案

在 linux 上你应该 不是 包括 -lSDL2,但使用 SDL2-config脚本。它有一些奇怪的依赖关系,只有那个脚本才能解决它。你应该用这样的东西编译:

gcc -o ./bin/gl_nbody -I/usr/local/include/SDL2 `sdl-config --cflags --libs` -g -lGLEW -lGL -lX11 -Wl,-rpath,/usr/local/lib -L/usr/local/lib ./src/main.c 

SDL Wiki 上的常见问题解答显示了更多详细信息:http://wiki.libsdl.org/FAQLinux

关于opengl - SDL 2.0/OpenGL 代码无法编译,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22160548/

相关文章:

c++ - 为 openGL 提供 3D 坐标的顺序是什么?

ubuntu - 如何安排服务器启动?

ubuntu - SDL2 - undefined reference - Ubuntu

使用 SDL 2 运行应用程序时 Xcode 5 崩溃

c++ - OpenGL混淆了推送和弹出的顺序

opengl - 在 OpenGL 中,为什么 glVertexAttribPointer 需要将 "pointer"参数作为 void* 传入?

c++ - 在保持纵横比的 openGL 中调整圆圈的大小

c - 模拟 UNIX 类型的 shell

c++ - 使用 openGL 在 SDL2 中全屏显示一个窗口

c - SDL_WINDOWEVENT_RESIZED 与 SDL_WINDOWEVENT_SIZE_CHANGED