C++ ld 返回了 1 个存在状态

C++ ld returned 1 exist status

本文关键字:存在 状态 ld 返回 C++      更新时间:2023-10-16

我正在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

这是文件:主.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 标志放在命令行的末尾。