链接 c++ 时遇到问题带有 mex 的静态库

发布于 2024-11-17 00:27:41 字数 3989 浏览 3 评论 0原文

我正在尝试在 mex 文件中使用 C++ 静态库,但无法链接它。

这是我尝试 mex 的 cpp 文件的源代码:

#include <math.h>
#include <matrix.h>
#include <mex.h>
#include <iostream>
#include "pbogus.h"



void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
{
pbogus p;
int x=p.fbogus(2);

 mexPrintf("%d",x);

 }

这是我的“库”的头文件和源代码:

//header
#ifndef PBOGUS_H
#define PBOGUS_H

class pbogus
{

public:
int fbogus(int );
};


#endif


//source
#include "pbogus.h"

int pbogus::fbogus(int x)
{
return 2*x;

}

该库名为 testsymcpp.lib

这是我正在使用的 mex 命令:

 mex -v -L. -ltestsymcpp hello.cpp

这是输出:

 This is mex, Copyright 1984-2007 The MathWorks, Inc. 

     -> Default options filename found in C:\Users\mihai\AppData\Roaming\MathWorks\MATLAB\R2008a 
---------------------------------------------------------------- 
->    Options file           = C:\Users\mihai\AppData\Roaming\MathWorks\MATLAB\R2008a\mexopts.bat 
      MATLAB                 = D:\MATLAB\R2008a 
->    COMPILER               = cl 
->    Compiler flags: 
         COMPFLAGS           = -c -Zp8 -GR -W3 -EHs -D_CRT_SECURE_NO_DEPRECATE -D_SCL_SECURE_NO_DEPRECATE -D_SECURE_SCL=0 -DMATLAB_MEX_FILE -nologo /MD 
         OPTIMFLAGS          = -O2 -Oy- -DNDEBUG 
         DEBUGFLAGS          = -Zi -Fd"hello.mexw64.pdb" 
         arguments           =  
         Name switch         = /Fo 
->    Pre-linking commands   =  
->    LINKER                 = link 
->    Link directives: 
         LINKFLAGS           = /dll /export:mexFunction /MAP /LIBPATH:"D:\MATLAB\R2008a\extern\lib\win64\microsoft" libmx.lib libmex.lib libmat.lib /implib:C:\Users\mihai\AppData\Local\Temp\mex_NeoKF8\templib.x /MACHINE:X64 kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib 
         LINKDEBUGFLAGS      = /DEBUG /PDB:"hello.mexw64.pdb" 
         LINKFLAGSPOST       =  
         Name directive      = /out:"hello.mexw64" 
         File link directive =  
         Lib. link directive =  
         Rsp file indicator  = @ 
->    Resource Compiler      = rc /fo "mexversion.res" 
->    Resource Linker        =  
---------------------------------------------------------------- 


--> cl  -c -Zp8 -GR -W3 -EHs -D_CRT_SECURE_NO_DEPRECATE -D_SCL_SECURE_NO_DEPRECATE -D_SECURE_SCL=0 -DMATLAB_MEX_FILE -nologo /MD /FoC:\Users\mihai\AppData\Local\Temp\mex_NeoKF8\hello.obj -ID:\MATLAB\R2008a\extern\include -ID:\MATLAB\R2008a\simulink\include -O2 -Oy- -DNDEBUG -DMX_COMPAT_32 D:\scoala\studienarbeit\code\testgrey\hello.cpp 

hello.cpp 
    Contents of C:\Users\mihai\AppData\Local\Temp\mex_NeoKF8\mex_tmp.rsp: 
  C:\Users\mihai\AppData\Local\Temp\mex_NeoKF8\hello.obj 


--> link /out:"hello.mexw64" /dll /export:mexFunction /MAP /LIBPATH:"D:\MATLAB\R2008a\extern\lib\win64\microsoft" libmx.lib libmex.lib libmat.lib /implib:C:\Users\mihai\AppData\Local\Temp\mex_NeoKF8\templib.x /MACHINE:X64 kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib  @C:\Users\mihai\AppData\Local\Temp\mex_NeoKF8\mex_tmp.rsp  .\testsymcpp.lib  

Microsoft (R) Incremental Linker Version 9.00.30729.01 
Copyright (C) Microsoft Corporation.  All rights reserved. 

C:\Users\mihai\AppData\Local\Temp\mex_NeoKF8\hello.obj  
   Creating library C:\Users\mihai\AppData\Local\Temp\mex_NeoKF8\templib.x and object C:\Users\mihai\AppData\Local\Temp\mex_NeoKF8\templib.exp 
hello.obj : error LNK2019: unresolved external symbol "public: int __cdecl pbogus::fbogus(int)" (?fbogus@pbogus@@QEAAHH@Z) referenced in function mexFunction 
hello.mexw64 : fatal error LNK1120: 1 unresolved externals 

  D:\MATLAB\R2008A\BIN\MEX.PL: Error: Link of 'hello.mexw64' failed. 

