C++ .dll not found (yet in path?)

9 次查看(过去 30 天)
Terence Macquart
Terence Macquart 2022-10-18
Hi,
I'm trying to learn how to link C++ code with matlab. The current option I am trying is the one explained here https://uk.mathworks.com/help/releases/R2019a/matlab/matlab_external/what-you-need-to-import-cpp-library-functions-into-matlab.html
I have created a typical .dll from a visual studio tutorial: https://learn.microsoft.com/en-us/cpp/build/walkthrough-creating-and-using-a-dynamic-link-library-cpp?view=msvc-170, which I have tested and appears to work fine in C++.
I have been able to follow most steps to link the dll to matlab (in the main file called "secondTest.m"):
clear; close all; clc;
mex -setup cpp
headerFile = 'C:\GitFolder\Project_C\MyDLL\MathLibrary\MathLibrary\MathLibrary.h';
libFile = 'C:\GitFolder\Project_C\MyDLL\MathLibrary\x64\Debug\MathLibrary.lib';
clibgen.generateLibraryDefinition(headerFile,'Libraries',libFile) % requires a DLL!
summary(defineMathLibrary)
build(defineMathLibrary)
% works up to here
Which return in the cmd window
% MEX configured to use 'MinGW64 Compiler (C++)' for C++ language compilation.
%
% To choose a different C++ compiler, select one from the following:
% MinGW64 Compiler (C++) mex -setup:C:\Users\tm16524\AppData\Roaming\MathWorks\MATLAB\R2021a\mex_C++_win64.xml C++
% MinGW64 Compiler with Windows 10 SDK or later (C++) mex -setup:'C:\Program Files\MATLAB\R2021a\bin\win64\mexopts\mingw64_g++_sdk10+.xml' C++
% Using MinGW64 Compiler (C++) compiler.
% Generated definition file defineMathLibrary.mlx and data file 'MathLibraryData.xml' contain definitions for 4 constructs supported by MATLAB.
% Build using build(defineMathLibrary).
%
% MATLAB Interface to MathLibrary Library
%
% Functions
% clib.MathLibrary.fibonacci_init(uint64,uint64)
% logical clib.MathLibrary.fibonacci_next()
% uint64 clib.MathLibrary.fibonacci_current()
% uint32 clib.MathLibrary.fibonacci_index()
%
% Building interface file 'MathLibraryInterface.dll'.
% Interface file 'MathLibraryInterface.dll' built in folder 'C:\GitFolder\Project_C\MyDLL\MathLibrary\MathLibrary'.
% To use the library, add the interface file folder to the MATLAB path.
The issue seems to be link to the run-time path. I have tried updating the system path by changing the environment variables as explained here: https://uk.mathworks.com/help/matlab/matlab_external/building-and-running-engine-applications-on-windows-operating-systems.html
but also live with:
% update path
% dllPath = 'C:\GitFolder\Project_C\MyDLL\MathLibrary\MathLibrary'; % tried both just in case
dllPath = 'C:\GitFolder\Project_C\MyDLL\MathLibrary\MathLibrary\MathLibraryInterface.dll';
syspath = getenv('PATH');
setenv('PATH',[dllPath pathsep syspath]);
getenv('PATH') % here i check the path is indeed updated
Then I finally try to call the function
% function call
clib.MathLibrary.fibonacci_init(1,1)
which returns the following error
% clib.MathLibrary.fibonacci_init(1,1)
% Unable to load interface library:
% 'C:\GitFolder\Project_C\MyDLL\MathLibrary\MathLibrary\MathLibraryInterface.dll'.
% Reason: The specified module could not be found.
% Ensure the C++ dependent libraries for the interface library are added to
% run-time path.
But the "MathLibraryInterface.dll" exists and is in that location. I have attached all files used in a .zip file.
I would greatly appreciate some help with this issue. I am using matlab release R2021a on windows.
Thanks.
Terence

回答(1 个)

Maneet Kaur Bagga
Maneet Kaur Bagga 2023-9-14
Hi Terence,
As per my understanding the error can be resolved by placing the compiled library file in the folder with the built interface file before calling the interface file.
Please refer to the following code for better understanding of the solution:
% Setup
hFile = 'C:\GitFolder\Project_C\MyDLL\MathLibrary\MathLibrary\MathLibrary.h';
libFile = 'C:\GitFolder\Project_C\MyDLL\MathLibrary\x64\Debug\MathLibrary.lib';
libname = "MathLibrary";
% Generate definition file for C++ library
clibgen.generateLibraryDefinition( ...
hFile, ...
PackageName=libname,...
Libraries=libFile, ...
ReturnCArrays=false,...
OverwriteExistingDefinitionFiles=true);
%% Build the interface files
build(definematrixOperations);
summary(defineMathLibrary);
addpath(libname);
%% Copy Library File to Interface Folder
dllPath = 'C:\GitFolder\Project_C\MyDLL\MathLibrary\MathLibrary\MathLibraryInterface.dll';
copyfile(dllFile, libname, "f")
doc clib.matrixOperations
Please refer to the following link for more information:
clibgen.generateLibraryDefinition
Hope this helps!
Thank You
Maneet Bagga

标签

产品


版本

R11.1

Community Treasure Hunt

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

Start Hunting!

Translated by