Unresolved External yet defined constructor? - c++

I can not for the life of me figure out why the implementation functions aren't being seen and the call to the constructor is unresolved. Any ideas would be appreciated.
Error:
1>Debug\MeGLWindow.obj : warning LNK4042: object specified more than once; extras ignored
1>MeApp.obj : error LNK2019: unresolved external symbol "public: __thiscall MeGLWindow::MeGLWindow(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >)" (??0MeGLWindow##QAE#V?$basic_string#DU?$char_traits#D#std##V?$allocator#D#2##std###Z) referenced in function "public: __thiscall MeApp::MeApp(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >)" (??0MeApp##QAE#V?$basic_string#DU?$char_traits#D#std##V?$allocator#D#2##std###Z)
1>C:\Users\FrizzleFry\documents\visual studio 2012\Projects\ConsoleApplication1\Debug\ConsoleApplication1.exe : fatal error LNK1120: 1 unresolved externals
App Header
#ifndef ME_APP
#define ME_APP
#include "MeGLWindow.h"
#include <string>
class MeApp {
private:
MeGLWindow* meWind;
std::string app;
public:
MeApp() {}
MeApp(std::string);
~MeApp();
void run();
};
#endif
The implementation
#include "MeApp.h"
#include <SDL.h>
#include <gl\glew.h>
#include <SDL_opengl.h>
#include <gl\glu.h>
#include <string>
MeApp::MeApp(std::string appName) {
app = appName;
meWind = new MeGLWindow(app);
}
MeApp::~MeApp() {
//delete[] meWind;
//meWind = NULL;
}
void MeApp::run() {
bool quit = false;
//Event handler
SDL_Event e;
//Enable text input
SDL_StartTextInput();
//While application is running
while( !quit )
{
//Handle events on queue
while( SDL_PollEvent( &e ) != 0 )
{
//User requests quit
if( e.type == SDL_QUIT )
{
quit = true;
}
//Handle keypress with current mouse position
else if( e.type == SDL_TEXTINPUT )
{
int x = 0, y = 0;
SDL_GetMouseState( &x, &y );
//handleKeys( e.text.text[ 0 ], x, y );
}
//meWind->show();
}
//Disable text input
SDL_StopTextInput();
}
}
The window header and implementation:
#ifndef ME_GL_WINDOW
#define ME_GL_WINDOW
#include "SDL.h"
#include "GL\glew.h"
#include "SDL_opengl.h"
#include "GL\glu.h"
#include <string>
class MeGLWindow {
private:
std::string appName;
int SCREEN_WIDTH, SCREEN_HEIGHT;
SDL_Window* meWind;
SDL_GLContext meContext;
public:
MeGLWindow() {}
MeGLWindow(std::string);
~MeGLWindow();
void show();
};
#endif
cpp
#include <SDL.h>
#include <gl\glew.h>
#include <SDL_opengl.h>
#include <gl\glu.h>
#include <string>
#include "MeGLWindow.h"
#include <iostream>
#include <string>
MeGLWindow::MeGLWindow(std::string app) {
appName = app;
if(SDL_Init( SDL_INIT_VIDEO) < 0) {
std::cout << "Failed to initilialize SDL!" << std::endl;
exit(1);
}
SCREEN_HEIGHT = 640;
SCREEN_WIDTH = 480;
//Use OpenGL 3.1 core
SDL_GL_SetAttribute( SDL_GL_CONTEXT_MAJOR_VERSION, 3 );
SDL_GL_SetAttribute( SDL_GL_CONTEXT_MINOR_VERSION, 1 );
SDL_GL_SetAttribute( SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE );
//Create window
meWind = SDL_CreateWindow( appName.c_str() , SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_OPENGL | SDL_WINDOW_SHOWN );
if( meWind == NULL )
{
std::cout << "Failed to create window!" << std::endl;
exit(2);
}
meContext = SDL_GL_CreateContext( meWind );
if( meContext = NULL ) {
std::cout << "Failed to create GL Context!" << std::endl;
exit(3);
}
glewExperimental = GL_TRUE;
GLenum glewerror = glewInit();
if( glewerror != GLEW_OK )
{
std::cout << "Error initializing GLEW!" << std::endl;
exit(4);
}
//use vsync
if( SDL_GL_SetSwapInterval( 1 ) < 0 )
{
std::cout << "Unable to set vsync!" << std::endl;
//printf( "warning: unable to set vsync! sdl error: %s\n", sdl_geterror() );
}
//initialize opengl
//( !initgl() )
}
MeGLWindow::~MeGLWindow() {
//glDeleteProgram( gProgramID );
//Destroy window
SDL_DestroyWindow( meWind );
meWind = NULL;
//Quit SDL subsystems
SDL_Quit();
}
void MeGLWindow::show() {
glClear( GL_COLOR_BUFFER_BIT );
SDL_GL_SwapWindow( meWind );
}
call file
#include <SDL.h>
#include <gl\glew.h>
#include <SDL_opengl.h>
#include <gl\glu.h>
#include <stdio.h>
#include <string>
#include "MeApp.h"
int main( int argc, char* args[] )
{
MeApp app("My Application");
app.run();
return 0;
}
EDIT
Well, I thought that maybe the .obj file was corrupted and had tried deleting it and rebuilding. They were rebuilt but still gave me this error.
If I introduce non-sensical code (I added the line a in the MeGLWindow.h as a class member) and the build gloriously failed due to:
1>c:\users\frizzlefry\documents\visual studio 2012\projects\consoleapplication1\consoleapplication1\meglwindow.h(22): error C2146: syntax error : missing ';' before identifier 'MeGLWindow'
1>c:\users\frizzlefry\documents\visual studio 2012\projects\consoleapplication1\consoleapplication1\meglwindow.h(22): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>
1>Build FAILED.
I then went to the project directory and deleted the entire debug folder save the two .dll files.
I then deleted the nonsensical code and rebuilt the solution to no avail. I received the same unresolved external symbol error.
When I originally created the files, I accidently named the file meGLWindow.h and I started getting this error after renaming the file. It wouldn't let me just rename the file with a capital, it would say that the file already existed. So I renamed it to something different, (2MeGLWindow.h) and then deleted the original file in the project folder and renamed it back to MeGLWindow.h
Then this error occured (but I had added code and didn't think this was the cause) especially since if I remove the MeGLWindow function calls (like new MeGLWindow(app);) then no build error occurs. It's only if I try to have either the main function or the app class create an instance of MeGLWindow.
I'm considering just copy pasta into a new solution. But maybe there are some sort of list of source files in the solution that are pointing to the wrong meGLWindow.h file that I originally created?
EDIT2
Just got fed up, it was 5 files. I made a new project, copy pasted the header and cpp files and it works like a charm. I will never simply rename a header file ever again.

Warning LNK4042 is a bit deceptive, actually it means that object FILE was specified more that once, so for some reason you already have MeGLWindow.obj, and it does not contain a symbol for the MeGLWindow(std::string), hence the error. Possible reasons are:
Build can't delete old MeGLWindow.obj from the times when that constructor was not implemented yet.
Build doesn't even try to delete old MeGLWindow.obj.
Some other MeGLWindow.obj pops up in the build before the proper one.
Solution: try clean build/rebuild/manually deleting Debug/MeGLWindow.obj; Carefully check build options, output file names. Check security/UAC/access rights. Hopefully that helps.

Related

Error multiple definition when compiling using headers [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
I am having a lot of trouble with my header files and the compilation. I have everything linked with header guards but for some reason I am still getting a lot of multiple definition errors. I'm also look for help on better way to organize code. Whatever help is appreciated!
This is the out output of my console when I do the g++ call:
g++ main.cpp close.cpp init.cpp load_media.cpp texture.cpp -w -lSDL2 -lSDL2_image -lSDL2_mixer -lSDL2_ttf -o run
/tmp/cc3oNgPs.o:(.bss+0x0): multiple definition of `g_font'
/tmp/ccg0hCKW.o:(.bss+0x0): first defined here
/tmp/cc3oNgPs.o:(.bss+0x8): multiple definition of `g_window'
/tmp/ccg0hCKW.o:(.bss+0x8): first defined here
/tmp/cc3oNgPs.o:(.bss+0x10): multiple definition of `g_renderer'
/tmp/ccg0hCKW.o:(.bss+0x10): first defined here
/tmp/cc3oNgPs.o:(.bss+0x20): multiple definition of `g_text_texture'
/tmp/ccg0hCKW.o:(.bss+0x20): first defined here
/tmp/ccIgzhbZ.o:(.bss+0x0): multiple definition of `g_font'
/tmp/ccg0hCKW.o:(.bss+0x0): first defined here
/tmp/ccIgzhbZ.o:(.bss+0x8): multiple definition of `g_window'
/tmp/ccg0hCKW.o:(.bss+0x8): first defined here
/tmp/ccIgzhbZ.o:(.bss+0x10): multiple definition of `g_renderer'
/tmp/ccg0hCKW.o:(.bss+0x10): first defined here
/tmp/ccIgzhbZ.o:(.bss+0x20): multiple definition of `g_text_texture'
/tmp/ccg0hCKW.o:(.bss+0x20): first defined here
/tmp/ccQs9gPv.o:(.bss+0x0): multiple definition of `g_font'
/tmp/ccg0hCKW.o:(.bss+0x0): first defined here
/tmp/ccQs9gPv.o:(.bss+0x8): multiple definition of `g_window'
/tmp/ccg0hCKW.o:(.bss+0x8): first defined here
/tmp/ccQs9gPv.o:(.bss+0x10): multiple definition of `g_renderer'
/tmp/ccg0hCKW.o:(.bss+0x10): first defined here
/tmp/ccQs9gPv.o:(.bss+0x20): multiple definition of `g_text_texture'
/tmp/ccg0hCKW.o:(.bss+0x20): first defined here
/tmp/ccxzUgM2.o:(.bss+0x0): multiple definition of `g_font'
/tmp/ccg0hCKW.o:(.bss+0x0): first defined here
/tmp/ccxzUgM2.o:(.bss+0x8): multiple definition of `g_window'
/tmp/ccg0hCKW.o:(.bss+0x8): first defined here
/tmp/ccxzUgM2.o:(.bss+0x10): multiple definition of `g_renderer'
/tmp/ccg0hCKW.o:(.bss+0x10): first defined here
/tmp/ccxzUgM2.o:(.bss+0x20): multiple definition of `g_text_texture'
/tmp/ccg0hCKW.o:(.bss+0x20): first defined here
collect2: error: ld returned 1 exit status
here are my 2 header files:
isolation.h
//include //include guard
#ifndef ISOLATION_H
#define ISOLATION_H
//include dependencies
#include <SDL2/SDL.h>
#include <SDL2/SDL_image.h>
#include <SDL2/SDL_ttf.h>
#include <stdio.h>
#include <string>
//include headers
#include "texture.h"
//Screen dimension constants
const int SCREEN_WIDTH = 1280;
const int SCREEN_HEIGHT = 800;
//forward delcarlation
//class Texture;
//start up SDL create window
bool init();
//load all media
bool load_media();
//free all and shut down SDL
void close();
//load global front
TTF_Font* g_font = NULL;
//window
SDL_Window* g_window = NULL;
//renderer
SDL_Renderer* g_renderer = NULL;
//load jpeg + font
//Texture background_texture;
//rendered font texture
Texture g_text_texture;
#endif
texture.h
//include guard
#ifndef TEXTURE_H
#define TEXTURE_H
//include dependencies
#include <SDL2/SDL.h>
#include <SDL2/SDL_image.h>
#include <SDL2/SDL_ttf.h>
#include <stdio.h>
#include <string>
//include headers
//#include "isolation.h"
class Texture {
public:
//initializes variables
Texture();
//deallocates memory
~Texture();
//load image from path
bool load_from_file( std::string path );
//create image from font string
bool load_from_rendered_text( std::string textureText, SDL_Color text_color );
//deallocates texture
void free();
//set color modulation
void set_color( Uint8 red, Uint8 green, Uint8 blue );
//set blend mode
void set_blend_mode( SDL_BlendMode blending );
//set alpha
void set_alpha( Uint8 alpha );
//render texture at point
void render( int x, int y, SDL_Rect* clip = NULL, double angle = 0.0, SDL_Point* center = NULL, SDL_RendererFlip flip = SDL_FLIP_NONE ) const;
//get image dimensions
int get_width() const;
int get_height() const;
private:
//texture pointer
SDL_Texture* m_texture;
//dimensions
int m_width;
int m_height;
};
#endif
init.cpp
#include "isolation.h"
//#include "texture.h"
bool init() {
//initialization flag
bool success = true;
//initialize SDL
if ( SDL_Init( SDL_INIT_VIDEO ) < 0 ) {
printf( "SDL could not initialized. SDL Error: %s\n", SDL_GetError() );
success = false;
}
else {
//set texture filtering linear
if ( !SDL_SetHint( SDL_HINT_RENDER_SCALE_QUALITY, "1" ) ) {
printf( "Warning: Linear filtering not enabled\n" );
}
//create window
g_window = SDL_CreateWindow( "Isolation", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN );
if ( g_window == NULL ) {
printf( "Window could not be created. SDL Error: %s\n", SDL_GetError() );
success = false;
}
else {
//create vsynced renderer
g_renderer = SDL_CreateRenderer( g_window, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC );
if ( g_renderer == NULL ) {
printf( "Renderer could not be created. SDL Error: %s\n", SDL_GetError() );
success = false;
}
else {
//initialize renderer color
SDL_SetRenderDrawColor (g_renderer, 0xFF, 0xFF, 0xFF, 0xFF );
//initialize JPEG loading
int img_flags = IMG_INIT_JPG;
if ( !( IMG_Init( img_flags ) & img_flags ) ) {
printf( "SDL_image could not be initialize. SDL_image Error: %s\n", IMG_GetError() );
success = false;
}
//initialize SDL_ttf
if (TTF_Init() == -1 ) {
printf( "SDL_ttf could not be initialize. SDL_ttf Error: %s\n", TTF_GetError() );
}
}
}
}
return success;
}
load_media.cpp
#include "isolation.h"
//s#include "texture.h"
bool load_media() {
bool success = true;
//load background img
//if( !background_texture.load_from_file( "img/vancouver.jpg" ) ) {
// printf( "Failed to load background texture!\n" );
// success = false;
//}
//open font
g_font = TTF_OpenFont( "lazy.ttf", 28 );
if ( g_font == NULL ) {
printf( "Failed to load lazy font. SDL_ttf Error: %s\n", TTF_GetError() );
success = false;
}
else {
//render texture
SDL_Color text_color = { 0, 0, 0 };
if ( !g_text_texture.load_from_rendered_text( "hello from the other side ", text_color ) ) {
printf( "Failed to render text texture\n" );
success = false;
}
}
return success;
}
close.cpp
#include "isolation.h"
//#include "texture.h"
void close() {
//free loaded text
g_text_texture.free();
//free font
TTF_CloseFont( g_font );
g_font = NULL;
//destroy window
SDL_DestroyWindow( g_window );
SDL_DestroyRenderer( g_renderer );
g_window = NULL;
g_renderer = NULL;
//quit SDL subsystems
TTF_Quit();
IMG_Quit();
SDL_Quit();
}
main.cpp
#include "isolation.h"
//#include "texture.h"
int main( int argc, char* args[] ) {
//start up SDL
if ( !init() ) {
printf( "Failed to initialize.\n" );
}
else {
//load media
if ( !load_media() ) {
printf( "Failed to load media.\n" );
}
else{
//main loop flag
bool quit = false;
//event handler
SDL_Event e;
//while running
while ( !quit ) {
//handle events on queue
while ( SDL_PollEvent( &e ) != 0 ) {
//user quit
if ( e.type == SDL_QUIT ) {
quit = true;
}
}
//clear screen
SDL_SetRenderDrawColor( g_renderer, 0xFF, 0xFF, 0xFF, 0xFF );
SDL_RenderClear( g_renderer );
//render frame
g_text_texture.render( ( SCREEN_WIDTH - g_text_texture.get_width() ) / 2, ( SCREEN_HEIGHT - g_text_texture.get_height() ) / 2 );
//update screen
SDL_RenderPresent( g_renderer );
}
}
}
//free memory and close SDL
close();
return 0;
}
Do not mix declaration with definition/instantiation in a .h file. Your are instantiating g_font, g_window and g_renderer in isolation.h file. The correct is instantiating only once, usually, in a .cpp
To solve your problem, change isolation.h to declare those variables as external linkage:
//load global front
extern TTF_Font* g_font;
//window
extern SDL_Window* g_window;
//renderer
extern SDL_Renderer* g_renderer;
and instantiate them only once in an appropriate .cpp file, for example, init.cpp

c++ Transitioning from single source file to multiple files. error: 'blank' does not name a type

I am currently attempting to write a very simple 2d game engine using SDL in order to help myself get more comfortable with coding. Unfortunately, I made the mistake of starting with a single source file, and now that my code is getting bigger, I am trying to split the existing code (which works fine before the split) into multiple header and source files. The first major issue I've encountered is that when I try to define SCREEN_HEIGHT, SCREEN_WIDTH, Window, and Renderer in init.cpp after declaring them in init.h, I get error: 'blank' does not name a type (blank being Window, Renderer, etc.). I am also trying to make them global, which may be part of the issue. Included below is the relevant code (I took out all the stuff I think is irrelevant in this case). I suspect I'm just missing something really simple here, but I've been unable to find an existing answer online like I have with my previous problems.
main.cpp
#include "globals.h"
#include "texture.h"
....
globals.h
#ifndef GLOBALS
#define GLOBALS
#include <SDL.h>
#include <SDL_image.h>
#include <SDL_ttf.h>
#include <stdio.h>
#include <string>
#endif //GLOBALS
texture.h
#ifndef TEXTURE
#define TEXTURE
#include "globals.h"
#include "init.h"
....
#endif // TEXTURE
texture.cpp
#include "texture.h"
....
init.h
#ifndef INIT
#define INIT
#include "globals.h"
//screen dimensions
int SCREEN_WIDTH;
int SCREEN_HEIGHT;
//initiates SDL and creates a window and renderer
bool init();
//the created window
SDL_Window* Window;
//the renderer that will be used
SDL_Renderer* Renderer;
#endif // INIT
init.cpp
#include "init.h"
SCREEN_WIDTH = 640;
SCREEN_HEIGHT = 480;
Window = NULL;
Renderer = NULL;
bool init()
{
bool success = true;
if( SDL_Init( SDL_INIT_VIDEO | SDL_INIT_AUDIO) < 0)
{
printf( "SDL was unable to initialize! SDL Error: %s\n", SDL_GetError());
success = false;
}
else
{
if( !SDL_SetHint( SDL_HINT_RENDER_SCALE_QUALITY, "1"))
{
printf( "Linear texture filtering not enabled!");
}
else
{
Window = SDL_CreateWindow( "Window", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN);
if( Window == NULL)
{
printf( "Window could not be created! SDL Error: %s\n", SDL_GetError());
success = false;
}
else
{
Renderer = SDL_CreateRenderer( Window, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
if( Renderer == NULL)
{
printf( "Renderer could not be created! SDL Error: %s\n", SDL_GetError());
success = false;
}
else
{
SDL_SetRenderDrawColor( Renderer, 0xFF, 0xFF, 0xFF, 0xFF);
int PNGImage = IMG_INIT_PNG;
if( !(IMG_Init( PNGImage) & PNGImage))
{
printf( "Image loading not enabled! SDL_Image Error: %s\n", IMG_GetError());
success = false;
}
if( TTF_Init() == -1)
{
printf( "True type fonts not enabled! SDL_TTF Error: %s\n", TTF_GetError());
success = false;
}
}
}
}
}
return success;
}
I get the error in init.cpp at the top of the code before bool init()
Renderer = NULL; is an assignment statement, which you cannot have in global scope. You can only have declarations in global scope. Since you have already declared Renderer in your header file, you don't need to redeclare it either. Just set it to null within one of your initialization functions (e.g. your current init() function).

Visual Studio 2013 and OpenGL

I have recently purchased a new laptop and got a new version of VS from my school. And I'm having a little trouble setting up my libraries.
I created a basic SDL_Window, as well as a SDL_GLContext.
I'm able to include my libraries and run the program, but I can't call functions like glClearColor, or glGetString(GL_VERSION). I get a rather strage warning and an error that I have never seen before, I'm guessing it's related to the 2013 version?
I have tried ignoring all specific default libraries, as well as trying to change the programtype (Multithreaded DLL, those 4).
And I have made sure all the dll-files are in place in my system folder.
What makes me wonder is the fact that glewInit() works, but not glClearColor() etc...
Output:
1>MSVCRTD.lib(cinitexe.obj) : warning LNK4098: defaultlib 'msvcrt.lib'
conflicts with use of other libs; use /NODEFAULTLIB:library
1>Window.obj : error LNK2019: unresolved external symbol
__imp__glClearColor#16 referenced in function "public: void __thiscall Window::initOpenGL(void)" (?initOpenGL#Window##QAEXXZ)
1>C:\Users\Aleksander\documents\visual studio 2013\Projects\OpenGL
Test\Debug\OpenGL Test.exe : fatal error LNK1120: 1 unresolved
externals
Header:
#pragma once
#include <SDL.h> //Tested OK
#include <SDL_image.h> //Tested OK
#include <SDL_mixer.h> //Tested OK
#include <SDL_net.h> //Tested OK
#include <OpenGL\glew.h> //?
#include <freeglut\freeglut.h> //?
#include <gl\GL.h> //?
#include <glm\glm.hpp> //Tested OK
#include <iostream> //Standard OK
#include <Box2D\Box2D.h> //Tested OK
using namespace std;
class Window {
SDL_Window *window;
SDL_GLContext context;
bool quit;
public:
Window();
~Window();
void initSDL();
void initOpenGL();
void run();
};
Source:
#include "Window.h"
Window::Window() {
printf("Starting window...\n");
//Initialize window
quit = false;
initSDL();
initOpenGL();
//Run window
run();
}
Window::~Window() {
printf("Closing window...\n");
//Clear window memory
SDL_GL_DeleteContext(context);
SDL_DestroyWindow(window);
//Close window library
SDL_Quit();
}
void Window::initSDL() {
if (SDL_Init(SDL_INIT_EVERYTHING) == -1) {
quit = true;
printf("Unable to initialize SDL!\n");
}
else {
//Create window
const char *title = "OpenGL Test";
int pos = SDL_WINDOWPOS_CENTERED;
int w = 800;
int h = 600;
Uint32 flags = SDL_WINDOW_OPENGL | SDL_WINDOW_SHOWN;
window = SDL_CreateWindow(title, pos, pos, w, h, flags);
//Create OpenGL context
SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 8);
SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 8);
SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 8);
SDL_GL_SetAttribute(SDL_GL_ALPHA_SIZE, 8);
SDL_GL_SetAttribute(SDL_GL_BUFFER_SIZE, 32);
SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
context = SDL_GL_CreateContext(window);
}
}
void Window::initOpenGL() {
GLenum init = glewInit();
if (init != GLEW_OK) {
quit = true;
printf("Unable to initialize OpenGL!\n");
printf("Error: %s!\n", glewGetErrorString(init));
}
else {
//printf("Vendor: %s\n", glGetString(GL_VENDOR));
//printf("Version: %s\n", glGetString(GL_VERSION));
//printf("Renderer: %s\n", glGetString(GL_RENDERER));
}
}
void Window::run() {
printf("Window started running!\n");
SDL_Event event;
while (!quit) {
SDL_GL_SwapWindow(window);
while (SDL_PollEvent(&event)) {
if (event.type == SDL_QUIT) {
quit = true;
}
}
}
}

Link Errors in Simple SDL Engine

I am trying to build a simple game engine with C++ and SDL. I am going through and trying to take the things that I know work in one big .cpp file, and organizing them into more logical patterns. For example, having a Game class, that contains a Screen or Engine class, a Logic class, an Entities class, etc. Obviously doesn't need to be super advanced as I'm just trying to get a handle for the first time on setting up things logically. Unfortunately I get the linker errors:
1>game.obj : error LNK2019: unresolved external symbol "public: __thiscall Screen::~Screen(void)" (??1Screen##QAE#XZ) referenced in function "public: __thiscall Game::Game(void)" (??0Game##QAE#XZ)
1>main.obj : error LNK2019: unresolved external symbol "public: __thiscall Game::~Game(void)" (??1Game##QAE#XZ) referenced in function _SDL_main
And the SDL is definitely set up properly, because the code executes just fine when everything is in one big file.
So far I have a Game class, and a Screen class.
game.h
#ifndef GAME_H
#define GAME_H
// Includes
#include "screen.h"
// SDL specific includes
#include "SDL.h"
class Game
{
private:
Screen screen;
public:
Game();
~Game();
};
#endif
game.cpp
#include "game.h"
Game::Game()
{
Screen screen;
}
screen.h
#ifndef SCREEN_H
#define SCREEN_H
#include "SDL.h"
class Screen
{
private:
// Screen dimension variables
int SCREEN_WIDTH, SCREEN_HEIGHT;
// --SDL object variables--
// The window we'll be rendering to
SDL_Window * window;
// The surface contained by the window
SDL_Surface * screenSurface;
public:
Screen();
~Screen();
// Functions for calling SDL objects
SDL_Window *getSDLWindow( void );
SDL_Surface *getSDLSurface( void );
};
#endif
screen.cpp
#include "screen.h"
#include <stdio.h>
Screen::Screen()
{
// Initialize window and SDL surface to null
window = NULL;
screenSurface = NULL;
// Initialize screen dimensions
SCREEN_WIDTH = 800;
SCREEN_HEIGHT = 600;
// Initialize SDL
if( SDL_Init( SDL_INIT_VIDEO ) < 0 )
{
printf("SDL could not initialize! SDL Error: %s\n", SDL_GetError() );
}
else
{
// Create a window
window = SDL_CreateWindow( "The First Mover", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN );
if (window == NULL)
{
printf ("Window could not be created! SDL Error: %s\n", SDL_GetError() );
}
else
{
// Get window surface
screenSurface = SDL_GetWindowSurface( window );
// Fill the surface white
SDL_FillRect( screenSurface, NULL, SDL_MapRGB( screenSurface->format, 0xFF, 0xFF, 0xFF) );
}
}
}
Any assistance appreciated! Also, if anyone has any ideas on how to better organize what I'm trying to do, feel free to comment!
Since you have declared the destructors for Screen and Game you need to provide definitions for them too. Add this in the Game.cpp and Screen.cpp files:
Game::~Game(){
// what ever you need in the destructor
}
Screen::~Screen(){
// what ever you need in the destructor
}
If you hadn't declared the destructors the compiler would have generated an implicitly for you.

GLEW Linking Error. error LNK2019

I'm using Visual Studio 2013 and am new to C++. I installed GLEW and freeglut. I try to build my file "main.c", and I receive this:
1>------ Build started: Project: testGlut1, Configuration: Debug Win32 ------
1> main.c
1>main.obj : error LNK2019: unresolved external symbol __imp__glewInit#0 referenced function _Initialize
1>main.obj : error LNK2019: unresolved external symbol __imp__glewGetErrorString#4 referenced in function _Initialize
1>C:\Users\User\documents\visual studio 2013\Projects\testGlut1\Debug\testGlut1.exe : fatal error LNK1120: 2 unresolved externals
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
I read through a similar issue, however the solution didn't work for me, in fact, nothing changed. Also, I only have 2 unresolved externals whereas the aforementioned issue showed 16 unresolved externals. Why? Why are those two errors isolated as such?
The source code for my program:
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <GL/glew.h>
#include <GL/freeglut.h>
#define WINDOW_TITLE_PREFIX "Chapter 1"
int CurrentWidth = 800,
CurrentHeight = 600,
WindowHandle = 0;
unsigned FrameCount = 0;
void Initialize(int, char*[]);
void InitWindow(int, char*[]);
void ResizeFunction(int, int);
void RenderFunction(void);
void TimerFunction(int);
void IdleFunction(void);
int main(int argc, char* argv[])
{
Initialize(argc, argv);
glutMainLoop();
exit(EXIT_SUCCESS);
}
void Initialize(int argc, char* argv[])
{
GLenum GlewInitResult;
InitWindow(argc, argv);
GlewInitResult = glewInit();
if (GLEW_OK != GlewInitResult) {
fprintf(
stderr,
"ERROR: %s\n",
glewGetErrorString(GlewInitResult)
);
exit(EXIT_FAILURE);
}
fprintf(
stdout,
"INFO: OpenGL Version: %s\n",
glGetString(GL_VERSION)
);
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
}
void InitWindow(int argc, char* argv[])
{
glutInit(&argc, argv);
glutInitContextVersion(4, 0);
glutInitContextFlags(GLUT_FORWARD_COMPATIBLE);
glutInitContextProfile(GLUT_CORE_PROFILE);
glutSetOption(
GLUT_ACTION_ON_WINDOW_CLOSE,
GLUT_ACTION_GLUTMAINLOOP_RETURNS
);
glutInitWindowSize(CurrentWidth, CurrentHeight);
glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA);
WindowHandle = glutCreateWindow(WINDOW_TITLE_PREFIX);
if (WindowHandle < 1) {
fprintf(
stderr,
"ERROR: Could not create a new rendering window.\n"
);
exit(EXIT_FAILURE);
}
glutReshapeFunc(ResizeFunction);
glutDisplayFunc(RenderFunction);
glutIdleFunc(IdleFunction);
glutTimerFunc(0, TimerFunction, 0);
}
void ResizeFunction(int Width, int Height)
{
CurrentWidth = Width;
CurrentHeight = Height;
glViewport(0, 0, CurrentWidth, CurrentHeight);
}
void RenderFunction(void)
{
++FrameCount;
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glutSwapBuffers();
glutPostRedisplay();
}
void IdleFunction(void)
{
glutPostRedisplay();
}
void TimerFunction(int Value)
{
if (0 != Value) {
char* TempString = (char*)
malloc(512 + strlen(WINDOW_TITLE_PREFIX));
sprintf(
TempString,
"%s: %d Frames Per Second # %d x %d",
WINDOW_TITLE_PREFIX,
FrameCount * 4,
CurrentWidth,
CurrentHeight
);
glutSetWindowTitle(TempString);
free(TempString);
}
FrameCount = 0;
glutTimerFunc(250, TimerFunction, 1);
}
Here are my additional dependencies:
Here is the Debug folder in MyDocuments:
There were simply incompatibilities between the OS versions of the linked files. I thought that the process was using SysWOW64 to search for the DLL files of GLUT and GLEW since my OS is Windows 64-bit. Anyway, I used the tool, Dependecy Walker 2.2, to track an previously created .exe file and learned that my OS was searching C:\Windows\System32 for the DLL files. That changed everything, annoyingly. Out of frustration in having searched for answers for over a day and a half, I went into C:\Program Files (x86)\Windows Kits\8.1\Lib\winv6.3\um\x86 and swapped the glew32.lib (64-bit) for glew32.lib (32-bit), and voila, I can run my program without any errors. Since I am not an expert, or even practiced in C programming, I can only say that this is a temporary solution for my problem. I'm not sure why the program searches through C:\Windows\System32 rather than C:\Windows\SysWOW64. But, I hope that this solution helps those whom have exprienced a mysterious linker error messages when trying to use GLEW, GLUT, and Microsoft Visual Studio.