【发布时间】:2015-03-05 06:21:36
【问题描述】:
我正在尝试让一个项目使用 SDL2、assimp、opengl 和 glew,但无法正确链接(我认为几乎是重复的,但更全面的问题和答案)。
我的 qmake .pro 文件:
CONFIG += console
CONFIG -= app_bundle
CONFIG -= qt
HEADERS += \
objloader.h \
display.h \
eventloop.h \
shader.h \
texture.h
SOURCES += main.cpp \
objloader.cpp \
display.cpp \
eventloop.cpp \
shader.cpp \
texture.cpp
INCLUDEPATH += "C:/code middleware/glew-1.12.0/include/" \
"C:\code middleware\glm\\" \
"C:\code middleware\SDL2-2.0.3\include\\" \
"C:\Users\smart_000\Desktop\assimp\include\\"
DEPENDPATH += "C:/code middleware/glew-1.10.0/bin/Release\Win32/" \
"C:\code middleware\SDL2-2.0.3\lib/x86/" \
"C:/Users/smart_000/Desktop/assimp/bin/" \
"C:/code middleware/glew-1.12.0/lib/Release/Win32/" \
"C:/code middleware/glew-1.12.0/bin/Release/Win32/"
LIBS += -L"C:\code middleware\SDL2-2.0.3\lib\x86" -lSDL2main -lSDL2
LIBS += -L"C:/Users/smart_000/Desktop/assimp/lib/" -lassimp.dll -lzlib
LIBS += -lopengl32
LIBS += -lglu32
LIBS += -L"C:/code middleware/glew-1.12.0/lib/Release/Win32/" -lglew32 -lglu32
我得到的链接器错误:linker errors
我已经尝试过的:
- 检查文件路径问题('/' 和 '\' 类型的内容以及文件名中的空格等)
- 动态和静态链接(是的,我在需要的地方定义了
GLEW_STATIC,并链接到 -lglew32s 等) - SDL 的东西:在上面弄乱并定义“NO_SDL_GLEXT”
制作测试程序并尝试仅将其与 g++ 链接(我在尝试静态选项时使用了 -static 标志,并将链接器标志放在需要它们的文件之后)。这些错误与 qt 的错误相同,但没有警告。
将 glew 源代码直接放在我的项目中并构建它(完全相同的错误)--- 问题非常相似,对我来说毫无意义。我已经注释掉了 qmake 的东西并重新运行了 qmake;我不知道如果我像以前那样把它放在我的项目中,我会得到这样的链接器错误。
- 修改后运行 qmake,哈哈
- 在绝望中移动“LIBS”和“DEPENDPATH”并添加了“CONFIG += opengl”
- 搞乱调试和发布版本。
我没有尝试过的:
- 将库和事物移动到系统路径
- 确保库是为 MINGW 构建的!
相关代码(我知道这很糟糕;当我遇到这些问题时,我只是在乱搞):
“main.cpp”:
#include <GL/glew.h>
#include <iostream>
#include <SDL.h>
#include <objloader.h>
#include "display.h"
#include "eventloop.h"
Display* display = NULL;
void callback()
{
std::cout << "yay" << std::endl;
display->Swap();
}
int main(int argc, char* argv[])
{
UShortVector indices;
Vec3Vector vertices;
Vec2Vector uvs;
Vec3Vector normals;
bool success = ObjLoader::loadObj("cube.obj", indices, vertices, uvs, normals);
display = new Display();
EventLoop eventLoop;
eventLoop.SetLoopCallback(&callback);
display->Create("yay");
eventLoop.Start();
return 0;
}
“显示.h”
#ifndef DISPLAY_H
#define DISPLAY_H
#include <GL/glew.h>
#include <SDL.h>
#include <iostream>
class Display
{
public:
Display();
~Display();
void Create(const char* title);
void Swap();
SDL_Window* window_ = NULL;
SDL_GLContext glContext_;
};
#endif // DISPLAY_H
“shader.h”
#ifndef SHADER_H
#define SHADER_H
#include <GL/glew.h>
#include <stdio.h>
#include <stdlib.h>
#include <vector>
#include <memory.h>
class Shader
{
public:
GLuint LoadShaders(const char * vertex_file_path, const char * fragment_file_path);
private:
Shader();
~Shader();
};
#endif // SHADER_H
“shader.cpp”
#include "shader.h"
Shader::Shader()
{
}
Shader::~Shader()
{
}
GLuint Shader::LoadShaders(const char * vertexShaderPath, const char * fragmentShaderPath)
{
FILE* vertexFile = fopen(vertexShaderPath, "r");
FILE* fragmentFile = fopen(fragmentShaderPath, "r");
if(!vertexFile || !fragmentFile)
{
if(!vertexFile)
{
perror("could not open the vertex shader file");
}
if(!fragmentFile)
{
perror("could not open the fragment shader file");
}
}
GLuint programID = glCreateProgram();
GLuint vertShader = glCreateShader(GL_VERTEX_SHADER);
GLuint fragShader = glCreateShader(GL_FRAGMENT_SHADER);
const size_t MAX_LINE_LENGTH = 120;
char line[MAX_LINE_LENGTH];
std::vector<GLchar*> vertShaderSource;
std::vector<GLchar*> fragmentShaderSource;
while(fgets(line, MAX_LINE_LENGTH, vertexFile) != NULL)
{
vertShaderSource.push_back((GLchar*)line);
memset((void*)line, '\0', sizeof(line));
}
while(fgets(line, MAX_LINE_LENGTH, fragmentFile) != NULL)
{
fragmentShaderSource.push_back((GLchar*)line);
memset((void*)line, '\0', sizeof(line));
}
glShaderSource(vertShader, vertShaderSource.size(), (const GLchar**)&vertShaderSource, NULL);
glShaderSource(fragShader, fragmentShaderSource.size(), (const GLchar**)&fragmentShaderSource, NULL);
// doesn't do anything at the moment, I know. It should still compile though.
return programID;
}
【问题讨论】:
标签: opengl linker qt-creator qmake glew