OpenGL-SDL Unresolved External Symbol

  • Thread starter Thread starter TheDemx27
  • Start date Start date
  • Tags Tags
    Symbol
AI Thread Summary
The discussion centers on troubleshooting linking errors encountered while setting up OpenGL with SDL and GLEW. The primary issue is an unresolved external symbol error related to the SDL_CreateWindow function, indicating that the necessary SDL library files may not be correctly linked. Participants emphasize the importance of linking the appropriate .lib files rather than just including the DLL file. There are suggestions to ensure that the SDL include directory is correctly set up in the project settings and to verify that the correct library files are being used. A specific problem arises with SDL redefining the main function, which can be resolved by defining SDL_MAIN_HANDLED before including SDL.h. However, even after making these adjustments, the user encounters further linker errors, particularly with the SDL_SetMainReady function. The conversation highlights the need for careful attention to linking libraries and understanding the implications of using SDL with Visual Studio. Ultimately, the user decides to switch to GLFW for their project, indicating a resolution to their initial issues.
TheDemx27
Gold Member
Messages
169
Reaction score
13
Hello.

I am trying to get OpenGL working on my machine using SDL and glew. I've created a class 'Display' that pretty much just sets up the display and provides a place to draw things. I have 11 errors. In the errors it references both constructor and destructor in the Display class. Here is an example of an error:

Error 1 error LNK2019: unresolved external symbol _SDL_CreateWindow referenced in function "public: __thiscall Display::Display(int,int,class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const &)" (??0Display@@QAE@HHABV? $basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@Z) C:\Users\Demx\documents\visual studio 2013\Projects\OpenGLProject\OpenGLProject\Display.obj OpenGLProject

I cannot make out head nor tail of anything coming after the reference to Display::Display(int, int etc.) If anyone can interpret some of that I would be grateful.

Here is the complete project:
https://github.com/TheDemx27/OpenGL-Bootstrap/tree/master/OpenGLProject

Thanks.
 
Last edited by a moderator:
Technology news on Phys.org
Haven't ever programmed in openGL. BUT that error looks like its a linker error.
I'd check through the documentation to see if you need to link in any external dll's or lib files.
 
Make sure you include the compiled library file within the linker properties as hinted above.

Also make sure that you have the library that matches the header used to define the function if you have multiple builds.
 
Yes I've linked the files and included SDL2.dll in the debug folder for dynamic linking.
Confining myself to just SDL, I've followed these http://www.libsdl.org/tmp/SDL/VisualC.html exactly, the files are in place and I've linked them as instructed. Simply including the file has created trouble building.
Code:
#include <SDL2/SDL.h> 

int main(int argc, char *argv[])
{

	return 0;
}
Is returning a linking error. Hopefully I'm just missing something really obvious, but I've been looking at this for 2 days and I still can't figure out what I'm doing wrong.
 
You don't include the DLL file - that is the compiled library code file.

You have to link the lib file (ie, something like SDL2.lib) which is for the linker itself.

