【问题标题】:Eclipse CDT + MinGW + GLEW, Type undefined reference to `LoadShaders(char const*, char const*)'Eclipse CDT + MinGW + GLEW,键入未定义的对“LoadShaders(char const*, char const*)”的引用
【发布时间】:2015-11-26 04:32:00
【问题描述】:

LoadShaders 出现一个错误。

#include <stdio.h>
#include <stdlib.h>

#define GLEW_STATIC
#include <GL/glew.h>

#include <glfw3.h>
GLFWwindow* window;

#include <glm/glm.hpp>
using namespace glm;

#include <common/shader.hpp>

int main( void )
{
    // Initialise GLFW
    if( !glfwInit() )
    {
        fprintf( stderr, "Failed to initialize GLFW\n" );
        return -1;
    }

    glfwWindowHint(GLFW_SAMPLES, 4);
    glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
    glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
    glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);

    // Open a window and create its OpenGL context
    window = glfwCreateWindow( 1024, 768, "Tutorial 02 - Red triangle", NULL, NULL);
    if( window == NULL ){
        fprintf( stderr, "Failed to open GLFW window. If you have an Intel GPU, they are not 3.3 compatible. Try the 2.1 version of the tutorials.\n" );
        glfwTerminate();
        return -1;
    }
    glfwMakeContextCurrent(window);

    // Initialize GLEW
    glewExperimental = true; // Needed for core profile
    if (glewInit() != GLEW_OK) {
        fprintf(stderr, "Failed to initialize GLEW\n");
        return -1;
    }

    // Ensure we can capture the escape key being pressed below
    glfwSetInputMode(window, GLFW_STICKY_KEYS, GL_TRUE);

    // Dark blue background
    glClearColor(0.0f, 0.0f, 0.4f, 0.0f);

    GLuint VertexArrayID;
    glGenVertexArrays(1, &VertexArrayID);
    glBindVertexArray(VertexArrayID);

    // Create and compile our GLSL program from the shaders
    GLuint programID = LoadShaders( "SimpleVertexShader.vertexshader", "SimpleFragmentShader.fragmentshader" );


    static const GLfloat g_vertex_buffer_data[] = {
        -1.0f, -1.0f, 0.0f,
         1.0f, -1.0f, 0.0f,
         0.0f,  1.0f, 0.0f,
    };

    GLuint vertexbuffer;
    glGenBuffers(1, &vertexbuffer);
    glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer);
    glBufferData(GL_ARRAY_BUFFER, sizeof(g_vertex_buffer_data), g_vertex_buffer_data, GL_STATIC_DRAW);

    do{

        // Clear the screen
        glClear( GL_COLOR_BUFFER_BIT );

        // Use our shader
        glUseProgram(programID);

        // 1rst attribute buffer : vertices
        glEnableVertexAttribArray(0);
        glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer);
        glVertexAttribPointer(
            0,                  // attribute 0. No particular reason for 0, but must match the layout in the shader.
            3,                  // size
            GL_FLOAT,           // type
            GL_FALSE,           // normalized?
            0,                  // stride
            (void*)0            // array buffer offset
        );

        // Draw the triangle !
        glDrawArrays(GL_TRIANGLES, 0, 3); // 3 indices starting at 0 -> 1 triangle

        glDisableVertexAttribArray(0);

        // Swap buffers
        glfwSwapBuffers(window);
        glfwPollEvents();

    } // Check if the ESC key was pressed or the window was closed
    while( glfwGetKey(window, GLFW_KEY_ESCAPE ) != GLFW_PRESS &&
           glfwWindowShouldClose(window) == 0 );

    // Cleanup VBO
    glDeleteBuffers(1, &vertexbuffer);
    glDeleteVertexArrays(1, &VertexArrayID);
    glDeleteProgram(programID);

    // Close OpenGL window and terminate GLFW
    glfwTerminate();

    return 0;
}

我得到错误:

描述资源路径位置类型 未定义对 `LoadShaders(char const*, char const*)' Opengl33.cpp /Opengl33/src 第 63 行 C/C++ 问题的引用

图书馆订单加载:

glew32s 
gdi32 
opengl32 
glfw3dll

控制台中的编译器字符串:

g++ -o Opengl33.exe "src\\Opengl33.o" -lglew32s -lgdi32 -lopengl32 -lglfw3dll 

(-DGLEW_STATIC别帮我)

我试图用谷歌搜索这个问题,但没有找到解决方案。

【问题讨论】:

  • 我在这里没有看到 LoadShader 的定义,如果它是在 common/shader.hpp 中定义的,那么请同时发布这个文件。
  • #ifndef SHADER_HPP #define SHADER_HPP GLuint LoadShaders(const char * vertex_file_path,const char * fragment_file_path); #endif - 都是 shader.hpp
  • 对不起格式,不明白如何在cmets中使用[code]

标签: c++ eclipse opengl glew


【解决方案1】:

在不同文件中实现方法/类时,必须将它们中的每一个编译为目标文件,然后将它们链接在一起。在这里,您只是链接 Opengl33.o,尽管 LoadShader 是在 shader.o 中定义的。尝试将链接器行更改为:

g++ -o Opengl33.exe "src\\Opengl33.o" "common\\shader.o" -lglew32s -lgdi32 -lopengl32 -lglfw3dll 

不过,我想知道为什么这是必要的,因为您使用的是 eclipse。

【讨论】:

  • 如果您知道,请告诉我如何在 Eclipse 中进行此操作 - 添加到链接器字符串“common\\shader.o”(common\\shader.hpp 位于 MinGW 的“包含”中)。对不起,我是菜鸟,初级程序员。
  • 至少在我的eclipse中,对项目中包含的所有源文件都是自动完成的。 shader.cpp 文件是否被编译?
  • 对不起,我在哪里可以看到编译的shader.cpp?我必须搜索“shader.o”吗?请,再次为我愚蠢的问题道歉......
  • 查看控制台(您已经从中复制了链接器行)。每个 *.cpp 文件都应该有一个 g++ -c .... 行。
  • 不要在链接器字符串中看到“shader.cpp”:信息:内部构建器用于构建 g++ -O0 -g3 -Wall -c -fmessage-length=0 -o "src\\opengl33. o" "..\\src\\opengl33.cpp" g++ -o opengl33.exe "src\\opengl33.o" -lglew32 -lopengl32 -lglfw3dll src\opengl33.o:在函数main': C:\workspace\opengl33\Debug/../src/opengl33.cpp:61: undefined reference to LoadShaders(char const* , char const*)' 可能是,我必须将文件夹“common”移动到“src”? (用opengl33.cpp放置)?现在“common/shader.cpp”位于“MinGW/includes”中
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-03-29
  • 1970-01-01
  • 1970-01-01
  • 2016-01-19
  • 2013-11-09
  • 1970-01-01
相关资源
最近更新 更多