How can I compile and link static libraries from the mingw64 compiler on a windows system using the mex command with the suffix file as ".a"?

54 views (last 30 days)
SYSTEM: Windows10 21H2
Matlab VERSION: R2022a
Complier: mingw64
Compile Error: the compiler reports an error that "libopencv_world440.dll.a.lib" cannot be found
Issue description:
Here's the problem: I want to use mex to call OpenCV C++ programs in a matlab environment, and I've already pass compiled the OpenCV computer vision library on windows as a complete binary file using the mingw64 compiler (i.e. I get "libopencv_world440. dll.a" static library and "libopencv_world440.dll" dynamic library). Now as an extra I have also written the mex C++ program and finally want to compile it via the mex command, which has the "-l" parameter command to specify the name of the library to be linked, which according to the official parameter description is of the form "-llibname", with the suffix "But the problem is that my library name above is suffixed with ".a" and is written as "-lopencv_world440.dll.a " does not compile well in this form(the compilation reports an error that the libopencv_world440.dll.a.lib file cannot be found), what can I do to link the ".a" library file correctly? your answer would be like greatly appreciate!
-------------------------------------IN CHINESE--------------------------------------
问题是这样的:我想在matlab环境中使用mex调用OpenCV C++程序,目前我已经在windows系统上通过mingw64编译器对OpenCV计算机视觉库进行了完整编译为二进制文件(即得到“libopencv_world440.dll.a”静态库和“libopencv_world440.dll”动态库)。现在额外我也写好了mex C++程序,最后想通过mex命令来编译,这其中有“-l”参数指令来指定需要链接的库名字,根据官方参数说明,形式为“-llibname”,后缀“.lib”会被自动加上去,但问题是我上面的库名字后缀是“.a”,写成“-lopencv_world440.dll.a”这种形式编译不通过(编译报错无法找到libopencv_world440.dll.a.lib文件),请问该如何做才能正确把“.a”的库文件链接起来?

Accepted Answer

cui on 19 Sep 2022
Edited: cui on 19 Sep 2022
when "libopencv_world440.dll.a" copied to matlab's current working directory,then I was able to pass and run the Mex file successfully without specifying "-l", the reference example is as follows:
% Notice: first use "mex -setup" to choose your c/c++ compiler
%% -------------------------------------------------------------------
%% get the architecture of this computer
is_64bit = strcmp(computer,'MACI64') || strcmp(computer,'GLNXA64') || strcmp(computer,'PCWIN64');
%% -------------------------------------------------------------------
%% the configuration of compiler
% You need to modify this configuration according to your own path of OpenCV
% 注意:你的VS/MinGW64 OpenCV平台一定要匹配Matlab 64位的!
out_dir='./';% 当前目录
CPPFLAGS = ' -g -ID:\opencv_4_4_0\MinGW64_v8_OpenCV4_4_Contrib_install\include -ID:\opencv_4_4_0\MinGW64_v8_OpenCV4_4_Contrib_install\include\opencv2'; % your OpenCV "include" path
LDFLAGS = ' -LD:\opencv_4_4_0\MinGW64_v8_OpenCV4_4_Contrib_install\x64\mingw\lib'; % 用OpenCV release版本的"lib"路径
% LIBS = ' -lopencv_world440'; % msvc release版本的lib,无后缀,系统会自动加上去
LIBS = ' libopencv_world440.dll.a'; % mingw64 编译器.a库文件,无"-l"参数
if is_64bit
CPPFLAGS = [CPPFLAGS ' -largeArrayDims'];
%% add your files here!
compile_files = [
% the list of your code files which need to be compiled
' D:\vs_files\project1\project1\findCheckerBoarderCorners.cpp'
%% compiling...
str = compile_files;
fprintf('compilation of: %s\n', str);
str = [str ' -outdir ' out_dir CPPFLAGS LDFLAGS LIBS];
args = regexp(str, '\s+', 'split');
fprintf('Congratulations, compilation successful!!!\n');

More Answers (0)

Community Treasure Hunt

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

Start Hunting!