C++ ld 返回了 1 个存在状态
C++ ld returned 1 exist status
我正在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 标志放在命令行的末尾。
相关文章:
- C++模板来检查友元函数的存在
- 既然存在危险,为什么项目要使用-I include开关
- 我们可以访问一个不存在的联盟的成员吗
- C++:对不存在的命名空间使用命名空间指令
- C++quit()函数中可能存在作用域问题
- C++擦除(如果存在)
- g++ 说函数不存在,即使包含正确的标头
- 这个极客对极客的trie实现是否存在内存泄漏问题
- 有了gcc,是否可以链接库,但前提是它存在
- Constexpr替代了新的放置方式,可以让内存中的对象保持未初始化状态
- 我不断收到 [错误] ID 返回 1 退出状态错误,但看不到问题所在
- C++LinkedList问题.数据类型之间存在冲突?没有匹配的构造函数
- OSX MetalKit CVMetalTextureCacheCreateTextureFromImage失败,状态:
- gcc和clang在表达式是否为常量求值的问题上存在分歧
- C++Builder中的OnClick事件签名存在问题
- 如何正确地将分支添加到已存在的树中
- 使用导入的图像保存在 QT 中的 QLabel 中,保存 GUI 的状态
- C++ ld 返回了 1 个存在状态
- 是否存在将filein重置为初始状态的方法
- 在N3600的“锁存器”样品中是否存在竞争状态?