Error while loading library

23 views (last 30 days)
Sandeep
Sandeep on 12 Jul 2016
Answered: Toby Dewhurst on 28 Feb 2018
I am trying to load a shared library from the sample code provided in the matlab documentation.
addpath(fullfile(matlabroot,'extern','examples','shrlib'))
if not(libisloaded('shrlibsample'))
loadlibrary('shrlibsample')
end
But when it tries to load the library i get an error:
Error using loadlibrary
The MATLAB program file describing your library could not be found, possible Perl or file system error.
Any help is appreciated.
  2 Comments
Walter Roberson
Walter Roberson on 12 Jul 2016
Which MATLAB version are you using?
Have you installed a compiler and used
mex -setup
?
Sandeep
Sandeep on 12 Jul 2016
Edited: Walter Roberson on 12 Jul 2016
I am using Matlab 2015b. It was working fine a week ago. But suddenly it stopped. Not sure what installation/ update has caused this.
Also compiler is installed
>> mex -setup
MEX configured to use 'Microsoft Windows SDK 7.1 (C)' for C language compilation.
Warning: The MATLAB C and Fortran API has changed to support MATLAB
variables with more than 2^32-1 elements. In the near future
you will be required to update your code to utilize the
new API. You can find more information about this at:
http://www.mathworks.com/help/matlab/matlab_external/upgrading-mex-files-to-use-64-bit-api.html.
To choose a different C compiler, select one from the following:
Microsoft Visual C++ 2015 Professional (C) mex -setup:'C:\Program Files\MATLAB\R2015b\bin\win64\mexopts\msvc2015.xml' C
Microsoft Windows SDK 7.1 (C) mex -setup:C:\Users\Stb6kor\AppData\Roaming\MathWorks\MATLAB\R2015b\mex_C_win64.xml C
To choose a different language, select one from the following:
mex -setup C++
mex -setup FORTRAN

Sign in to comment.

Accepted Answer

Sandeep
Sandeep on 26 Jul 2016
Edited: Sandeep on 26 Jul 2016
I Finally found the solution for this problem. This was caused by a space/carriage return in the Path value of the Environment variable. It was not evident when I manually checked the path. Then i copied it to Notepad++ and found there was a space and a carriage return. Replaced the Path value with the edited path value(removing space and carriage return). Now the loadlibrary function works fine.
PS: The attached image has the markings about what i am talking about. The problem is only with WindowsPowershell\V1.0. Ignore the rest.

More Answers (3)

Toby Dewhurst
Toby Dewhurst on 28 Feb 2018
I had a very similar problem: When I tried,
addpath(fullfile(matlabroot,'extern','examples','shrlib'))
if not(libisloaded('shrlibsample'))
loadlibrary('shrlibsample')
end
I got:
Error using loadlibrary
Too many output arguments.
Following @Philip Borghesani's suggestion, I found that it was the "perl" command that was broken (returning too many outputs).
The problem turned out to be that MATLAB's built-in perl.m was shadowed by a perl.m in a toolbox I had downloaded from a third party.

Philip Borghesani
Philip Borghesani on 12 Jul 2016
Edited: Philip Borghesani on 12 Jul 2016
Something quite odd is going on, This error is caused by a timeout waiting for the prototype file to be visible to MATLAB after supposedly successful creation. I have only questions and suggestions.
  • If your current directory is not writable by you try changing to one that is.
  • Make sure your tempdir is writable and the disk is not full
  • If nether of these helps or is the problem try using the m_filename option to loadlibrary to specify the name and location of the prototype file that will be generated.
  • If all else fails contact support.
  4 Comments
Jan Niklas Caspers
Jan Niklas Caspers on 14 Jul 2016
I got the same problem, when I tried the perl command I got as a reponse an empty answer, while when I use it on a computer where the loadlibrary works, I get the expected hello. So, how do I fix perl now?
Sandeep
Sandeep on 19 Jul 2016
Even i got an Empty result as below:
>> perl('test.pl')
ans =
''

Sign in to comment.


