如何从linux交叉编译时链接到第三方DLL?

我想越过使用mingw-w64工具从Linux的编译对于Windows 64位C++项目(其实我使用的是基于MinGW的-W64 M Cross Environment,但我不认为这是相关的问题)。在我的情况下,最后一步需要链接到几个第三方编译的dll。我没有这些DLL的源代码,只有头文件和编译后的二进制文件使用什么编译?我不知道,但可能Visual Studio。如何从linux交叉编译时链接到第三方DLL?

要解释清楚我在做什么,我想从Linux的交叉编译Matlab的MEX文件,需要链接到Matlab的库libmex.dll,libmx.dll和libmat.dll。但是,我不认为需要任何Matlab知识来回答这个问题。

我隐约地感到自己需要从动态链接库中提取的定义,到.DEF文件,这也许就是我需要建立某种实际链接到另一个链接库。

我已尝试这两个步骤,第一个创建的定义文件对每个库,我用下面的命令做了:

gendef libmx.dll 

这里是造成定义文件的片断libmx.def

; 

; Definition file of libmx.dll

; Automatic generated by gendef

; written by Kai Tietz 2008

;

LIBRARY "libmx.dll"

EXPORTS

; public: __cdecl <struct mxArray_tag,class Mprotected_mxArray_helper>::<struct mxArray_tag,class Mprotected_mxArray_helper>(class <struct mxArray_tag,class Mprotected_mxArray_helper> const &__ptr64)__ptr64

[email protected][email protected]@[email protected]@@@[email protected]@@Z

; public: __cdecl <struct mxArray_tag,class Mprotected_mxArray_helper>::<struct mxArray_tag,class Mprotected_mxArray_helper>(struct mxArray_tag *__ptr64,bool)__ptr64

[email protected][email protected]@[email protected]@@@[email protected][email protected]@[email protected]

; public: __cdecl <struct mxArray_tag,class Mprotected_mxArray_helper>::<struct mxArray_tag,class Mprotected_mxArray_helper>(void)__ptr64

[email protected][email protected]@[email protected]@@@[email protected]

; public: __cdecl matrix::serialize::Exception::Exception(class matrix::serialize::Exception const &__ptr64)__ptr64

[email protected]@[email protected]@[email protected]@@Z

; public: __cdecl matrix::serialize::Exception::Exception(int,__int64)__ptr64

[email protected]@[email protected]@[email protected][email protected]

; public: __cdecl matrix::serialize::FailedUCNV::FailedUCNV(class matrix::serialize::FailedUCNV const &__ptr64)__ptr64

[email protected]@[email protected]@[email protected]@@Z

; public: __cdecl matrix::serialize::FailedUCNV::FailedUCNV(int,__int64,int)__ptr64

[email protected]@[email protected]@[email protected][email protected]

; public: __cdecl Mprotected_mxArray::Mprotected_mxArray(class Mprotected_mxArray const &__ptr64)__ptr64

[email protected]@[email protected]@@Z

; public: __cdecl Mprotected_mxArray::Mprotected_mxArray(struct mxArray_tag *__ptr64,bool)__ptr64

[email protected]@[email protected][email protected]@[email protected]

; public: __cdecl Mprotected_mxArray::Mprotected_mxArray(void)__ptr64

[email protected]@[email protected]

; public: __cdecl RRTableVisitor::RRTableVisitor(class RRTableVisitor const &__ptr64)__ptr64

[email protected]@[email protected]@@Z

然后,我试图根据Mingw here提供的说明创建导入库。然而,由于我使用的MinGW-W64我的命令竟是

x86_64-w64-mingw32.static-dlltool -d libmx.def -l libmx.a 

然而,当我试图建立连接到这些库一个简单的例子,我得到一个错误。以下是用于构建和链接库的实际命令。

/opt/mxe/usr/bin/x86_64-w64-mingw32.static-gcc -c -I/usr/local/MATLAB/R2015a/extern/include -I/usr/local/MATLAB/R2015a/simulink/include -DMATLAB_MEX_FILE -std=c99 -D_GNU_SOURCE -fexceptions -fPIC -fno-omit-frame-pointer -pthread -I/opt/mxe/usr/bin/include -DMX_COMPAT_32 -O -DNDEBUG "/home/rcrozier/yprime.c" 

/opt/mxe/usr/bin/x86_64-w64-mingw32.static-gcc -O -L/home/rcrozier/Sync/work/matlab_windows_libs/r2013a -L/opt/mxe/usr/bin/lib -pthread -Wl,--version-script,/usr/local/MATLAB/R2015a/extern/lib/glnxa64/mexFunction.map -Wl,--no-undefined -I/usr/local/MATLAB/R2015a/extern/include -o "yprime.mexw64" yprime.o -Wl,-rpath-link -L/home/rcrozier/Sync/work/matlab_windows_libs/r2013a -lmx -lmex -lmat -L/opt/mxe/usr/bin/lib -lm -lstdc++

编译步骤去确定,但链接步骤失败,出现以下错误:

yprime.o:yprime.c:(.text+0x53): undefined reference to `mxGetM' 

yprime.o:yprime.c:(.text+0x5f): undefined reference to `mxGetN'

yprime.o:yprime.c:(.text+0x6b): undefined reference to `mxIsDouble'

yprime.o:yprime.c:(.text+0x78): undefined reference to `mxIsComplex'

yprime.o:yprime.c:(.text+0xbe): undefined reference to `mxCreateDoubleMatrix_700'

yprime.o:yprime.c:(.text+0xca): undefined reference to `mxGetPr'

yprime.o:yprime.c:(.text+0xd5): undefined reference to `mxGetPr'

yprime.o:yprime.c:(.text+0xde): undefined reference to `mxGetPr'

/opt/mxe/usr/lib/gcc/x86_64-w64-mingw32.static/4.9.4/../../../../x86_64-w64-mingw32.static/lib/../lib/libmingw32.a(lib64_libmingw32_a-crt0_c.o): In function `main':

/opt/mxe/tmp-gcc-x86_64-w64-mingw32.static/gcc-4.9.4/mingw-w64-v4.0.6/mingw-w64-crt/crt/crt0_c.c:18: undefined reference to `WinMain'

collect2: error: ld returned 1 exit status

我很高兴此编译的结果静态链接。

所以我的问题是,究竟是什么我必须做在Linux当交叉编译的Windows链接到第三方的DLL,为什么会连接步骤是在这种情况下失败?

顺便说一句,这个特定的例子是一个简单的C文件的例子,实际上我也想链接C++文件。随意指出我的流程中涉及到对C和C++构建流程感到困惑的任何明显缺陷!

回答:

你确定你转换成静态库的三个库不依赖于其他没有提供的窗口的dll(你可以用dep walker来查看,http://www.dependencywalker.com/,在wine中)?

我也想交叉编译MEX文件,但我没能找到一个解决方案,在Linux完全地工作。

因此,我所做的就是将我的项目作为单独的独立的dll进行交叉编译,最后使用来自matlab的mex和dll上的链接来编译windows上mex文件的“主”。

如果你没有接入Windows电脑,也许你可以尝试安装matlab和mingw葡萄酒。

以上是 如何从linux交叉编译时链接到第三方DLL? 的全部内容, 来源链接: utcz.com/qa/263653.html

回到顶部