C++ ld 返回 1 退出状态

标签 c++ linux opengl gcc

我正在 Linux 上从事 OpenGL 项目。但是当我尝试使用 SDL2 创建窗口时,我得到的输出是:

g++ -lGL -lGLEW -lSDL2 main.cpp display.cpp 
/tmp/cchQfbLR.o: In function `Display::Display(unsigned long, unsigned long, std::string const&)':
display.cpp:(.text+0x21): undefined reference to `SDL_Init'
display.cpp:(.text+0x30): undefined reference to `SDL_GL_SetAttribute'
display.cpp:(.text+0x3f): undefined reference to `SDL_GL_SetAttribute'
display.cpp:(.text+0x4e): undefined reference to `SDL_GL_SetAttribute'
display.cpp:(.text+0x5d): undefined reference to `SDL_GL_SetAttribute'
display.cpp:(.text+0x6c): undefined reference to `SDL_GL_SetAttribute'
/tmp/cchQfbLR.o:display.cpp:(.text+0x7b): more undefined references to `SDL_GL_SetAttribute' follow
/tmp/cchQfbLR.o: In function `Display::Display(unsigned long, unsigned long, std::string const&)':
display.cpp:(.text+0xb1): undefined reference to `SDL_CreateWindow'
display.cpp:(.text+0xc9): undefined reference to `SDL_GL_CreateContext'
display.cpp:(.text+0xd6): undefined reference to `glewInit'
/tmp/cchQfbLR.o: In function `Display::update()':
display.cpp:(.text+0x152): undefined reference to `SDL_PollEvent'
/tmp/cchQfbLR.o: In function `Display::swapBuffers()':
display.cpp:(.text+0x18e): undefined reference to `SDL_GL_SwapWindow'
/tmp/cchQfbLR.o: In function `Display::clearScreen(float, float, float, float)':
display.cpp:(.text+0x1e1): undefined reference to `glClearColor'
display.cpp:(.text+0x1eb): undefined reference to `glClear'
/tmp/cchQfbLR.o: In function `Display::~Display()':
display.cpp:(.text+0x20a): undefined reference to `SDL_GL_DeleteContext'
display.cpp:(.text+0x21a): undefined reference to `SDL_DestroyWindow'
display.cpp:(.text+0x21f): undefined reference to `SDL_Quit'
collect2: error: ld returned 1 exit status

问题是我之前在 code::blocks 中使用过完全相同的代码,并且它有效。我在其他主题中搜索了相同的错误,发现它可能与链接有关。但我看不出我做错了什么。这是我如何编译我的项目:

g++ -lGL -lGLEW -lSDL2 main.cpp display.cpp -o main.o

这是文件: main.cpp

#include <iostream>
#include "display.h"

int main(int argc, char** argv){
    Display display(800, 600, "engine");

    while(!display.isClosed()){
        display.clearScreen(.1f,.1f,.6f,1.f);

        display.update();
    }
    std::cout << "Hello world !" << std::endl;
    return 0;
}

显示.h

#ifndef DISPLAY_H
#define DISPLAY_H

#include <string>
#include <SDL2/SDL.h>

class Display
{
public:
    Display(size_t _width, size_t _height, const std::string& _title);
    void update(); 
    void swapBuffers(); 
    void clearScreen(float _r, float _g, float _b, float _a); 
    ~Display();
    bool isClosed() const {return closed;}
private:
    bool closed; 
    SDL_Window* mWindow; 
    SDL_GLContext mContext; 
};


#endif

显示.cpp

#include "display.h"
#include <GL/glew.h>
#include <iostream>

Display::Display(size_t _width, size_t _height, const std::string& _title){
    SDL_Init(SDL_INIT_EVERYTHING);

    SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 8);
    SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 8);
    SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 8);
    SDL_GL_SetAttribute(SDL_GL_ALPHA_SIZE, 8);
    SDL_GL_SetAttribute(SDL_GL_BUFFER_SIZE, 32);
    SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);

    mWindow = SDL_CreateWindow(_title.c_str(), SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, _width, _height, SDL_WINDOW_OPENGL);
    mContext = SDL_GL_CreateContext(mWindow);
    GLuint status = glewInit(); 
    if (status != GLEW_OK){
        std::cerr << "Failed to link the glew library..." << std::endl;
    }
    closed = false; 

}
void Display::update(){
    swapBuffers(); 
    SDL_Event e; 
    while(SDL_PollEvent(&e)){
        if(e.type == SDL_QUIT)
            closed = true; 
    }
} 
void Display::swapBuffers(){
    SDL_GL_SwapWindow(mWindow);
} 
void Display::clearScreen(float _r, float _g, float _b, float _a){
    glClearColor(_r, _g, _b, _a);
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
} 
Display::~Display(){
    SDL_GL_DeleteContext(mContext);
    SDL_DestroyWindow(mWindow);
    SDL_Quit(); 
}

感谢您的宝贵时间!

最佳答案

将各种 -l 标志放在命令行末尾。

关于C++ ld 返回 1 退出状态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27474067/

相关文章:

c++ - C编程: translated function from MATLAB to C gives slightly (but significantly) different result

c++ - 如何让一个函数在程序的早期运行一次?

linux - 如何使用 mingw-w64 编译和链接 32 位 Windows 可执行文件

linux - 为什么在 bash 中的 while read 循环中重定向 stdin?

c++ - OpenGL Camera Strafing 不起作用

c++ - 在 Qt 中创建 OpenGL 上下文

c++ - C++中的初始化结构错误

c++ - Qt - 如何处理对话框的内存管理?

c - Unix环境高级编程 : Get Configuration Limits Figure 2. 12 "Build C Program to print all supported configuration limits"

c++ - 3d max 与 c++ 的集成,Cal3D 从哪里开始?