Jump to content

  • Log In with Google      Sign In   
  • Create Account


Error LNK2019, SDL_IMG, SLD_LoadTexture. Unsolvable problem.


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
21 replies to this topic

#1 Bigfatmeany   Members   -  Reputation: 136

Like
0Likes
Like

Posted 14 March 2014 - 08:55 PM

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.



Sponsor:

#2 DaBono   Members   -  Reputation: 963

Like
1Likes
Like

Posted 15 March 2014 - 08:04 AM

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.



#3 Bigfatmeany   Members   -  Reputation: 136

Like
0Likes
Like

Posted 15 March 2014 - 10:14 AM

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.


Edited by Bigfatmeany, 15 March 2014 - 10:16 AM.


#4 DaBono   Members   -  Reputation: 963

Like
0Likes
Like

Posted 15 March 2014 - 11:21 AM

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)?



#5 Bigfatmeany   Members   -  Reputation: 136

Like
0Likes
Like

Posted 15 March 2014 - 11:48 AM

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.



#6 EarthBanana   Members   -  Reputation: 872

Like
0Likes
Like

Posted 15 March 2014 - 01:42 PM

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


Edited by EarthBanana, 15 March 2014 - 01:48 PM.


#7 Bigfatmeany   Members   -  Reputation: 136

Like
0Likes
Like

Posted 15 March 2014 - 02:26 PM

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;
}



#8 dejaime   Crossbones+   -  Reputation: 4002

Like
0Likes
Like

Posted 18 March 2014 - 06:27 AM

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.

Edited by dejaime, 18 March 2014 - 06:33 AM.


#9 Bigfatmeany   Members   -  Reputation: 136

Like
0Likes
Like

Posted 18 March 2014 - 05:59 PM

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();
		}

	}
}

Edited by Bigfatmeany, 18 March 2014 - 05:59 PM.


#10 dejaime   Crossbones+   -  Reputation: 4002

Like
0Likes
Like

Posted 18 March 2014 - 07:13 PM

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?


Edited by dejaime, 18 March 2014 - 07:15 PM.


#11 Bigfatmeany   Members   -  Reputation: 136

Like
0Likes
Like

Posted 18 March 2014 - 07:42 PM

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?

 

ah, this solved my first error, no it didnt. which was odd. It compiles with no errors or warnings. except for the break, which is now happening here.

SDL_Renderer *CSDL_Setup::GetRenderer()
{
	return renderer;
}

more specifically

return renderer;

EDIT:

interestingly enough if I have:

csdl_setup = new CSDL_Setup(&quit);

it breaks in the original place.

however:

csdl_setup == new CSDL_Setup(&quit);

"==" causes it to break in the return renderer; function.

any known reason to why == and = are different or did i just missing something from my books and studies?


Edited by Bigfatmeany, 18 March 2014 - 07:47 PM.


#12 dejaime   Crossbones+   -  Reputation: 4002

Like
0Likes
Like

Posted 18 March 2014 - 07:52 PM

It would be easier to understand your code if you placed it (in its entirety) somewhere like PasteOfCode or Gist.

 

The CSDL_Setup class declaration would be great, while the context where you call the problematic GetRenderer() function would also help. It doesn't seem like something that should break that way...

 

btw, what compiler are you using? All compilers I know show a warning when attributing something somewhere where a "truth value" should be; unless their warnings are suppressed manually.


Edited by dejaime, 18 March 2014 - 07:56 PM.


#13 Bigfatmeany   Members   -  Reputation: 136

Like
0Likes
Like

Posted 18 March 2014 - 08:00 PM

It would be easier to understand your code if you placed it (in its entirety) somewhere like PasteOfCode or Gist.

 

The CSDL_Setup class declaration would be great, while the context where you call the problematic GetRenderer() function would also help. It doesn't seem like something that should break that way...

 

btw, what compiler are you using? All compilers I know show a warning when attributing something somewhere where a "truth value" should be; unless their warnings are suppressed manually.

SDL_Setup.cpp http://paste.ofcode.org/h7TiTsMNJLQK9U57k9Xw7q

SDL_Setup.h http://paste.ofcode.org/9qem9BMGWj8TcqcFMFQEZJ

Game_Loop.cpp http://paste.ofcode.org/x4KRbXXgAvACvi9ch46vjx

Game_Loop.h http://paste.ofcode.org/DSpAvqcgDGQ66MEY6jKvEA

 

