Load 64-Bit .dll Library from Sub-Folder

10 views (last 30 days)
Shawn Mason
Shawn Mason on 29 Sep 2017
Edited: Mike Arms on 25 Feb 2023
I have a 64-bit shared library that loads fine when the *.h, *.lib, and *.dll's are in the current folder along with the main application script. When I try to move the library files to a sub-directory the loadlibrary function fails linking to the library. I've tried both "addpath" to the subdirectory and adding the "includepath" input argument to the loadlibrary call. Is there another way to make the linker in the compiler aware of additional file paths? Error message is below:
Error using loadlibrary
Building BTICARD_thunk_pcwin64 failed. Compiler output is:
cl -I"C:\matlab\R2017a\extern\include" /W3 /nologo -I"C:\matlab\R2017a\extern\include"
-I"C:\matlab\R2017a\simulink\include" /DTARGET_API_VERSION=700 /D_CRT_SECURE_NO_DEPRECATE
/D_SCL_SECURE_NO_DEPRECATE /D_SECURE_SCL=0
-I"\\subdirectory\Matlab\Tools\64bit"
-I"\\subdirectory\Matlab\Tools\64bit\USB429" "BTICARD_thunk_pcwin64.c" -LD
-Fe"BTICARD_thunk_pcwin64.dll"
BTICARD_thunk_pcwin64.c
LINK : fatal error LNK1104: cannot open file 'bticard64.lib'

Answers (2)

Jatin Waghela
Jatin Waghela on 2 Oct 2017
One possible reason for this error could be that the function is trying to access the temp directory on your machine but the path has spaces which is why it is not read correctly. For example, if the path to your temp directory is similar to the one below, it will throw the above error:
“C:\Users\userName\DOCUMENTS\Local Settings\temp”
This path will incorrectly be read as
“C:\Users\userName\DOCUMENTS\Local”
And hence would fail to read a file that is actually stored in the temp directory.
To fix this please change your environment variable (TEMP/TMP) to reflect a path that does not have spaces like the one below (only an example):
“C:\Users\userName\DOCUMENTS\LocalSettings\temp”
Please follow the instructions to change the environment variable:
a) From inside MATLAB you can use the following commands:
newPath = 'new\path'; setenv('TEMP', newPath); setenv('TMP', newPath);
b) From Windows follow the instructions below:
1) Right-click My Computer, and then click Properties. 2) Click the Advanced tab. 3) Click Environment variables.
Click on the following options, for either a user or a system variable: a) Click New to add a new variable name and value. b) Click an existing variable, and then click Edit to change its name or value. c) Click an existing variable, and then click Delete to remove it.
Please note that by changing the environment variable, you are not only changing it for MATLAB but for all applications that refer to the variable.
  1 Comment
Shawn Mason
Shawn Mason on 2 Oct 2017
I reviewed my Environment variables and all temp variables were set to c:\temp. I've had issues with that before and I was given the 'notempdir' switch from a previous request. I verified that it must have that switch, even if everything is in the current folder.
It normally always fails with the .lib linking, so it seems that path information may be fed to the C compiler but not to the linker.
[notfound,warnings]=loadlibrary('BTICARD64.DLL','BTICARD.H','notempdir','alias','BTICARD');

Sign in to comment.


Mike Arms
Mike Arms on 25 Feb 2023
Edited: Mike Arms on 25 Feb 2023
I ran into this same issue in R2019b with a 64-bit library. The workaround I used was to wrap the library within a C# .NET Framework DLL, then add the wrapped DLL with NET.addAssembly. I was then able to make calls to all the original shared library methods. Fortunately, the C# API code was already available for the library, so I wasn't bogged down with writing all that.

Categories

Find more on Search Path in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!