gpt4 book ai didi

c++ - C++中的多重定义

转载 作者:行者123 更新时间:2023-11-28 00:44:01 27 4
gpt4 key购买 nike

<分区>

在名为 gl_ext.h 的文件中,我有以下内容:

#ifndef GLEXT_H_INCLUDED
#define GLEXT_H_INCLUDED

#include <stdexcept>

#ifdef WIN32
#include <Windows.h>
#include <GL/GL.h>
#include <GL/glext.h>
# define glGetProcAddress(arg) wglGetProcAddress(arg)
#elif __linux__
#include <GL/gl.h>
#include <GL/glext.h>
# include <GL/glx.h>
# define glGetProcAddress(arg) glXGetProcAddress((const GLubyte*)arg)
#endif

PFNGLCREATESHADERPROC glCreateShader = 0;

namespace glext
{
bool load_gl_extensions()
{
static bool loaded = false;
if (loaded) {
return true;
}
if (!glCreateShader) {
glCreateShader =
(PFNGLCREATESHADERPROC)(glGetProcAddress("glCreateShader"));
if (!glCreateShader) {
throw "Failed to load glCreateShader";
}
}
}
}
#endif

使用以下 .pro 文件从 qt creator 中构建时

QT += core gui opengl

TEMPLATE = app
TARGET = GLExtensions
INCLUDEPATH += .

LIBS += -lGL
HEADERS += gl_ext.h \
qtrenderer.h

SOURCES += main.cpp \
qtrenderer.cpp

这个“头库”的用法如下: main.cpp

#include <QtGui/QApplication>
#include "qtrenderer.h"

int main(int argc, char * argv[]) {
QApplication app(argc, argv);

QtRenderer *renderer = new QtRenderer();
renderer->show();

app.exec();
}

qtrenderer.h

#ifndef QTRENDERER_H_INCLUDED
#define QTRENDERER_H_INCLUDED

#include <QtCore/QObject>
#include <QtOpenGL/QGLWidget>

#include <gl_ext.h>

class QtRenderer : public QGLWidget
{

Q_OBJECT

private:
QtRenderer(const QtRenderer &other);
QtRenderer &operator = (const QtRenderer &other);

protected:
virtual void paintGL();
virtual void initializeGL();

public:
QtRenderer();
~QtRenderer();

public slots:
virtual void updateGL();
};

#endif

qtrenderer.cpp

#include "qtrenderer.h"
QtRenderer::QtRenderer() :
QGLWidget() {
}

QtRenderer::~QtRenderer() {
}

void QtRenderer::initializeGL() {
try {
glext::load_gl_extensions();
} catch (...) {
throw std::runtime_error("Failed to load needed extensions.");
}
}

void QtRenderer::paintGL() {
swapBuffers();
}

void QtRenderer::updateGL() {
paintGL();
}

使用

构建此源代码时
gcc (GCC) 4.7.2 20120921 (Red Hat 4.7.2-2)

我收到以下构建错误:

qtrenderer.o: In function `glext::load_gl_extensions()':
/home/mehoggan/Devel/test_gl/./gl_ext.h:28: multiple definition of `glCreateShader'
main.o:/home/mehoggan/Devel/test_gl/./gl_ext.h:28: first defined here

为什么会这样?

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com