If you are using a DLL, then usually what happens is that you call LoadLibrary to load the DLL
and then you get the procedure address (I think it's GetProcAddress or something similar) and then
you save the pointer and call the function through casting that pointer in a specific way.

A library file may wrap this functionality for you and if it does then it will be present in your .lib file
that is given to you by the developer.

See if you can search for all .lib files within the SDK and see what ones are available.
 
TheDemx27 said:
I've followed these http://www.libsdl.org/tmp/SDL/VisualC.html exactly

No you didn't. You included <SDL2/SDL.h>. The "SDL 101" demo says #include "SDL.h".

Most likely, SDL.h includes some more include files which can't be found the way you did it. Did you follow the instruction
"Add the SDL include directory to your list of includes..." or did you just try to fix the problem by including <SDL2/SDL.h>?

It seems strange that you are getting "a linking error" here since the program shouldn't be trying to include anything! The actual error message might be more useful than your attempt at interpreting it.
 
You don't include the DLL file - that is the compiled library code file.

I didn't use the best choice of words. I placed the SDL.dll in the project directory as per instructions.

You have to link the lib file (ie, something like SDL2.lib) which is for the linker itself.

I already have.

A library file may wrap this functionality for you and if it does then it will be present in your .lib file

This functionality is present in the lib file as you say.

See if you can search for all .lib files within the SDK and see what ones are available.

I don't quite know what you mean by search. I'm just using visual studio.
Thanks for responding.
 
Last edited:
AlephZero said:
Most likely, SDL.h includes some more include files which can't be found the way you did it. Did you follow the instruction
"Add the SDL include directory to your list of includes..." or did you just try to fix the problem by including <SDL2/SDL.h>?
True, it wasn't exactly, but it was an attempt to make things a bit more organized, being in a sub-directory doesn't change anything as long as the paths referenced are ok relative to that file. I tried it without the sub-directories just for the sake of getting this thing working and it gives the same linker messages.

SDL.h should have no problem referencing the other header files. And it doesn't have any problem, since Intellisense suggests the various .h files as I type them.
 
Last edited:
TheDemx27 said:
Error 1 error LNK2019: unresolved external symbol _SDL_CreateWindow referenced in function "public: __thiscall Display::Display(int,int,class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const &)" (??0Display@@QAE@HHABV? $basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@Z) C:\Users\Demx\documents\visual studio 2013\Projects\OpenGLProject\OpenGLProject\Display.obj OpenGLProject
.
From what I can tell you created an object class called Display (or there is one inside of OpenGL that you are instantiating). What the error is saying is the constructor for the Display object is trying to call a function _SDL_CreateWindow which is external to the object. The linker is saying that this function does not exist in the complied and linked object.

Try following http://www.opengl.org/wiki/Tutorial1:_Creating_a_Cross_Platform_OpenGL_3.2_Context_in_SDL_(C_/_SDL )
it calls that function directly you might be able to narrow down what you are missing :D
 
Last edited by a moderator:
  • #10
Well the problem with simply writing include <SDL.h> is that SDL.h includes SDL_main.h which has the definition
Code:
/*In file SDL_main.h*/
#define main SDL_main
Which conflicts with my main function. If I comment out the above line I can compile, but I can't call any functions at all.
I have no idea what to do.:cry:
 
  • #11
take a look at these see if they help

http://stackoverflow.com/questions/11976084/why-sdl-defines-main-macro
http://www.cplusplus.com/forum/general/11692/

or this from http://forums.libsdl.org/viewtopic.php?p=37460&sid=3374c819e18df779e17b4ce5a49fdd15
Quote:
If you don't want SDL to redefine your main function, just define SDL_MAIN_HANDLED before you include SDL.h, and then call SDL_SetMainReady() before you call SDL_Init().

The reason it's there is, as people mentioned, there are some platforms that don't use main() as the program entry point, or there needs to be some custom runtime setup that happens before your program runs.

The best way to figure out what this is, is to look in SDL_main.h and see what platforms this is needed on:
#if defined(__WIN32__) || defined(__IPHONEOS__) || defined(__ANDROID__)

On Windows, this is simply a convenience so you don't have to provide WinMain(). If you have that, or you are using a library that needs it, you don't need SDL to provide it.

On iOS, you need to set up an application delegate and start the event system, otherwise your code will crash in really non-intuitive ways during window creation.

On Android, the entry point is Java (unless you're completely using the NDK) and your "main" function is called on a separate thread.
 
  • Like
Likes 1 person
  • #12
Well, as soon as I #define SDL_MAIN_HANDLED, I can compile, but as soon as I call SDL_SetMainReady(), it still give me a linker error for that function.

Error 1 error LNK2019: unresolved external symbol _SDL_SetMainReady referenced in function _main C:\Users\Demx\Documents\Visual Studio 2013\Projects\OpenGLProject\OpenGLProject\main.obj OpenGLProject

As far as I can tell by looking at SDL_main.h, #defining SDL_MAIN_HANDLED is analogous to my commenting out #define main SDL_main in this situation.
 
  • #13
Of course perhaps that was because it was expecting WinMain now that I'm defining SDL_MAIN_HANDLED, so that could be the right way to go. The official SDL documentation has this to say about WinMain
If for some reason you need to use WinMain(), take a look at the SDL source code in src/main/win32/SDL_main.c to see what kind of initialization you need to do in your WinMain() function so that SDL works properly.
So I looked through the source code and found this:
Code:
/* This is where execution begins [windowed apps] */
#ifdef _WIN32_WCE
int WINAPI WinMain(HINSTANCE hInst, HINSTANCE hPrev, LPWSTR szCmdLine, int sw)
#else
It looked ok to me, so I slapped that on my main.cpp, and now I'm just getting stuck on this one error:
Code:
Error	1	error C2731: 'WinMain' : function cannot be overloaded	c:\users\demx\documents\visual studio 2013\projects\openglproject\openglproject\main.cpp	8	1	OpenGLProject
Which I might be able to solve myself. I sure hope this is the end of it. :biggrin:
 
  • #14
lol hopefully you get this all fixed up!
I know my biggest problems when I run into errors that don't have an immediately clear solution is that I don't actually stop to think about WHY the problem is occurring, and WHAT the behavior is telling me about what is going on.

This week I was working with a development framework which I hadn't touched in a few years, and long story short all the classes are sub classes of a std class. Then you overload custom functions in your sub class for the functionality.
So I do this set up my custom function and it doesn't run, I then spend a couple hours confused as #*!# as to what was going on. Ended up that I had the } closing the sub class before I put in my custom functions (so they were global functions instead of class functions) /facepalm
 
  • #15
Well that still didn't work and I'm pretty much done with this. I've got GLFW working instead and I'm going to use that. Thanks cpscdave for your help though!

Edit:
And so life is good. :approve:
 
Last edited:
Back
Top