Jan Niklas Caspers
Jan Niklas Caspers on 14 Jul 2016
Any responses from Matlab Support? Have the same error and tried all steps here.
  2 Comments
Walter Roberson
Walter Roberson on 14 Jul 2016
For a response from Mathworks Support you need to open a technical support case. This resource, MATLAB Answers, is replied to by volunteers, most of whom do not work for Mathworks.
Sandeep
Sandeep on 15 Jul 2016
Edited: Walter Roberson on 15 Jul 2016
Well they gave me set of solutions, none worked for me. You can try it and check if it works for you.
The error usually occurs when with damaged MATLAB installation path. Please execute the following commands to restore the MATLAB default path -
>> restoredefaultpath
>> rehash toolboxcache
Additionally, possible solution for this would be to modify MATLAB function "perl.m" so that it can execute perl application without an extension. Please perform the below step to modify "perl.m" file.
  1. Open perl.m file by typing "edit perl.m" in MATLAB command window.
  2. Replace line "cmdString = ['perl' cmdString];" with "cmdString = ['perl.exe' cmdString]; "
  3. Save perl.m
  4. Restart MATLAB and try executing "loadlibrary"
If the above steps do not resolve the issue another possible cause of the error might be corrupt PATH. If the PATH variable contains double quotes(" "). Remove them.
  1. Open cmd.exe
  2. Execute "set" command
  3. Check if any PATH environmental variable contains double quotes(" "). If so remove those double quotes.
After following the above steps you should be able to use "loadlibrary" without errors. Should you have any more queries regarding the issue, please drop an email and I shall be happy to reopen the case and assist you further.
Next mail>>
It is quite strange that you are still not able to use the function 'loadlibrary' despite MATLAB seemingly working fine.
I am investigating on this issue. But in the meantime, can you please re-install MATLAB and check if the issue persists.
Next mail>>
Thank you for your email. In one of your last emails you inquired if the issue might be related to compilers or not. I was investigating along this line to check if it can be a compiler related issue. I believe you are using MATLAB R2015b. If you browse to the location C:\Program Files\MATLAB\R2015b\extern\examples\shrlib, you would see that there are files like shrlibsample.c, shrlibsample.h and also a mex file shrlibsample.mexw64, besides other files as well.
So I took the files 'shrlibsample.c', 'shrlibsample.h' and 'shrhelp.h' into a separate directory and tried executing the following command.
>> loadlibrary('shrlibsample','shrlibsample.h','addheader','shrhelp.h')
The above command did not work an it returned the following error -
"Error using loadlibrary
There was an error loading the library "shrlibsample"
The specified module could not be found.
Caused by:
Error using loaddefinedlibrary
The specified module could not be found."
On this, I mex-ed the file 'shrlibsample.c' by executing the following command (Or we can just copy the file shrlibsample.mexw64 from C:\Program Files\MATLAB\R2015b\extern\examples\shrlib into the current directory)
>> mex shrlibsample.c
This created a MEX file equivalent of the C file 'shrlibsample.c'. Then on running the loadlibrary command as mentioned above, I could successfully load the library and this can be verified by running the following command -
>> libisloaded('shrlibsample')
A value of 1 indicates that the library has been loaded while a value of 0 indicates that it is not loaded.
So as you mentioned, this issue might be related to the compiler that is installed on your machine. But this is not usually true because these sample example files should be present by default on all MATLAB installation. Can you please verify if the file 'shrlibsample.mexw64' is present at the location 'C:\Program Files\MATLAB\R2015b\extern\examples\shrlib' or if the same location is accessible via MATLAB directory browser.
Additionally, if an appropriate compiler is installed on your machine, then the a fresh mex file can be generated and the process can be repeated. To verify if a compiler has been installed, please execute the following command at the MATLAB Command Window -
>> mex -setup
This will give a list of all installed compilers. If the same are installed on your machine, please repeat the procedure above to see if the problem persists.
I look forward to hearing from you.

Sign in to comment.

Categories

Find more on C Shared Library Integration 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!