2014-09-16 4 views
0

나는 Eliot Eshelman의 FreeGLUT, OpenGL 및 멋진 simplex 노이즈 기능을 사용하는 간단한 프로그램을 가지고 있습니다. 목표는 2D 슬라이스에 SDL로 관리 한 3D 단순 노이즈를 표시하는 것입니다.다중 정의 : 말쑥하게 함

제 문제는 해결할 수없는 여러 정의 오류가 나타납니다.

Google 및 잠시 동안이 포럼에서 검색했습니다. 나는 오류를 일으킬 수있는 몇 가지 것들에 대해 배웠지 만, 내 원인을 찾을 수없는 것 같습니다. 나는 오랜 시간 동안 이러한 실수에 곤란을 겪었다.

다음은 관련 소스 및 오류 출력입니다.

오류 발생 이전에 더 많은 버그가있을 수 있지만 아직까지는 디버그 할 수 없었습니다.

출력 :

-------------- Build: Release in OpenGL Test (compiler: GNU GCC Compiler)--------------- 

mingw32-g++.exe -Wall -O2 -std=c++11 -IC:\freeglut\include -c "C:\OpenGL Test\OpenGL Test\main.cpp" -o obj\Release\main.o 
mingw32-g++.exe -LC:\freeglut\lib -o "bin\Release\OpenGL Test.exe" obj\Release\LUtil.o obj\Release\main.o obj\Release\simplexnoise.o -s -lmingw32 -lOpenGL32 -lglu32 -lfreeglut -mwindows 
obj\Release\main.o:main.cpp:(.bss+0x0): multiple definition of `textureName' 
obj\Release\LUtil.o:LUtil.cpp:(.bss+0x0): first defined here 
obj\Release\main.o:main.cpp:(.bss+0x20): multiple definition of `noiseImage' 
obj\Release\LUtil.o:LUtil.cpp:(.bss+0x20): first defined here 
obj\Release\main.o:main.cpp:(.bss+0x12c020): multiple definition of `zOffset' 
obj\Release\LUtil.o:LUtil.cpp:(.bss+0x12c020): first defined here 
collect2.exe: error: ld returned 1 exit status 
Process terminated with status 1 (0 minute(s), 0 second(s)) 
0 error(s), 0 warning(s) (0 minute(s), 0 second(s)) 

LUtil.h :

/*This source code copyrighted by Lazy Foo' Productions (2004-2013) 
and may not be redistributed without written permission.*/ 
//Version: 001 

#ifndef LUTIL_H 
#define LUTIL_H 

#include "LOpenGL.h" 

#include "simplexnoise.h" 

//Screen Constants 
const int SCREEN_WIDTH = 640; 
const int SCREEN_HEIGHT = 480; 
const int SCREEN_FPS = 60; 

float zOffset = 0; 

float noiseImage[640][480]; 

GLuint textureName; 

bool initGL(); 
/* 
Pre Condition: 
-A valid OpenGL context 
Post Condition: 
-Initializes matrices and clear color 
-Reports to console if there was an OpenGL error 
-Returns false if there was an error in initialization 
Side Effects: 
-Projection matrix is set to identity matrix 
-Modelview matrix is set to identity matrix 
-Matrix mode is set to modelview 
-Clear color is set to black 
*/ 

void update(); 
/* 
Pre Condition: 
-None 
Post Condition: 
-Does per frame logic 
Side Effects: 
-None 
*/ 

void render(); 
/* 
Pre Condition: 
-A valid OpenGL context 
-Active modelview matrix 
Post Condition: 
-Renders the scene 
Side Effects: 
-Clears the color buffer 
-Swaps the front/back buffer 
*/ 

#endif 

LUtil.cpp :

/*This source code copyrighted by Lazy Foo' Productions (2004-2013) 
and may not be redistributed without written permission.*/ 
//Version: 001 

#include "LUtil.h" 

bool initGL() 
{ 
    //Initialize Projection Matrix 
    glMatrixMode(GL_PROJECTION); 
    glLoadIdentity(); 

    //Initialize Modelview Matrix 
    glMatrixMode(GL_MODELVIEW); 
    glLoadIdentity(); 

    //Initialize clear color 
    glClearColor(0.f, 0.f, 0.f, 1.f); 

    //Check for error 
    GLenum error = glGetError(); 
    if(error != GL_NO_ERROR) 
    { 
     printf("Error initializing OpenGL! %s\n", gluErrorString(error)); 
     return false; 
    } 

    glGenTextures(1, &textureName); 
    glBindTexture(GL_TEXTURE_2D, textureName); 

    return true; 
} 

void update() 
{ 
    for(int y = 0; y < SCREEN_HEIGHT; y++) 
    { 
     for(int x = 0; x < SCREEN_WIDTH; x++) 
     { 
       noiseImage[x][y] = raw_noise_3d(x, y, zOffset); 
       glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, SCREEN_WIDTH, SCREEN_HEIGHT, 0, GL_RGB, GL_FLOAT, &noiseImage[0][0]); 
     } 
    } 
} 

void render() 
{ 
    //Clear color buffer 
    glClear(GL_COLOR_BUFFER_BIT); 

    glColor4f(1, 0.5f, 0, 1); 

    //Render quad 
    glBegin(GL_QUADS); 
     glTexCoord2f( -1.f, -1.f); 
     glTexCoord2f( -1.f, 1.f); 
     glTexCoord2f(1.f, -0.f); 
     glTexCoord2f(1.f, 1.f); 
    glEnd(); 

    //Update screen 
    glutSwapBuffers(); 
} 

MAIN.CPP :

/*This source code copyrighted by Lazy Foo' Productions (2004-2013) 
and may not be redistributed without written permission.*/ 
//Version: 001 

#include "LUtil.h" 

void runMainLoop(int val); 
/* 
Pre Condition: 
-Initialized freeGLUT 
Post Condition: 
-Calls the main loop functions and sets itself to be called back in 1000/SCREEN_FPS milliseconds 
Side Effects: 
-Sets glutTimerFunc 
*/ 

int main(int argc, char* args[]) 
{ 
    //Initialize FreeGLUT 
    glutInit(&argc, args); 

    //Create OpenGL 2.1 context 
    glutInitContextVersion(4, 4); 

    //Create Double Buffered Window 
    glutInitDisplayMode(GLUT_DOUBLE); 
    glutInitWindowSize(SCREEN_WIDTH, SCREEN_HEIGHT); 
    glutCreateWindow("OpenGL"); 

    //Do post window/context creation initialization 
    if(!initGL()) 
    { 
     printf("Unable to initialize graphics library!\n"); 
     return 1; 
    } 

    //Set rendering function 
    glutDisplayFunc(render); 

    //Set main loop 
    glutTimerFunc(1000/SCREEN_FPS, runMainLoop, 0); 

    //Start GLUT main loop 
    glutMainLoop(); 

    return 0; 
} 

void runMainLoop(int val) 
{ 
    //Frame logic 
    update(); 
    render(); 

    //Run frame one more time 
    glutTimerFunc(1000/SCREEN_FPS, runMainLoop, val); 
} 

내가 알 수있는 한, 여러 정의는 없지만 틀릴 수도 있습니다. 다시 한번, 나는이 질문에 "그 사람"이라고 말하면서 미안합니다.

+4

, 당신이 원하지 않는 .h 파일에서 변수 (또는 객체)를 정의하십시오. 'GLuint textureName; '과 같이 전역 변수를 사용해야 할 경우 관련 .cpp 파일에서 전역 변수를 정의한 다음'extern '을 사용하여 다른 파일에서 선언하십시오. – mbadawi23

+1

** 선언 **과 ** 정의 **의 차이점을 알아야합니다. –

+1

이중 include는 문제가 아니어야합니다. 올바른 #ifndef가있는 것 같습니다. 그러나 실제로 경고를 발생시키는 정의는 .h 파일에 있어서는 안됩니다. – Pieter21

답변

6
LUtil.cpp정의를 글로벌 variabls의 이동

:

float zOffset = 0; 

float noiseImage[640][480]; 

GLuint textureName; 

그런 다음 LUtil.h variabls를 선언 :

엄지 손가락의 규칙으로
extern float zOffset; 

extern float noiseImage[640][480]; 

extern GLuint textureName;