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

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

问题描述

所以,我有一个调用我的 C 源代码的 MEX 网关脚本文件.我使用 -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 文件

然而,这并不能解决我的问题,尽管它是同一个问题.我无权访问 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.

推荐答案

在你的 上一篇文章,您提到您决定使用 Visual C++ 编译 GSL 库,使用 布莱恩格拉德曼.

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 压缩包,例如 C:gsl-1.16
  4. 解压VS项目文件到源码之上,这将覆盖三个文件,并添加一个文件夹C:gsl-1.16uild.vc11.
  5. 打开 Visual Studio 2012,加载解决方案:C:gsl-1.16uild.vc11gsl.lib.sln
  6. 将配置更改为所需的输出:对我来说,我选择了 platform=x64mode=Release
  7. 首先你必须先构建 gslhdrs 项目
  8. 现在构建整个解决方案.这将创建两个静态库 cblas.libgsl.lib 存储在 C:gsl-1.16libx64Release 中(以及带有相应的 PDB 调试符号).它还将创建一个包含最终头文件的目录:C:gsl-1.16gsl
  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

接下来我们继续构建一个 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.16libx64Release" 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天全站免登陆