mex: linker does not seem to be able to access external lib file; -l & -L options not helping

For a project for college I need to write an S-Function in Matlab to acquire data from a data acquisition device (NI USB-6008).
I've been reading the on-line help and I've successfully compiled a simple S-Function.
Then I added to my basic S-Function (NI_USB_6008_AnalogInput.c):
#include "NIDAQmx.h"
and then added this declaration to the function 'mdlOutputs':
TaskHandle taskHandle = 0;
(The 'TaskHandle' type is declared in the header file above.)
That compiles.
However, when I try to call a function from the NIDAQmx library:
DAQmxCreateTask("", taskHandle);
I get the following error:
>> mex NI_USB_6008_AnalogInput.c
Writing library for NI_USB_6008_AnalogInput.mexw32
c:\docume~1\l10\config~1\temp\ni_usb_6008_analoginput.obj .text:
undefined reference to '_DAQmxCreateTask@8'
C:\ARCHIVOS DE PROGRAMA\MATLAB\R2006A\BIN\MEX.PL: Error: Link of
'NI_USB_6008_AnalogInput.mexw32' failed.
??? Error using ==> mex
Unable to complete successfully
(I tried two compilers: the default one, LCC, and MS Visual C++ 2005, the error is similar with both of them.)
The problem, I understand, is that the compiler generates an object file with the correct reference to the function I'm trying to call, but then the linker is unable to access that function from the NIDAQmx.lib library. (Both the header and the library files are in the same directory as the source file.)
I tried using the -l and -L options to MEX and adding the .lib file at the end of the mex command, but I still get the same error message.
(I shuold add that I could successfully compile a simple C file with calls to the NIDAQmx library using MS Visual C++ 2005, but in Matlab I get the linker error, even when Matlab is configured to use the MS compiler.)
Can anyone suggest a solution?
FWIW, I'm running Matlab R2006a (7.2) on Windows XP with SP2.
Thank you.
Ignacio

 Accepted Answer

For lack of a better solution, here's what I'm doing.
I removed the following libraries:
  • user32.lib
  • gdi32.lib
  • winspool.lib
  • comdlg32.lib
  • advapi32.lib
  • shell32.lib
  • ole32.lib
  • oleaut32.lib
  • uuid.lib
  • odbc32.lib
  • odbccp32.lib
from this file:
C:\Documents and Settings\{USER}\Application Data\MathWorks\MATLAB\R2006a\mexopts.bat
Using MS Visual C++ 2005 it compiles correctly.
The correct way to do it would be to make the compiler/linker see the libraries above. It's not clear to me why it cannot access them, but I haven't looked much into it. What I just described is a quick & dirty solution. Hope it helps someone else who runs into this problem -- at least to get things to work!

More Answers (1)

Hi,
you are right, you need the -l and -L. Your call should look something like
mex NI_USB_6008_AnalogInput.c -Lc:\someFolder -lsomLibrary.lib
Second, I would compile using verbose mode, i.e.
mex -v ...
to see in more detail what happens. Please try both (again) and tell us what happens, or what error comes up ...
Titus

6 Comments