??? Error using ==> mex at 207
Unable to complete successfully.

I am trying to use a c++ static library in a mex file and I cannot link it.

This is the source of the cpp file I am trying to mex:

#include <math.h>
#include <matrix.h>
#include <mex.h>
#include <iostream>
#include "pbogus.h"



void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
{
pbogus p;
int x=p.fbogus(2);

 mexPrintf("%d",x);

 }

This is the header file and the source code of my "library":

//header
#ifndef PBOGUS_H
#define PBOGUS_H

class pbogus
{

public:
int fbogus(int );
};


#endif


//source
#include "pbogus.h"

int pbogus::fbogus(int x)
{
return 2*x;

}

The library is named testsymcpp.lib

This is the mex command I am using:

 mex -v -L. -ltestsymcpp hello.cpp

And this is the output:

 This is mex, Copyright 1984-2007 The MathWorks, Inc. 

     -> Default options filename found in C:\Users\mihai\AppData\Roaming\MathWorks\MATLAB\R2008a 
---------------------------------------------------------------- 
->    Options file           = C:\Users\mihai\AppData\Roaming\MathWorks\MATLAB\R2008a\mexopts.bat 
      MATLAB                 = D:\MATLAB\R2008a 
->    COMPILER               = cl 
->    Compiler flags: 
         COMPFLAGS           = -c -Zp8 -GR -W3 -EHs -D_CRT_SECURE_NO_DEPRECATE -D_SCL_SECURE_NO_DEPRECATE -D_SECURE_SCL=0 -DMATLAB_MEX_FILE -nologo /MD 
         OPTIMFLAGS          = -O2 -Oy- -DNDEBUG 
         DEBUGFLAGS          = -Zi -Fd"hello.mexw64.pdb" 
         arguments           =  
         Name switch         = /Fo 
->    Pre-linking commands   =  
->    LINKER                 = link 
->    Link directives: 
         LINKFLAGS           = /dll /export:mexFunction /MAP /LIBPATH:"D:\MATLAB\R2008a\extern\lib\win64\microsoft" libmx.lib libmex.lib libmat.lib /implib:C:\Users\mihai\AppData\Local\Temp\mex_NeoKF8\templib.x /MACHINE:X64 kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib 
         LINKDEBUGFLAGS      = /DEBUG /PDB:"hello.mexw64.pdb" 
         LINKFLAGSPOST       =  
         Name directive      = /out:"hello.mexw64" 
         File link directive =  
         Lib. link directive =  
         Rsp file indicator  = @ 
->    Resource Compiler      = rc /fo "mexversion.res" 
->    Resource Linker        =  
---------------------------------------------------------------- 


--> cl  -c -Zp8 -GR -W3 -EHs -D_CRT_SECURE_NO_DEPRECATE -D_SCL_SECURE_NO_DEPRECATE -D_SECURE_SCL=0 -DMATLAB_MEX_FILE -nologo /MD /FoC:\Users\mihai\AppData\Local\Temp\mex_NeoKF8\hello.obj -ID:\MATLAB\R2008a\extern\include -ID:\MATLAB\R2008a\simulink\include -O2 -Oy- -DNDEBUG -DMX_COMPAT_32 D:\scoala\studienarbeit\code\testgrey\hello.cpp 

hello.cpp 
    Contents of C:\Users\mihai\AppData\Local\Temp\mex_NeoKF8\mex_tmp.rsp: 
  C:\Users\mihai\AppData\Local\Temp\mex_NeoKF8\hello.obj 


--> link /out:"hello.mexw64" /dll /export:mexFunction /MAP /LIBPATH:"D:\MATLAB\R2008a\extern\lib\win64\microsoft" libmx.lib libmex.lib libmat.lib /implib:C:\Users\mihai\AppData\Local\Temp\mex_NeoKF8\templib.x /MACHINE:X64 kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib  @C:\Users\mihai\AppData\Local\Temp\mex_NeoKF8\mex_tmp.rsp  .\testsymcpp.lib  

Microsoft (R) Incremental Linker Version 9.00.30729.01 
Copyright (C) Microsoft Corporation.  All rights reserved. 

C:\Users\mihai\AppData\Local\Temp\mex_NeoKF8\hello.obj  
   Creating library C:\Users\mihai\AppData\Local\Temp\mex_NeoKF8\templib.x and object C:\Users\mihai\AppData\Local\Temp\mex_NeoKF8\templib.exp 
hello.obj : error LNK2019: unresolved external symbol "public: int __cdecl pbogus::fbogus(int)" (?fbogus@pbogus@@QEAAHH@Z) referenced in function mexFunction 
hello.mexw64 : fatal error LNK1120: 1 unresolved externals 

  D:\MATLAB\R2008A\BIN\MEX.PL: Error: Link of 'hello.mexw64' failed. 

??? Error using ==> mex at 207
Unable to complete successfully.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文