Error LNK2019, SDL_IMG, SLD_LoadTexture. Unsolvable problem.

Started by
20 comments, last by DejaimeNeto 10 years, 1 month ago

Im using SDL2 to do all my image rendering, screen creation, basically what it's used for. I'm near the end of finishing up, and I wanted to test my image loading function, The test ended in a failure. I have been trying to solve this problem for little over two hours now, and I am making no progress. this is my error.


Error	1 error LNK2019: unresolved external symbol _IMG_LoadTexture referenced in function "public: __thiscall CSprite::CSprite(struct SDL_Renderer *,class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >,int,int,int,int)" (??0CSprite@@QAE@PAUSDL_Renderer@@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@HHHH@Z)	z:\my documents\visual studio 2013\Projects\SDL tuts 1\SDL tuts 1\Sprite.obj	SDL tuts 1

here is the class, and the header file.


#include "Sprite.h"
#include "includes.h"



CSprite::CSprite(SDL_Renderer *passedrenderer, std::string Filepath, int x, int y, int w, int h)
{

	renderer = passedrenderer;
	image = NULL;
	image = IMG_LoadTexture(renderer, Filepath.c_str());

	rect.x = x;
	rect.y = y;
	rect.w = w;
	rect.h = h;
}


CSprite::~CSprite()
{
	SDL_DestroyTexture(image);
}

void CSprite::Draw()
{
	SDL_RenderCopy(renderer, image, NULL, &rect);
}

#pragma once
#include "includes.h"
class CSprite
{
public:
	CSprite(SDL_Renderer *passedrenderer,std::string Filepath, int x, int y, int w, int h);
	~CSprite();

	void Draw();
private:
	SDL_Texture *image;
	SDL_Rect rect;
	SDL_Renderer *renderer;
	

};

If you need anything else, Im more than willing to give the needed resources.

Advertisement

Normally, this means that the compiler can find the header files for the SDL_image library, by your linker is not trying to link with SDL_image.lib, and therefore cannot find the function.

Make sure you tell your linker to use SDL_image.lib, just as you did with the regular SDL library.

Normally, this means that the compiler can find the header files for the SDL_image library, by your linker is not trying to link with SDL_image.lib, and therefore cannot find the function.

Make sure you tell your linker to use SDL_image.lib, just as you did with the regular SDL library.

I have double checked everything I could, I unlinked the .lib file, and as I expected the Image functions become unidentified, so I quickly re-linked it. After further testing, I have narrowed it down to this line.

EDIT: im aware the error directly points to this line, however I was unsure if something other than this line could be causing to throw the error.


image = IMG_LoadTexture(renderer, Filepath.c_str());

more noteably, "Filepath.c_str());"

as Filepath is created as a string, and the LoadTexture function requires a const char*, I used the c_str() prefix to convert it from a string. undoing this gives me a conversion error, and converting it gives me the error listed above. So now im stuck between what seems to be a rock and a hard place.

You say that unlinking the lib-file causes other unresolved externals, right?

Now since IMG_LoadTexture is specifically added in SDL_image 2.0.0, is it possible that you use the header file of SDL2, but link to an older version of the library (.lib-file)?

You say that unlinking the lib-file causes other unresolved externals, right?

Now since IMG_LoadTexture is specifically added in SDL_image 2.0.0, is it possible that you use the header file of SDL2, but link to an older version of the library (.lib-file)?

Right before I checked this post, I found my SDL2_image.dll to be corrupted, so after re-downloading it, and re-placing the old file, everything seems fine on my code end. however now im getting

0xC0000005: Access violation reading location 0x00000000

in line 10 of this


#include "Game_Loop.h"



CGame_Loop::CGame_Loop()
{
	csdl_setup = new CSDL_Setup(&quit);
	player = new CSprite(csdl_setup->GetRenderer(), "Z:\My Documents\Visual Studio 2013\Projects\SDL tuts 1\face.bmp", 0,0,200,200);
	quit = false;
	while ((!quit) && (csdl_setup->GetMainEvent()->type != SDL_QUIT))
	{
		csdl_setup->Begin();
		player->Draw();

			csdl_setup->End();
	}
}


CGame_Loop::~CGame_Loop()
{
}

as far as im aware this error means something is wrong with a pointer somewhere, pointing to bad memory, but I have no clue where.

It is highly likely that you are not calling necessary init function - I dont know what your main() looks like but it should be calling SDL_Init somewhere before you call any SDL functions - see the SDL doc for SDLInit

If the SDL_Init call is in your CSDL_Setup you could step through your game loop code with your debugging tool and see exactly where the code is failing - Step in to your sld_setup->Begin() function and see the values of the object's variables

It is highly likely that you are not calling necessary init function - I dont know what your main() looks like but it should be calling SDL_Init somewhere before you call any SDL functions - see the SDL doc for SDLInit

If the SDL_Init call is in your CSDL_Setup you could step through your game loop code with your debugging tool and see exactly where the code is failing - Step in to your sld_setup->Begin() function and see the values of the object's variables

my main is fairly simple, and my SDL_init call is made first thing in the CSDL_Setup class, and my main looks simply like this:


#include "includes.h"
#include "SDL_Setup.h"
#include "Game_Loop.h"





int main(int argc, char* args[])
{
	CGame_Loop();
	
	return 0;
}

Are you sure your libraries (especially SDL_Image) are in their latest versions?

@edit
Access violations, accessing 0x000000... do you, by chance, have a global pointer that isn't allocated anywhere or something like that? Running your debugger would probably give you the line where this break happens.

Are you sure your libraries (especially SDL_Image) are in their latest versions?

@edit
Access violations, accessing 0x000000... do you, by chance, have a global pointer that isn't allocated anywhere or something like that? Running your debugger would probably give you the line where this break happens.

I have nothing that is un-allocated, however as shown here, for some reason my renderer and main event are appearing null, even though they aren't and shouldn't be.

heres the line that breaks:


while (csdl_setup->GetMainEvent()->type != SDL_QUIT)

here is a picture showing them both appear as NULL and the SDL code:

EDIT: I couldn't get the picture to work so heres this:
csdl_setup = 0x01394de8 {window=0x01013050 {...} renderer=0x00000000 <NULL> mainEvent=0x00000000 <NULL> }

SDL code:


CSDL_Setup::CSDL_Setup(bool *quit)
{
	*quit = false; 
	if (*quit = true)
	{
		// Start SDL
		SDL_Init(SDL_INIT_EVERYTHING);
		window = SDL_CreateWindow("My First RPG!", 100, 100, 600, 480, SDL_WINDOW_SHOWN);
		if (window == NULL)
		{
			std::cout << "No window!" << std::endl;
			*quit = true;
		}
		renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);
		if (renderer = NULL)
		{
			std::cout << "No Renderer" << std::endl;
			*quit = true;
		}
		if (*quit = true)
		{
			//Quit SDL
			SDL_Quit();
		}

	}
}

if (renderer = NULL) {...

You are attributing NULL to the renderer here, I am sure you meant to use == .
I have done this several times.

Same with the quit test...

Didn't your compiler spit a warning about this?

This topic is closed to new replies.

Advertisement