用mex链接和加载静态.lib [英] Linking and LOADING static .lib with mex

查看:358
本文介绍了用mex链接和加载静态.lib的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我有一个MEX网关脚本文件,该文件调用了我的C源代码.我已使用-L和-I命令将64位编译的GSL库(.libs)链接到mex可执行文件,该文件随后以.mexw64的扩展名进行编译.

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.

我希望将此可执行文件传输到另一台Windows计算机并正常运行,而未安装任何GSL库.那是唯一的解决方案,我不在乎他的论点是关于编译时动态链接/代码生成的好处.我想要一个可执行文件,它不仅具有(当然)已预先声明的所有功能,而且还具有预定义的功能.

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.

我被认为是静态"链接与动态链接的对立面.但是我已经阅读了所有有关互连网的矛盾定义.我需要一个完全100%独立的单文件.

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.

据说您可以在可以生成的mex函数中链接实际的.obj文件,但是不幸的是,我随后遇到了无法解决的符号错误.

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

有人提到我可以使用-l(小写L)直接静态链接所需的实际.lib,但这不是事实.

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.

有没有人可以引导我朝着正确的方向发展,要么如何不仅链接所有内容,而且还链接所有定义并准备在可执行文件运行时加载-完全独立,或者为什么我会陷入困境?当包括我的.obj文件时,未解决的符号/链接器错误?我是否误解了有关链接过程的基本知识?

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?

也:为了进一步说明,我通过Visual Studio为64位体系结构构建并链接了GSL库,并且可以轻松地将其与MATLAB链接,所以这不再是我的问题.

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).

我在这里看过这篇文章: 使用GNU编译器(包括库)生成独立的MEX文件

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

尽管这是相同的问题,但这并不能解决我的问题.我无权使用gcc;它终于在MATLAB中的MSVS12编译器上进行编译,我不会尝试通过MinGW使用GCC重新编译(已经尝试过,无法弄清楚),因此-static和.a选项不存在.

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.

推荐答案

在您的布莱恩·格拉德曼.

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

这是有关如何构建与GSL库静态链接的MEX函数的分步说明,

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

  1. 下载GNU GSL源( GSL v1.16 )
  2. 下载匹配的Visual Studio项目文件( VS2012 for GSL v1.16 )
  3. 提取GSL tarball,对C:\gsl-1.16
  4. 在源代码上方提取VS项目文件,这将覆盖三个文件并添加一个文件夹C:\gsl-1.16\build.vc11.
  5. 打开Visual Studio 2012,并加载解决方案:C:\gsl-1.16\build.vc11\gsl.lib.sln
  6. 将配置更改为所需的输出:对我来说,我选择了platform=x64mode=Release
  7. 首先,您必须先构建gslhdrs项目
  8. 现在构建整个解决方案.这将创建两个存储在C:\gsl-1.16\lib\x64\Release中的静态库cblas.libgsl.lib(以及相应的PDB调试符号).它还将创建一个包含最终头文件的目录:C:\gsl-1.16\gsl
  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.16\build.vc11.
  5. Open Visual Studio 2012, and load the solution: C:\gsl-1.16\build.vc11\gsl.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.16\lib\x64\Release (along with corresponding PDB debugging symbols). It will also create a directory containing the final header files: C:\gsl-1.16\gsl

接下来,我们继续构建MEX功能.使用以下简单程序(从Bessel函数中计算一些值,并将其作为输出返回):

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):

#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));
}

这是在MATLAB中编译以上C代码的方法:

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.16\lib\x64\Release" cblas.lib gsl.lib

最后,我们测试MEX文件,并将其与MATLAB自己的Bessel函数报告的值进行比较:

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

请注意,内置的MEX函数没有外部DLL依赖项(预期的"Visual C运行时"和常规的MATLAB库除外).您可以根据需要使用 Dependency Walker 进行验证.因此,您只需简单地单独部署gsl_test.mexw64文件(假设用户已经具有相应的 VC ++运行时安装在他们的计算机上.

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).

这篇关于用mex链接和加载静态.lib的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