Identifier not found error although having declared it in the header file

A simple question, why do I get ['closeglobal':identifier not found] when compiling the program? Thank you.



The content of the initglobal.h:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18

#ifndef INITGLOBAL_H
#define INITGLOBAL_H


#include <SDL.h>


SDL_Window* gWindow;

const int WINDOW_HEIGHT = 680;
const int WINDOW_WIDTH = 680;

void initWindow();



#endif 


The content of the initglobal.cpp:
1
2
3
4
5
6
7
8
9
#include "initglobal.h"	


void initWindow()
{
	gWindow = SDL_CreateWindow("Boom", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, WINDOW_WIDTH, WINDOW_HEIGHT, SDL_WINDOW_SHOWN);


}


The content of the closeglobal.h:
1
2
3
4
5
6
7
8
9
10
11
#ifndef INITGLOBAL_H
#define INITGLOBAL_H

#include <iostream>
#include <SDL.h>

extern SDL_Window* gWindow;

void closeglobal();

#endif 


The content of the closeglobal.cpp:
1
2
3
4
5
6
7
#include "closeglobal.h"

void closeglobal()
{
	SDL_DestroyWindow(gWindow);
	gWindow = nullptr;
}


The content of the main.cpp:
1
2
3
4
5
6
7
8
9
10
11
12
#include <SDL.h>
#include "initglobal.h"
#include "closeglobal.h"	


int main(int argc, char* args[])
{
	initWindow();
	closeglobal();

	return 0;
}

You have forgotten to link closeglobal.o to your application.

BTW, you should not be defining any globals in header files. That program has two gWindow objects in it -- one in main.o and one in initglobal.cpp.

initglobal.h
1
2
3
4
5
6
7
8
9
10
11
12
13
#ifndef INITGLOBAL_H
#define INITGLOBAL_H

#include <SDL.h>

extern SDL_Window* gWindow;

const int WINDOW_HEIGHT = 680;
const int WINDOW_WIDTH = 680;

void initWindow();

#endif 

initglobal.cpp
1
2
3
4
5
6
7
8
#include "initglobal.h"	

SDL_Window* gWindow;

void initWindow()
{
	gWindow = SDL_CreateWindow("Boom", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, WINDOW_WIDTH, WINDOW_HEIGHT, SDL_WINDOW_SHOWN);
}

There is no reason that gWindow should be mentioned in closeglobal.h. Also, don't reuse guard tags.

closeglobal.h
1
2
3
4
5
6
7
8
9
#ifndef CLOSEGLOBAL_H
#define CLOSEGLOBAL_H

#include <iostream>
#include <SDL.h>

void closeglobal();

#endif  


closeglobal.cpp
1
2
3
4
5
6
7
8
#include "initglobal.h"
#include "closeglobal.h"

void closeglobal()
{
	SDL_DestroyWindow(gWindow);
	gWindow = nullptr;
}


One more thing: there is a design flaw here. You have two modules to play with one thing. Don't split them up.

global.h
1
2
3
4
5
6
7
8
9
#ifndef GLOBAL_H
#define GLOBAL_H

extern SDL_Window* gWindow;

void initWindow();
void closeWinow();

#endif 

global.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include "global.h"

SDL_Window* gWindow;

void initWindow()
{
	gWindow = SDL_CreateWindow("Boom", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, WINDOW_WIDTH, WINDOW_HEIGHT, SDL_WINDOW_SHOWN);
}

void closeWindow()
{
	SDL_DestroyWindow(gWindow);
	gWindow = nullptr;
}

main.cpp
1
2
3
4
5
6
7
8
9
10
11
#include <SDL.h>

#include "global.h"	

int main(int argc, char* args[])
{
	initWindow();
	closeWindow();

	return 0;
}

You should also develop a habit of naming paired things in a way that shows them as paired. As it was, who would know that "initWindow" and "closeglobal" are related, without having read the code? And the name should be specific to the action. "initialize" talks about messing something that already exists. When you call "initWindow", no window exists -- it both creates and initializes the window.

I would have named them something like
- OpenWindow, CloseWindow
- CreateWindow, DestroyWindow
- NewWindow, FreeWindow

Hope this helps.
I am a beginner, so this really helps. Thank you.
Topic archived. No new replies allowed.