简体   繁体   English

C ++未定义引用虽然函数在同一文件中定义

[英]C++ undefined reference though function is defined in the same file

I'm currently trying to write a OpenGL program in C++. 我目前正在尝试用C ++编写OpenGL程序。 When I try to compile my program I get undefined references, everywhere where i call member functions of my class, although I call these functions from member functions... 当我尝试编译我的程序时,我得到了未定义的引用,我在任何地方调用我的类的成员函数,尽管我从成员函数中调用这些函数...

These are the error messages I get: 这些是我得到的错误消息:

make clean && make
rm -f *.o *~ main
g++   -c main.cpp  -o main.o
g++   -c renderer.cpp  -o renderer.o
g++   -o main main.o renderer.o  -lglut -lGL -lGLEW
renderer.o: In function `Renderer::resize(int, int)':
renderer.cpp:(.text+0x299): undefined reference to `Renderer::Perspective(float*, float, float, float, float)'
renderer.o: In function `Renderer::setupShaders()':
renderer.cpp:(.text+0x43d): undefined reference to `Renderer::loadShaderSrc(char const*, int&)'
renderer.cpp:(.text+0x456): undefined reference to `Renderer::loadShaderSrc(char const*, int&)'
renderer.cpp:(.text+0x4e7): undefined reference to `Renderer::printShaderInfoLog(int)'
renderer.cpp:(.text+0x4fe): undefined reference to `Renderer::printShaderInfoLog(int)'
renderer.cpp:(.text+0x568): undefined reference to `Renderer::printProgramInfoLog(int)'
collect2: Fehler: ld gab 1 als Ende-Status zurück
make: *** [main] Fehler 1

This are my source files: 这是我的源文件:

main.cpp: main.cpp中:

#include "renderer.h"

static Renderer *renderer;

static void glutDisplay()
{
    renderer->display();
    glutSwapBuffers();
}

static void glutResize(int w, int h)
{
    renderer->resize(w, h);
}

int main(int argc, char **argv)
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE);
    glutInitWindowPosition(100, 100);
    glutInitWindowSize(320, 320);
    glutCreateWindow("Title");

    GLenum res = glewInit();
    if(res != GLEW_OK)
        return -1;

    glutDisplayFunc(glutDisplay);
    glutReshapeFunc(glutResize);
    renderer = new Renderer;
    renderer->init();
    glutMainLoop();
    return 0;
}

renderer.cpp: renderer.cpp:

#include "renderer.h"

Renderer::Renderer() : programID(0.0), vao(0), vbo(0), vertexLoc(0), colorLoc(0), projectionLoc(0), modelviewLoc(0)
{}

Renderer::~Renderer()
{
    glDeleteVertexArrays(1, &vao);
    glDeleteBuffers(1, &vbo);
    glDeleteProgram(programID);
    glDeleteShader(vertShaderID);
    glDeleteShader(fragShaderID);
}

void Renderer::init()
{
    glEnable(GL_DEPTH_TEST);
    glEnable(GL_DEPTH);
    glClearColor(0.0f, 0.0f, 0.0f, 0.0f);

    setupShaders();

    //glGenVertexArrays(1, vao);
    glGenBuffers(1, &vbo);

    float triangleVertexData[] = {
       0.0f, 0.5f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f,
      -0.5f,-0.5f, 0.0f, 0.0f, 1.0f, 0.0f, 1.0f,
       0.5f,-0.5f, 0.0f, 0.0f, 0.0f, 1.0f, 1.0f,
    };

    glBindBuffer(GL_ARRAY_BUFFER, vbo);
    glBufferData(GL_ARRAY_BUFFER, sizeof(triangleVertexData), triangleVertexData, GL_STATIC_DRAW);
    glBindBuffer(GL_ARRAY_BUFFER, 0);
}

void Renderer::resize(int w, int h)
{
    glViewport(0, 0, w, h);
    Perspective(projection, 45.0f, (float)w/(float)h, 0.5f, 4.0f);
}

