Unresolved external symbol: SDL/OpenGL libraries linked from MEX using Visual Studio

7 views (last 30 days)
I am attempting to use a MEX routine to launch a lightweight OpenGL-based window, driven with SDL. The window is successfully launched when compiled as a standalone EXE in Visual Studio. When I call the same method from mexFunction(), however, I run into everyone's favorite linking error:
error LNK2019: unresolved external symbol SDL_GL_SetAttribute referenced...
error LNK2019: unresolved external symbol SDL_SetVideoMode referenced...
error LNK2019: unresolved external symbol SDL_Init referenced...
[...]
However, SDL.lib and SDLmain.lib are indeed on the linker's path. I chose to place them in .../Program Files/Microsoft SDKs/Windows/..., and removing or renaming them causes a 'cannot open input file' error--LNK1181--so I know they are in scope.
I have tried to address this issue by creating my own MEX options file from the default mexopts.bat, repeatedly adjusting linker options to converge as close as possible to the Visual Studio command line link options. However, I continue to receive LNK2019 errors.
I have also considered that this be the result of MATLAB trying to compile a 64-bit MEX routine against 32-bit static libraries. However, it only complains about symbols from SDL.lib, and not OpenGl32.lib. Even if this was the case, I should be seeing a LNK2001 error instead.
I'm a little bit out of my Comfort Zone of Absolute Certainty in this case, and seem to have exhausted my creative problem-solving abilities for the time being. The last option I can think of is to completely recompile the SDL static libraries, but I don't want to go through the pain of doing so if it's not going to give me anything different than the libraries I already have. Any suggestions?
[EDIT: Removed superfluous sentence. 8/4/2011 @ 11:37PM]

Accepted Answer

Kaustubha Govind
Kaustubha Govind on 4 Aug 2011
It seems to me like the issue really is because you are attempting to link a 32-bit static library against a 64-bit MEX function. Apparently, OpenGL32 is a 64-bit library on 64-bit Windows machines. From this page:
"If you are running a 64 bit version of Windows, you might be wondering if there is a OpenGL64.dll file. The answer is no, there isn't. On all versions of Windows, Windows\System32 contains all the 64 bit DLLs. It contains the OpenGL32.dll which is actually a 64 bit dll."
This is why you don't getting linking errors against OpenGL32.lib.
If you really don't want to rebuild your libraries for 64-bit targets, you could simply install 32-bit MATLAB to compile and run 32-bit MEX-functions instead. See here for installation instructions and here for caveats.
  2 Comments
Brian Kirkpatrick
Brian Kirkpatrick on 26 Aug 2011
Building a 32-bit MEX from 32-bit MATLAB works, and with minimal hassle.
I'm going to continue to look at ways to build a 64-bit MEX file instead, though. Most likely this will involve compiling 64-bit SDL and SDL-related DLLs for Windows. At the very least, it will be an interesting learning process.
Thanks for your thoughts.
Brian Kirkpatrick
Brian Kirkpatrick on 1 Sep 2011
Success!
*Short Version*
MATLAB was trying to use 32-bit static libraries to link a 64-bit MEX, despite the fact that 64-bit static libraries were available and within the link path. The problem was solved by moving the 32-bit static libraries from the default link path and placing them in a 32-bit specific location.
*Long Version*
# Recompiled the SDL libraries to SDL.lib and SDLmain.lib static library files from <http://www.libsdl.org/download-1.2.php the source>. Used the included VisualC solution, adding a x64 platform to the Configuration Manager and using that to compile both projects.
# Placed libraries on a 64-bit specific path
# Re-ran "mex -setup" in 64-bit MATLAB to set up MEX options for that platform
# Ran "mex -v..." with the -L flags pointing to the 64-bit specific path (failed at first)
# Used -v output to determine where MATLAB was adding 32-bit libraries; moved them from link path to 32-bit specific location. Re-ran "mex -v..."; success!

Sign in to comment.

More Answers (0)

Categories

Find more on Write C Functions Callable from MATLAB (MEX Files) in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!