ld返回1个退出状态

ld返回1个退出状态

我正在OpenGL项目上的Linux中工作。但是,当我尝试使用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;
}


display.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


display.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++ - C++ ld返回1个退出状态,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/27474067/

10-10 21:26