void Renderer::display()
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    glUseProgram(programID);
    glUniformMatrix4fv(projectionLoc, 1, GL_FALSE, projection);
    glUniformMatrix4fv(modelviewLoc, 1, GL_FALSE, modelview);

    glBindBuffer(GL_ARRAY_BUFFER, vbo);
    glEnableVertexAttribArray(vertexLoc);
    glVertexAttribPointer(vertexLoc, 3, GL_FLOAT, GL_FALSE, 7 * sizeof(float), 0);
    glDisableVertexAttribArray(vertexLoc);
    glEnableVertexAttribArray(colorLoc);
    glVertexAttribPointer(colorLoc, 3, GL_FLOAT, GL_FALSE, 7 * sizeof(float), (void*) (3 * sizeof(float)));
    glDisableVertexAttribArray(colorLoc);
    glBindBuffer(GL_ARRAY_BUFFER, 0);
}

void Renderer::setupShaders()
{
    vertShaderID = glCreateShader(GL_VERTEX_SHADER);
    fragShaderID = glCreateShader(GL_FRAGMENT_SHADER);

    GLint vertlength, fraglength;
    const char* vertexshader = loadShaderSrc("shaders/simple.vert", vertlength);
    const char* fragmentshader = loadShaderSrc("shaders/simple.frag", fraglength);

    glShaderSource(vertShaderID, 1, &vertexshader, &vertlength);
    glShaderSource(fragShaderID, 1, &fragmentshader, &fraglength);

    free((char*) vertexshader);
    free((char*) fragmentshader);

    glCompileShader(vertShaderID);
    glCompileShader(fragShaderID);

    printShaderInfoLog(vertShaderID);
    printShaderInfoLog(fragShaderID);

    programID = glCreateProgram();
    glAttachShader(programID, vertShaderID);
    glAttachShader(programID, fragShaderID);

    glLinkProgram(programID);
    printProgramInfoLog(programID);

    vertexLoc = glGetAttribLocation(programID, "vertex_position");
    colorLoc = glGetAttribLocation(programID, "vertex_color");

    projectionLoc = glGetUniformLocation(programID, "projection");
    modelviewLoc = glGetUniformLocation(programID, "modelview");
}

void printShaderInfoLog(GLint obj)
{
    int infoLogLength = 0;
    int returnLength  = 0;
    char *infoLog;
    glGetShaderiv(obj, GL_INFO_LOG_LENGTH,&infoLogLength);
    if (infoLogLength > 0) {
      infoLog = (char *)malloc(infoLogLength);
      glGetShaderInfoLog(obj, infoLogLength, &returnLength, infoLog);
      printf("%s\n",infoLog);
      free(infoLog);
    }   
}

void printProgramInfoLog(GLint obj)
{
    int infoLogLength = 0;
    int returnLength  = 0;
    char *infoLog;
    glGetProgramiv(obj, GL_INFO_LOG_LENGTH,&infoLogLength);
    if (infoLogLength > 0) {
      infoLog = (char *)malloc(infoLogLength);
      glGetProgramInfoLog(obj, infoLogLength, &returnLength, infoLog);
      printf("%s\n",infoLog);
      free(infoLog);
    }
}

const char* loadShaderSrc(const char *filename, GLint &filesize)
{
    std::ifstream file(filename, std::ios::in);
    std::string shader( (std::istreambuf_iterator<char>(file) ),
                        (std::istreambuf_iterator<char>()     ) );
    filesize = (GLint) shader.length();
    return shader.c_str();
}

void Perspective(float *a, float fov, float aspect, float zNear, float zFar)
{
    float f = 1.0f / float(tan(fov / 2.0f * (PI / 180.0f)));
    for(int i = 0; i < 16; i++) a[i] = 0.0f;
    a[0] = f / aspect;
    a[1 * 4 + 1] = f;
    a[2 * 4 + 2] = (zNear + zFar) / (zNear - zFar);
    a[3 * 4 + 2] = 2.0f * zNear * zFar / (zNear - zFar);
    a[2 * 4 + 3] = -1.0f;
}