Im using visual studio 2013. If it is showing them, im seeing them no-where, and I know it used to throw warnings in 2010.



#14 dejaime   Crossbones+   -  Reputation: 4002

Like
0Likes
Like

Posted 18 March 2014 - 08:05 PM

The problem can be here:

player = new CSprite(csdl_setup->GetRenderer(), "Z:\My Documents\Visual Studio 2013\Projects\SDL tuts 1\face.bmp", 0,0,200,200);

"...\face.bmp". Try swapping all your \ with '/' or '\\' and trying again. The path is probably being misinterpreted...

player = new CSprite(csdl_setup->GetRenderer(), "Z:/My Documents/Visual Studio 2013/Projects/SDL tuts 1/face.bmp", 0,0,200,200);

Edited by dejaime, 18 March 2014 - 08:06 PM.


#15 Bigfatmeany   Members   -  Reputation: 136

Like
0Likes
Like

Posted 18 March 2014 - 08:20 PM

 

The problem can be here:

player = new CSprite(csdl_setup->GetRenderer(), "Z:\My Documents\Visual Studio 2013\Projects\SDL tuts 1\face.bmp", 0,0,200,200);

"...\face.bmp". Try swapping all your \ with '/' or '\\' and trying again. The path is probably being misinterpreted...

player = new CSprite(csdl_setup->GetRenderer(), "Z:/My Documents/Visual Studio 2013/Projects/SDL tuts 1/face.bmp", 0,0,200,200);

that did give me a warning, i just didn't get around to changing  it, but it still breaks.



#16 dejaime   Crossbones+   -  Reputation: 4002

Like
0Likes
Like

Posted 18 March 2014 - 08:32 PM

Now I'll ask you for your CSprite class; I hope you don't mind!

 

interestingly enough if I have:

csdl_setup = new CSDL_Setup(&quit);

it breaks in the original place.

however:

csdl_setup == new CSDL_Setup(&quit);

"==" causes it to break in the return renderer; function.

any known reason to why == and = are different or did i just missing something from my books and studies?

Didn't see this earlier.

== is a comparison, while = is an attribution.

This:

x = 1

means that you want x to be 1, and now it is.

x == 1

means that you want to know if it is 1.

 

So, == is comparing two things,

while = is modifying the left one's value to be the same as the right one.

 

When I said the quit test, I meant this one:

if (*quit = true)

Edited by dejaime, 18 March 2014 - 08:34 PM.


#17 Bigfatmeany   Members   -  Reputation: 136

Like
0Likes
Like

Posted 18 March 2014 - 08:34 PM

Now I'll ask you for your CSprite class; I hope you don't mind!

 

interestingly enough if I have:

csdl_setup = new CSDL_Setup(&quit);

it breaks in the original place.

however:

csdl_setup == new CSDL_Setup(&quit);

"==" causes it to break in the return renderer; function.

any known reason to why == and = are different or did i just missing something from my books and studies?

Didn't see this earlier.

== is a comparison, while = is an attribution.

This:

x = 1

means that you want x to be 1, and now it is.

x == 1

means that you want to know if it is 1.

 

So, == is comparing two things,

while = is modifying the left one's value to be the same as the right one.

see, thats what i thought. But it confuses my why visual studio 2013 wouldn't throw me a warning about that in an if check.

Sprite.cpp http://paste.ofcode.org/xDsSHHcNbL8YWhjscfkWnh

sprite.h http://paste.ofcode.org/u8Jb28PZAUTux7PfrhJG8Y


Edited by Bigfatmeany, 18 March 2014 - 08:36 PM.


#18 dejaime   Crossbones+   -  Reputation: 4002

Like
0Likes
Like

Posted 18 March 2014 - 08:46 PM

Would you try this modified version?

http://paste.ofcode.org/gaDCuxtcxt5q6HWRjTLnXb

and tell me what happens?


Edited by dejaime, 18 March 2014 - 08:46 PM.


#19 Bigfatmeany   Members   -  Reputation: 136

Like
0Likes
Like

Posted 18 March 2014 - 09:01 PM

Would you try this modified version?

http://paste.ofcode.org/gaDCuxtcxt5q6HWRjTLnXb

and tell me what happens?

Well, it just continues to fail at the same point. 

return renderer;

I'm unable to see a consol or anything.



#20 dejaime   Crossbones+   -  Reputation: 4002

Like
0Likes
Like

Posted 18 March 2014 - 09:03 PM

Are you sure the csdl_setup is a valid pointer? Where is it constructed?




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS