Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
940 views
in Technique[技术] by (71.8m points)

matlab - Linking and LOADING static .lib with mex

So, I have a MEX gateway script file that calls my C source code. I've used the -L and -I commands to link my 64-bit compiled GSL libraries (.libs) to my mex executable, which is then compiled under the extension of .mexw64.

I want for this executable to be transferred to another windows machine and run fine, without any GSL libraries installed. That is the the only solution, I don't care what he arguments are regarding the benefits of the dynamic linking/code generation upon compile-time are. I want an executable that has every function not only (of course) pre-declared, but also PRE-DEFINED.

I was lead to believe that this is what 'static' linking is vs. dynamic; but I've read some contradictory definitions all around the interwebs. I need a completely 100% standalone, singular file.

Supposedly you can link the actual .obj file in the mex function, which I can generate, but unfortunately I then get unresolved symbol errors.

Someone else mentioned that I can use the -l (lowercase L) to directly link the actual .lib(s) needed, statically, but that is NOT true.

So is there anyone that can lead me in the right direction, either how to have everything not only linked but to also have the DEFINITIONS linked and ready to load when executable is run--completely standalone, or why I am running into unresolved symbols/linker errors when I include my .obj file? Am I misunderstanding something elementary about the linking process?

Also: To elaborate a bit more, I have the GSL libraries built and linked via Visual Studio for the 64 bit architecture, and I can link it easily with MATLAB, so that is not my problem (any more).

EDIT: I've seen the post here: Generating standalone MEX file with GNU compilers, including libraries

This doesn't solve my problem, however, although it is the same question. I don't have access to gcc; it's finally compiling on the MSVS12 compiler in MATLAB, I'm not going try to recompile using GCC via MinGW (already tried, couldn't figure it out), so -static and .a options are out.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

In your previous post, you mentioned that you decided to compile GSL library with Visual C++, using the VS solution provided by Brian Gladman.

Here is a step-by-step illustration on how to build a MEX-function that links against GSL libraries statically:

  1. Download GNU GSL sources (GSL v1.16)
  2. Download the matching Visual Studio project files (VS2012 for GSL v1.16)
  3. Extract the GSL tarball, say to C:gsl-1.16
  4. Extract the VS project files on top of the sources, this will overwrite three files as well as add a folder C:gsl-1.16uild.vc11.
  5. Open Visual Studio 2012, and load the solution: C:gsl-1.16uild.vc11gsl.lib.sln
  6. Change the configuration to the desired output: for me I chose platform=x64 and mode=Release
  7. First you must build the gslhdrs project first
  8. Now build the whole solution. This will create two static libraries cblas.lib and gsl.lib stored in C:gsl-1.16libx64Release (along with corresponding PDB debugging symbols). It will also create a directory containing the final header files: C:gsl-1.16gsl

Next we proceed to build a MEX-function. Take the following simple program (computes some value from a Bessel function, and return it as output):

gsl_test.c

#include "mex.h"
#include <gsl/gsl_sf_bessel.h>

void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
{
    if (nrhs != 0 || nlhs > 1) mexErrMsgTxt("Wrong number of args.");
    plhs[0] = mxCreateDoubleScalar(gsl_sf_bessel_J0(5.0));
}

This is how to compile the above C code in MATLAB:

>> mex -largeArrayDims gsl_test.c -I"C:gsl-1.16" -L"C:gsl-1.16libx64Release" cblas.lib gsl.lib

Finally we test the MEX-file, and compare it against the value reported by MATLAB's own Bessel function:

>> x = gsl_test()
ans =
   -0.1776

>> y = besselj(0,5)
y =
   -0.1776

>> max(x-y)    % this should be less than eps
ans =
   8.3267e-17

Note that the built MEX-function has no external DLL dependencies (other than "Visual C Runtime" which is expected, and the usual MATLAB libraries). You can verify that by using Dependency Walker if you want. So you can simply deploy the gsl_test.mexw64 file alone (assuming the users already have the corresponding VC++ runtime installed on their machines).


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...