Skip to content

header file 'mutilple definition' in multil-file builds #529

@gay-chicken

Description

@gay-chicken

To solve the problem of 'multiple definition', I created a pre-included header file:

#pragma once    // try this way

#ifndef PREINCLUDE_HPP
#define PREINCLUDE_HPP

#ifndef GLAD_GL_IMPLEMENTION
#define GLAD_GL_IMPLEMENTION
#include "glad/gl.h"    // only-header
#endif

#include "glfw/glfw3.h"

#endif

In the Launch.cpp file, I need to use glad to load functions.

#include "preinclude.hpp"
#include "Renderer.hpp"

bool Init() {
    if (!gladLoadGL(glfwGetProcAddress)) return false;
    return true;
}

Then, in the Renderer.cpp file, I need to use the relevant definitions from OpenGL.

#include "preinclude.hpp"

void draw() {
    glBindVertex(vao);
    glDrawArray(GL_TRIANGLES, 0, 36);
    glBindVertex(0);
}

No matter how I include 'glad', it always prompts me with 'multiple definition'.
like:

[build] D:/MinGW/bin/../lib/gcc/x86_64-w64-mingw32/14.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: CMakeFiles\engine.dir/objects.a(texture_resource.cpp.obj):D:/Desktop/ResourcesSets/Code/Project/engine/include/glad/gl.h:17298: multiple definition of `glad_glWindowRectanglesEXT'; CMakeFiles\engine.dir/objects.a(application.cpp.obj):D:/Desktop/ResourcesSets/Code/Project/engine/include/glad/gl.h:17298: first defined here
[build] D:/MinGW/bin/../lib/gcc/x86_64-w64-mingw32/14.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: CMakeFiles\engine.dir/objects.a(texture_resource.cpp.obj):D:/Desktop/ResourcesSets/Code/Project/engine/include/glad/gl.h:17299: multiple definition of `glad_glWriteMaskEXT'; CMakeFiles\engine.dir/objects.a(application.cpp.obj):D:/Desktop/ResourcesSets/Code/Project/engine/include/glad/gl.h:17299: first defined here

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions