C++ MEX-file Errors

I have some errors related to mex programming in Matlab. In particular, I am trying to compile and link C++ files using the mex command in Matlab i.e. “mex myfunc1.cpp mysubfunc2.cpp etc”. I get the following errors, many of which are linked to OpenGL library functions:

g_opengl.obj : error LNK2001: unresolved external symbol
__imp__wglGetProcAddress@4
g_opengl.obj : error LNK2001: unresolved external symbol
__imp__wglGetCurrentDC@0
g_opengl.obj : error LNK2001: unresolved external symbol
__imp__glGetString@4
g_opengl.obj : error LNK2001: unresolved external symbol
__imp__glPixelStorei@8
g_opengl.obj : error LNK2001: unresolved external symbol
_gluPerspective@32
g_opengl.obj : error LNK2001: unresolved external symbol
__imp__glMatrixMode@4
g_opengl.obj : error LNK2001: unresolved external symbol
__imp__glBlendFunc@8
gl_texture.obj : error LNK2001: unresolved external symbol
__imp__glEnable@4
joystick.obj : error LNK2001: unresolved external symbol
__imp__joyGetNumDevs@0
nafxcw.lib(appcore.obj) : error LNK2001: unresolved external symbol
___argv
nafxcw.lib(appcore.obj) : error LNK2001: unresolved external symbol
___argc
nafxcw.lib(apphelp.obj) : error LNK2001: unresolved external symbol
__mbctype
nafxcw.lib(filelist.obj) : error LNK2001: unresolved external symbol
__mbctype
nafxcw.lib(timecore.obj) : error LNK2001: unresolved external symbol
__mbctype
g_main.dll : fatal error LNK1120: 65 unresolved externals

C:\MATLAB701\BIN\WIN32\MEX.PL: Error: Link of ‘g_main.dll’ failed.

…and many other similar errors that seem to show that Matlab has problems linking with OpenGL library functions even though I have included the necessary header files in the coding i.e. (#include <GL/gl.h>, #include <GL/glu.h> and #include <GL/glaux.h>).

The program works well without any errors in Microsoft Visual Studio 6.0. I have also configured the compiler to the Microsoft Visual C/C++ 6.0 compiler using the mex -setup command.

What is causing these errors? It seems to me that the problem is with the linker. If the problem is with the linker, how does one configure the linker to generate the required dll?

Thank you.

You need to link the OpenGL libraries. Think you can just add opengl32.lib to the list of files being compiled.

Thanks for your help. In including opengl32.lib to the list of files being compiled, many of the errors have disappeared. The following errors still remain though:

g_opengl.obj : error LNK2001: unresolved external symbol _gluPerspective@32
gl_texture.obj : error LNK2001: unresolved external symbol _gluBuild2DMipmaps@28
joystick.obj : error LNK2001: unresolved external symbol __imp__joyGetNumDevs@0
joystick.obj : error LNK2001: unresolved external symbol __imp__joyGetPos@8
nafxcw.lib(appcore.obj) : error LNK2001: unresolved external symbol ___argv
nafxcw.lib(appcore.obj) : error LNK2001: unresolved external symbol ___argc
nafxcw.lib(apphelp.obj) : error LNK2001: unresolved external symbol __mbctype
nafxcw.lib(filelist.obj) : error LNK2001: unresolved external symbol __mbctype
nafxcw.lib(timecore.obj) : error LNK2001: unresolved external symbol __mbctype

Any ideas as to which library(s) are required to eliminate these remaining errors?

Thanks.

can you really use opengl with matlab? what can you do with it? i thought that was just for math, like maple and mathematica (anyone use those anymore?).

cheers

For the glu* functions you need glu32.lib.

I think the joy* functions are defined in winmm.lib, but I’m not sure.

The others look like they should be defined in the standard C library, I don’t know how to get rid of these errors…

Originally posted by <quat>:
[b] can you really use opengl with matlab? what can you do with it? i thought that was just for math, like maple and mathematica (anyone use those anymore?).

cheers [/b]
Of course you can. MATLAB is a full blown programming language, with a very nice and friendly C interface which you can use to extend the language as far as you like in any direction.

After including opengl32.lib, glu32.lib and winmm.lib, the errors reduced to:

nafxcw.lib(appcore.obj) : error LNK2001: unresolved external symbol ___argv
nafxcw.lib(appcore.obj) : error LNK2001: unresolved external symbol ___argc
nafxcw.lib(apphelp.obj) : error LNK2001: unresolved external symbol __mbctype
nafxcw.lib(filelist.obj) : error LNK2001: unresolved external symbol __mbctype
nafxcw.lib(timecore.obj) : error LNK2001: unresolved external symbol __mbctype
g_main.dll : fatal error LNK1120: 3 unresolved externals

From researching on this, it seems that _AFXDLL is not #defined, so the static MFC library is selected. This is linked to the preprocessor settings of the program.

How do you solve this problem, and configure Matlab to use MFC in a shared DLL. (The program requires the usage of MFC in a shared DLL).

Thanks.