And if you are able to compile using VC++, use this one in MATLA as well.
Do you have the header file in the same folder? Otherwise you will need the -I flag to specify the folder ...
Thank you for your comments, Titus.
To keep things simple and avoid possible problems with spaces in pathnames, I copied the following files to the C:\sandbox folder:
NI_USB_6008_AnalogInput.c
NIDAQmx.h
NIDAQmx.lib
I added that folder to the path using pathtool and I set the current directory to it in Matlab.
Then I ran this command:
mex -LC:\sandbox -lNIDAQmx -v NI_USB_6008_AnalogInput.c
(Interestingly enough, if I type -lNIDAQmx.lib I get this error message: "Warning: NIDAQmx.lib specified with -l option not found on -L path" )
Here's the output I get with LCC (note the message "Could not locate C:\NIDAQmx.lib" ):
This is mex, Copyright 1984-2005 The MathWorks, Inc.
-> Default options filename found in C:\Documents and Settings\L10\Application Data\MathWorks\MATLAB\R2006a
----------------------------------------------------------------
-> Options file = C:\Documents and Settings\L10\Application Data\MathWorks\MATLAB\R2006a\mexopts.bat
MATLAB = C:\Archivos de programa\MATLAB\R2006a
-> COMPILER = lcc
-> Compiler flags:
COMPFLAGS = -c -Zp8 -I"C:\Archivos de programa\MATLAB\R2006a\sys\lcc\include" -DMATLAB_MEX_FILE -noregistrylookup
OPTIMFLAGS = -DNDEBUG
DEBUGFLAGS = -g4
arguments =
Name switch = -Fo
-> Pre-linking commands =
-> LINKER = lcclnk
-> Link directives:
LINKFLAGS = -tmpdir "." -dll "C:\Archivos de programa\MATLAB\R2006a\extern\lib\win32\lcc\mexFunction.def" -L"C:\Archivos de programa\MATLAB\R2006a\sys\lcc\lib" -libpath "C:\Archivos de programa\MATLAB\R2006a\extern\lib\win32\lcc" C:\DOCUME~1\L10\CONFIG~1\Temp_lib87992.obj
LINKDEBUGFLAGS =
LINKFLAGSPOST = libmx.lib libmex.lib libmat.lib
Name directive = -o "NI_USB_6008_AnalogInput.mexw32"
File link directive =
Lib. link directive =
Rsp file indicator = @
-> Resource Compiler = lrc -I"C:\Archivos de programa\MATLAB\R2006a\sys\lcc\include" -noregistrylookup -fo"mexversion.res"
-> Resource Linker =
----------------------------------------------------------------
--> "lcc -c -Zp8 -I"C:\Archivos de programa\MATLAB\R2006a\sys\lcc\include" -DMATLAB_MEX_FILE -noregistrylookup -FoC:\DOCUME~1\L10\CONFIG~1\Temp\NI_USB_6008_AnalogInput.obj -I"C:\Archivos de programa\MATLAB\R2006a"\extern\include -I"C:\Archivos de programa\MATLAB\R2006a"\simulink\include -DNDEBUG NI_USB_6008_AnalogInput.c"
--> "lcc -c -Zp8 -I"C:\Archivos de programa\MATLAB\R2006a\sys\lcc\include" -DMATLAB_MEX_FILE -noregistrylookup "C:\Archivos de programa\MATLAB\R2006a\sys\lcc\mex\lccstub.c" -FoC:\DOCUME~1\L10\CONFIG~1\Temp_lib87992.obj"
Contents of 8799_tmp.rsp:
C:\DOCUME~1\L10\CONFIG~1\Temp\NI_USB_6008_AnalogInput.obj
--> "lcclnk -o "NI_USB_6008_AnalogInput.mexw32" -tmpdir "." -dll "C:\Archivos de programa\MATLAB\R2006a\extern\lib\win32\lcc\mexFunction.def" -L"C:\Archivos de programa\MATLAB\R2006a\sys\lcc\lib" -libpath "C:\Archivos de programa\MATLAB\R2006a\extern\lib\win32\lcc" C:\DOCUME~1\L10\CONFIG~1\Temp_lib87992.obj -s @8799_tmp.rsp C:\sandbox/NIDAQmx.lib libmx.lib libmex.lib libmat.lib"
Could not locate C:\NIDAQmx.lib
C:\ARCHIVOS DE PROGRAMA\MATLAB\R2006A\BIN\MEX.PL: Error: Link of 'NI_USB_6008_AnalogInput.mexw32' failed.
??? Error using ==> mex
Unable to complete successfully
When I run mex -setup and set the Visual C++ 2005 compiler, I get this error message:
C:\DOCUME~1\L10\CONFIG~1\Temp\NI_USB_6008_AnalogInput.obj
LINK : fatal error LNK1181: cannot open input file 'user32.lib'
I noticed that if I edit the file C:\Documents and Settings\{USER}\Application Data\MathWorks\MATLAB\R2006a\mexopts.bat and remove all the libraries it cannot open ( user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib ) it compiles correctly -- but I realize that's a quick and dirty solution. Could anyone please explain what's the right way to do it?
Here's the output when I remove those libraries from the file I mentioned above:
This is mex, Copyright 1984-2005 The MathWorks, Inc.
-> Default options filename found in C:\Documents and Settings\L10\Application Data\MathWorks\MATLAB\R2006a
----------------------------------------------------------------
-> Options file = C:\Documents and Settings\L10\Application Data\MathWorks\MATLAB\R2006a\mexopts.bat
MATLAB = C:\Archivos de programa\MATLAB\R2006a
-> COMPILER = cl
-> Compiler flags:
COMPFLAGS = /c /Zp8 /GR /W3 /EHsc- /Zc:wchar_t- /DMATLAB_MEX_FILE /nologo
OPTIMFLAGS = /MD /O2 /Oy- /DNDEBUG
DEBUGFLAGS = /MD /Zi /Fd"NI_USB_6008_AnalogInput.mexw32.pdb"
arguments =
Name switch = /Fo
-> Pre-linking commands =
-> LINKER = link
-> Link directives:
LINKFLAGS = /dll /export:mexFunction /MAP /LIBPATH:"C:\Archivos de programa\MATLAB\R2006a\extern\lib\win32\microsoft" libmx.lib libmex.lib libmat.lib /implib:C:\DOCUME~1\L10\CONFIG~1\Temp_lib8208.x /MACHINE:X86 kernel32.lib
LINKDEBUGFLAGS = /DEBUG /PDB:"NI_USB_6008_AnalogInput.mexw32.pdb"
LINKFLAGSPOST =
Name directive = /out:"NI_USB_6008_AnalogInput.mexw32"
File link directive =
Lib. link directive =
Rsp file indicator = @
-> Resource Compiler = rc /fo "mexversion.res"
-> Resource Linker =
----------------------------------------------------------------
--> "cl /c /Zp8 /GR /W3 /EHsc- /Zc:wchar_t- /DMATLAB_MEX_FILE /nologo /FoC:\DOCUME~1\L10\CONFIG~1\Temp\NI_USB_6008_AnalogInput.obj -I"C:\Archivos de programa\MATLAB\R2006a"\extern\include -I"C:\Archivos de programa\MATLAB\R2006a"\simulink\include /MD /O2 /Oy- /DNDEBUG NI_USB_6008_AnalogInput.c"
NI_USB_6008_AnalogInput.c
C:\Archivos de programa\MATLAB\R2006a\simulink\include\simulink.c(384) : warning C4996: 'sprintf' was declared deprecated
C:\Archivos de programa\Microsoft Visual Studio 8\VC\INCLUDE\stdio.h(345) : see declaration of 'sprintf'
Message: 'This function or variable may be unsafe. Consider using sprintf_s instead. To disable deprecation, use _CRT_SECURE_NO_DEPRECATE. See online help for details.'
C:\Archivos de programa\MATLAB\R2006a\simulink\include\simulink.c(1439) : warning C4996: 'sprintf' was declared deprecated
C:\Archivos de programa\Microsoft Visual Studio 8\VC\INCLUDE\stdio.h(345) : see declaration of 'sprintf'
Message: 'This function or variable may be unsafe. Consider using sprintf_s instead. To disable deprecation, use _CRT_SECURE_NO_DEPRECATE. See online help for details.'
C:\Archivos de programa\MATLAB\R2006a\simulink\include\simulink.c(1448) : warning C4996: 'sprintf' was declared deprecated
C:\Archivos de programa\Microsoft Visual Studio 8\VC\INCLUDE\stdio.h(345) : see declaration of 'sprintf'
Message: 'This function or variable may be unsafe. Consider using sprintf_s instead. To disable deprecation, use _CRT_SECURE_NO_DEPRECATE. See online help for details.'
C:\Archivos de programa\MATLAB\R2006a\simulink\include\simulink.c(1477) : warning C4996: 'sprintf' was declared deprecated
C:\Archivos de programa\Microsoft Visual Studio 8\VC\INCLUDE\stdio.h(345) : see declaration of 'sprintf'
Message: 'This function or variable may be unsafe. Consider using sprintf_s instead. To disable deprecation, use _CRT_SECURE_NO_DEPRECATE. See online help for details.'
C:\Archivos de programa\MATLAB\R2006a\simulink\include\simulink.c(2105) : warning C4996: 'sprintf' was declared deprecated
C:\Archivos de programa\Microsoft Visual Studio 8\VC\INCLUDE\stdio.h(345) : see declaration of 'sprintf'
Message: 'This function or variable may be unsafe. Consider using sprintf_s instead. To disable deprecation, use _CRT_SECURE_NO_DEPRECATE. See online help for details.'
C:\Archivos de programa\MATLAB\R2006a\simulink\include\simulink.c(2112) : warning C4996: 'sprintf' was declared deprecated
C:\Archivos de programa\Microsoft Visual Studio 8\VC\INCLUDE\stdio.h(345) : see declaration of 'sprintf'
Message: 'This function or variable may be unsafe. Consider using sprintf_s instead. To disable deprecation, use _CRT_SECURE_NO_DEPRECATE. See online help for details.'
C:\Archivos de programa\MATLAB\R2006a\simulink\include\simulink.c(2124) : warning C4996: 'sprintf' was declared deprecated
C:\Archivos de programa\Microsoft Visual Studio 8\VC\INCLUDE\stdio.h(345) : see declaration of 'sprintf'
Message: 'This function or variable may be unsafe. Consider using sprintf_s instead. To disable deprecation, use _CRT_SECURE_NO_DEPRECATE. See online help for details.'
C:\Archivos de programa\MATLAB\R2006a\simulink\include\simulink.c(2133) : warning C4996: 'sprintf' was declared deprecated
C:\Archivos de programa\Microsoft Visual Studio 8\VC\INCLUDE\stdio.h(345) : see declaration of 'sprintf'
Message: 'This function or variable may be unsafe. Consider using sprintf_s instead. To disable deprecation, use _CRT_SECURE_NO_DEPRECATE. See online help for details.'
C:\Archivos de programa\MATLAB\R2006a\simulink\include\simulink.c(2138) : warning C4996: 'sprintf' was declared deprecated
C:\Archivos de programa\Microsoft Visual Studio 8\VC\INCLUDE\stdio.h(345) : see declaration of 'sprintf'
Message: 'This function or variable may be unsafe. Consider using sprintf_s instead. To disable deprecation, use _CRT_SECURE_NO_DEPRECATE. See online help for details.'
C:\Archivos de programa\MATLAB\R2006a\simulink\include\simulink.c(2182) : warning C4996: 'sprintf' was declared deprecated
C:\Archivos de programa\Microsoft Visual Studio 8\VC\INCLUDE\stdio.h(345) : see declaration of 'sprintf'
Message: 'This function or variable may be unsafe. Consider using sprintf_s instead. To disable deprecation, use _CRT_SECURE_NO_DEPRECATE. See online help for details.'
Contents of 8208_tmp.rsp:
C:\DOCUME~1\L10\CONFIG~1\Temp\NI_USB_6008_AnalogInput.obj
--> "link /out:"NI_USB_6008_AnalogInput.mexw32" /dll /export:mexFunction /MAP /LIBPATH:"C:\Archivos de programa\MATLAB\R2006a\extern\lib\win32\microsoft" libmx.lib libmex.lib libmat.lib /implib:C:\DOCUME~1\L10\CONFIG~1\Temp_lib8208.x /MACHINE:X86 kernel32.lib @8208_tmp.rsp C:\sandbox/NIDAQmx.lib "
Microsoft (R) Incremental Linker Version 8.00.50727.42
Copyright (C) Microsoft Corporation. All rights reserved.
C:\DOCUME~1\L10\CONFIG~1\Temp\NI_USB_6008_AnalogInput.obj
Creating library C:\DOCUME~1\L10\CONFIG~1\Temp_lib8208.x and object C:\DOCUME~1\L10\CONFIG~1\Temp_lib8208.exp
--> "del "NI_USB_6008_AnalogInput.map""
--> "del C:\DOCUME~1\L10\CONFIG~1\Temp_lib8208.x"
--> "mt -outputresource:"NI_USB_6008_AnalogInput.mexw32";2 -manifest "NI_USB_6008_AnalogInput.mexw32.manifest""
Microsoft (R) Manifest Tool version 5.2.3790.2014
Copyright (c) Microsoft Corporation 2005.
All rights reserved.
--> "del "NI_USB_6008_AnalogInput.mexw32.manifest""
So briefly, I managed to compile the file with mex, but what I did just doesn't seem right. Could anyone please point me in the right direction?
Thank you.
Ignacio
BTW, I followed the steps given here (i.e., I installed the Platform SDK for Microsoft® Windows Server® 2003 R2 and set the MSSdk environment variable as instructed), but I still get this error message:
C:\DOCUME~1\L10\CONFIG~1\Temp\NI_USB_6008_AnalogInput.obj
LINK : fatal error LNK1181: cannot open input file 'user32.lib'
(NB: That solution was reported for release R2007a and I'm using R2006a.)
What am I missing?
Hi Ignacio,
I'm sorry but I have to admit I don't know any further. If you followed the advice about the Platform SDK, I don't know what to do next except for trying to contact MathWorks support...
Titus
Thank you, Titus. I really appreciate your input.
I will follow your advice and contact MathWorks support. I will come back to this thread when I have a solution.
Ignacio

Sign in to comment.

Categories

Find more on MATLAB Compiler in Help Center and File Exchange

Asked:

on 26 Jul 2012

Community Treasure Hunt

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

Start Hunting!