void Modelview(float *mv)
{
    for(int i = 0; i < 16; i++) mv[i] = 0.0f;
    for(int i = 0; i < 4; i++) mv[i * 4 + i] = 1.0f;
    mv[3 * 4 + 2] = -2.0f;
}

renderer.h: renderer.h:

#include <cmath>
#include <stdio.h>
#include <iostream>
#include <fstream>
#include <string>
#include <GL/glew.h>
#include <GL/glut.h>

#define PI 3.14159

class Renderer {
    private:
        GLuint programID;
        GLuint vertShaderID;
        GLuint fragShaderID;
        GLuint vao;
        GLuint vbo;
        GLint vertexLoc;
        GLint colorLoc;
        GLint projectionLoc;
        GLint modelviewLoc;
        float projection[16];
        float modelview[16];
    public:
        Renderer();
        ~Renderer();
        void init();
        void resize(int w, int h);
        void display();
    private:
        void setupShaders();
        void printShaderInfoLog(GLint obj);
        void printProgramInfoLog(GLint obj);
        const char* loadShaderSrc(const char *filename, GLint &filesize);
        void Perspective(float *a, float fov, float aspect, float zNear, float zFar);
        void Modelview();
};

I compile using this Makefile (using g++ with linux): 我使用这个Makefile编译(使用g ++和linux):

CC = g++
CFLAGS = -Wall -g
INCLUDES = 
LFLAGS = 
LIBS = -lglut -lGL -lGLEW
SRCS = main.cpp renderer.cpp
OBJS = $(SRCS:.cpp=.o)
MAIN = main

all:        $(MAIN)

$(MAIN):    $(OBJS)
            $(CC) $(CLFAGS) $(INCLUDES) -o $(MAIN) $(OBJS) $(LFLAGS) $(LIBS)

.cpp.o:     
            $(CC) $(CLFAGS) $(INCLUDES) -c $<  -o $@

clean:      
            $(RM) *.o *~ $(MAIN)

It's probably just a stupid beginner mistake but I cant find it. 这可能只是一个愚蠢的初学者错误,但我找不到。 I simply don't understand how I can get a undefined reference to a function if that function is defined in the same file. 如果在同一个文件中定义了该函数,我根本不明白如何获取函数的未定义引用。

I hope you can help me =) 我希望你能帮助我=)

In your cpp file the Perspective function is called Perspective rather than Renderer::Perspective . 在您的cpp文件中, Perspective函数名为Perspective而不是Renderer::Perspective This means that the Perspective method is not actually defined, even though there is now an unused function called Perspective . 这意味着实际上没有定义Perspective方法,即使现在有一个名为Perspective的未使用函数。

Note: you have the same issue with other private methods of your Renderer class. 注意:您与Renderer类的其他private方法存在相同的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 C ++未定义对头文件外部定义的成员函数的引用 - c++ undefined reference to member function defined outside of header file C++ 对已定义函数的未定义引用 - C++ undefined reference to defined function 在C ++中对未定义函数的未定义引用? - undefined reference to a defined function in c++? 对同一文件中定义的函数的未定义引用 - Undefined reference to a function defined in the same file 从C ++到C文件中定义的变量/函数的未定义引用 - Undefined reference from C++ to variables/functions defined in C file 对通过静态库调用的同一.c文件中定义的函数的未定义引用 - Undefined reference to Function defined in same .c file invoked through static library 即使我已经定义了所有功能,也无法获得对C ++中错误的未定义引用 - Getting an undefined reference to error in c++ even though I have the functions all defined 编译具有相同标头的 C 和 C++ 文件时未定义的引用 - Undefined reference when compiling a C and C++ file with same header 为什么编译器会抱怨对constexpr函数的未定义引用,即使它是在另一个源文件中定义的? - Why does the compiler complain about undefined reference to a constexpr function even though it is defined in another source file? 在函数中未定义的参考C ++ - In function undefined reference c++
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM