本文介绍了MATLAB R2016b-MEX无法编译C ++代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的最新帖子杂乱无章的信息很多.部分原因是这个问题,希望它本身提供更多信息会更有意义.

My last post was cluttered with lots of information. Part of it was this problem, which hopefully on its own with more info will make more sense.

我正在尝试使用MATLAB R2016b中的mex在Windows上编译C ++ 03代码.

I am attempting to use mex in MATLAB R2016b to compile C++03 code on Windows.

当我尝试这样做时,出现以下错误:

When I try to do so, I get the following errors:

Error using mex
cpp_mexapi_version.o: In function `mexfilerequiredapiversion':
C:/Progra~1/MATLAB/R2016b/extern/version/cpp_mexapi_version.cpp:4: multiple definition of `mexfilerequiredapiversion'
C:\Users\myName\AppData\Local\Temp\2\mex_53351148310745_8852\cpp_mexapi_version.obj:cpp_mexapi_version.cpp:(.text+0x0): first defined here
myclass.o: In function `MyClass::MyClass(int, double*, int, int, int*, double, int, double, double, int, bool, char)':
C:\msys64\home\myName\Desktop\test/myclass.cpp:67: undefined reference to `__imp___acrt_iob_func'
C:\msys64\home\myName\Desktop\test/myclass.cpp:110: undefined reference to `__imp___acrt_iob_func'
C:\msys64\home\myName\Desktop\test/myclass.cpp:121: undefined reference to `__imp___acrt_iob_func'
myclass.o: In function `MyClass::MyClass(int, double*, int, int*, int, double, double, int, char)':
C:\msys64\home\myName\Desktop\test/myclass.cpp:139: undefined reference to `__imp___acrt_iob_func'
C:\msys64\home\myName\Desktop\test/myclass.cpp:178: undefined reference to `__imp___acrt_iob_func'
myclass.o:C:\msys64\home\myName\Desktop\test/myclass.cpp:189: more undefined references to `__imp___acrt_iob_func' follow
myclass.o: In function `MyAssertionClass::MyAssertionClass(char const*, int, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >)':
C:\msys64\home\myName\Desktop\test/myclass.cpp:1014: undefined reference to `std::__cxx11::basic_ostringstream<char, std::char_traits<char>, std::allocator<char> >::basic_ostringstream()'
myclass.o: In function `MyAssertionClass::MyAssertionClass(char const*, int, char const*)':
C:\msys64\home\myName\Desktop\test/myclass.cpp:1029: undefined reference to `std::__cxx11::basic_ostringstream<char,
std::char_traits<char>, std::allocator<char> >::basic_ostringstream()'
collect2.exe: error: ld returned 1 exit status

这是失败之前的详细日志:

Here's the verbose log from before it fails:

>> mex -v COMPFLAGS='$COMPFLAGS -std=c++03' -IC:\ourLibraryLocation myTargetMex.cpp myclass.o (a few other .o files here) cpp_mexapi_version.o
Verbose mode is on.
Neither -compatibleArrayDims nor -largeArrayDims is selected.
     Using -compatibleArrayDims. In the future, MATLAB will require the use of
     -largeArrayDims and remove the -compatibleArrayDims option.
     For more information:
     http://www.mathworks.com/help/matlab/matlab_external/upgrading-mex-files-to-use-64-bit-api.html.
... Looking for compiler 'MinGW64 Compiler (C++)' ...
... Looking for environment variable 'MW_MINGW64_LOC' ...Yes ('C:\TDM-GCC-64').
... Looking for file 'C:\TDM-GCC-64\bin\g++.exe' ...Yes.
... Looking for folder 'C:\TDM-GCC-64' ...Yes.
Found installed compiler 'MinGW64 Compiler (C++)'.
Set PATH = C:\TDM-GCC-64\bin;C:\Program Files\MATLAB\R2016b\extern\include\win64;C:\Program Files\MATLAB\R2016b\extern\include;C:\Program Files\MATLAB\R2016b\simulink\include;C:\Program Files\MATLAB\R2016b\lib\win64;C:\Program Files (x86)\Common Files\Oracle\Java\javapath;C:\Program Files (x86)\Intel\Intel(R) Management Engine Components\iCLS\;C:\Program Files\Intel\Intel(R) Management Engine Components\iCLS\;C:\Program Files\avs\bin;C:\Program Files\avs\bin32;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Windows\System32\OpenSSH\;C:\Program Files (x86)\Enterprise Vault\EVClient\;C:\Program Files\Intel\WiFi\bin\;C:\Program Files\Common Files\Intel\WirelessCommon\;C:\Program Files (x86)\Intel\Intel(R) Management Engine Components\DAL;C:\Program Files\Intel\Intel(R) Management Engine Components\DAL;C:\Program Files (x86)\NVIDIA Corporation\PhysX\Common;C:\Program Files\MATLAB\R2016b\bin;C:\TDM-GCC-64\bin;C:\Users\myName\AppData\Local\Microsoft\WindowsApps;
Set INCLUDE = C:\TDM-GCC-64\include;C:\TDM-GCC-64\include;
Set LIB = C:\TDM-GCC-64\lib;;C:\TDM-GCC-64\lib;;
Set MW_TARGET_ARCH = win64;win64;
Set LIBPATH = C:\Program Files\MATLAB\R2016b\extern\lib\win64;C:\Program Files\MATLAB\R2016b\extern\lib\win64;
Options file details
-------------------------------------------------------------------
    Compiler location: C:\TDM-GCC-64
    Options file: C:\Users\myName\AppData\Roaming\MathWorks\MATLAB\R2016b\mex_C++_win64.xml
    CMDLINE2 : C:\TDM-GCC-64\bin\g++ -m64 -Wl,--no-undefined -shared -s -Wl,"C:\Program Files\MATLAB\R2016b/extern/lib/win64/mingw64/exportsmexfileversion.def" C:\Users\myName\AppData\Local\Temp\2\mex_53351148310745_8852\myTargetMex.obj C:\Users\myName\AppData\Local\Temp\2\mex_53351148310745_8852\cpp_mexapi_version.obj myclass.o (a few other .o files here) cpp_mexapi_version.o  -L"C:\Program Files\MATLAB\R2016b\extern\lib\win64\mingw64" -llibmx -llibmex -llibmat -lm -llibmwlapack -llibmwblas -o myTargetMex.mexw64
    CXX : C:\TDM-GCC-64\bin\g++
    COMPILER : C:\TDM-GCC-64\bin\g++
    DEFINES : -DMX_COMPAT_32   -m64 -DMATLAB_MEX_FILE
    MATLABMEX : -DMATLAB_MEX_FILE
    CXXFLAGS : -fexceptions -fno-omit-frame-pointer -std=c++11
    INCLUDE : -IC:\ourLibraryLocation  -I"C:\Program Files\MATLAB\R2016b/extern/include" -I"C:\Program Files\MATLAB\R2016b/simulink/include"
    CXXOPTIMFLAGS : -O -DNDEBUG
    CXXDEBUGFLAGS : -g
    LDXX : C:\TDM-GCC-64\bin\g++
    LINKER : C:\TDM-GCC-64\bin\g++
    LDFLAGS : -m64 -Wl,--no-undefined
    LDTYPE : -shared
    LINKEXPORT : -Wl,"C:\Program Files\MATLAB\R2016b/extern/lib/win64/mingw64/mexFunction.def"
    LINKEXPORTVER : -Wl,"C:\Program Files\MATLAB\R2016b/extern/lib/win64/mingw64/exportsmexfileversion.def"
    LIBLOC : C:\Program Files\MATLAB\R2016b\extern\lib\win64\mingw64
    LINKLIBS : -L"C:\Program Files\MATLAB\R2016b\extern\lib\win64\mingw64" -llibmx -llibmex -llibmat -lm -llibmwlapack -llibmwblas
    LDOPTIMFLAGS : -s
    LDDEBUGFLAGS : -g
    OBJEXT : .obj
    LDEXT : .mexw64
    SETENV : set COMPILER=g++
                set COMPFLAGS=-c -fexceptions -fno-omit-frame-pointer -std=c++11 -DMX_COMPAT_32   -m64 -DMATLAB_MEX_FILE  -DMATLAB_MEX_FILE
                set OPTIMFLAGS=-O -DNDEBUG
                set DEBUGFLAGS=-g
                set LINKER=g++
                set LINKFLAGS=-m64 -Wl,--no-undefined -shared -L"C:\Program Files\MATLAB\R2016b\extern\lib\win64\mingw64" -llibmx -llibmex -llibmat -lm -llibmwlapack -llibmwblas -Wl,"C:\Program Files\MATLAB\R2016b/extern/lib/win64/mingw64/mexFunction.def"
                set LINKDEBUGFLAGS=-g
                set NAME_OUTPUT= -o "%OUTDIR%%MEX_NAME%%MEX_EXT%"
    MINGWROOT : C:\TDM-GCC-64
    MATLABROOT : C:\Program Files\MATLAB\R2016b
    ARCH : win64
    SRC : C:\Users\myName\Desktop\test\myTargetMex.cpp;"C:\Program Files\MATLAB\R2016b\extern\version\cpp_mexapi_version.cpp"
    OBJ : C:\Users\myName\AppData\Local\Temp\2\mex_53351148310745_8852\myTargetMex.obj;C:\Users\myName\AppData\Local\Temp\2\mex_53351148310745_8852\cpp_mexapi_version.obj
    OBJS : C:\Users\myName\AppData\Local\Temp\2\mex_53351148310745_8852\myTargetMex.obj C:\Users\myName\AppData\Local\Temp\2\mex_53351148310745_8852\cpp_mexapi_version.obj myclass.o (a few other .o files here) cpp_mexapi_version.o
    SRCROOT : C:\Users\myName\Desktop\test\myTargetMex
    DEF : C:\Users\myName\AppData\Local\Temp\2\mex_53351148310745_8852\myTargetMex.def
    EXP : myTargetMex.exp
    LIB : myTargetMex.lib
    EXE : myTargetMex.mexw64
    ILK : myTargetMex.ilk
    MANIFEST : myTargetMex.mexw64.manifest
    TEMPNAME : myTargetMex
    EXEDIR :
    EXENAME : myTargetMex
    COMPFLAGS :  -std=c++03
    OPTIM : -O -DNDEBUG
    LINKOPTIM : -s
    CMDLINE1_0 : C:\TDM-GCC-64\bin\g++ -c -DMX_COMPAT_32   -m64 -DMATLAB_MEX_FILE  -IC:\ourLibraryLocation  -I"C:\Program Files\MATLAB\R2016b/extern/include" -I"C:\Program Files\MATLAB\R2016b/simulink/include" -fexceptions -fno-omit-frame-pointer -std=c++11 -O -DNDEBUG C:\Users\myName\Desktop\test\myTargetMex.cpp -o C:\Users\myName\AppData\Local\Temp\2\mex_53351148310745_8852\myTargetMex.obj
    CMDLINE1_1 : C:\TDM-GCC-64\bin\g++ -c -DMX_COMPAT_32   -m64 -DMATLAB_MEX_FILE  -IC:\ourLibraryLocation  -I"C:\Program Files\MATLAB\R2016b/extern/include" -I"C:\Program Files\MATLAB\R2016b/simulink/include" -fexceptions -fno-omit-frame-pointer -std=c++11 -O -DNDEBUG "C:\Program Files\MATLAB\R2016b\extern\version\cpp_mexapi_version.cpp" -o C:\Users\myName\AppData\Local\Temp\2\mex_53351148310745_8852\cpp_mexapi_version.obj
-------------------------------------------------------------------
Building with 'MinGW64 Compiler (C++)'.
C:\TDM-GCC-64\bin\g++ -c -DMX_COMPAT_32   -m64 -DMATLAB_MEX_FILE  -IC:\ourLibraryLocation  -I"C:\Program Files\MATLAB\R2016b/extern/include" -I"C:\Program Files\MATLAB\R2016b/simulink/include" -fexceptions -fno-omit-frame-pointer -std=c++11 -O -DNDEBUG C:\Users\myName\Desktop\test\myTargetMex.cpp -o C:\Users\myName\AppData\Local\Temp\2\mex_53351148310745_8852\myTargetMex.obj
C:\TDM-GCC-64\bin\g++ -c -DMX_COMPAT_32   -m64 -DMATLAB_MEX_FILE  -IC:\ourLibraryLocation  -I"C:\Program Files\MATLAB\R2016b/extern/include" -I"C:\Program Files\MATLAB\R2016b/simulink/include" -fexceptions -fno-omit-frame-pointer -std=c++11 -O -DNDEBUG "C:\Program Files\MATLAB\R2016b\extern\version\cpp_mexapi_version.cpp" -o C:\Users\myName\AppData\Local\Temp\2\mex_53351148310745_8852\cpp_mexapi_version.obj
C:\TDM-GCC-64\bin\g++ -m64 -Wl,--no-undefined -shared -s -Wl,"C:\Program Files\MATLAB\R2016b/extern/lib/win64/mingw64/exportsmexfileversion.def" C:\Users\myName\AppData\Local\Temp\2\mex_53351148310745_8852\myTargetMex.obj C:\Users\myName\AppData\Local\Temp\2\mex_53351148310745_8852\cpp_mexapi_version.obj myclass.o (a few other .o files here) cpp_mexapi_version.o  -L"C:\Program Files\MATLAB\R2016b\extern\lib\win64\mingw64" -llibmx -llibmex -llibmat -lm -llibmwlapack -llibmwblas -o myTargetMex.mexw64

需要明确的是,MyClass只是myTargetMex中包含的另一个类.碰巧它是第一个,因此自然而然地是尝试编译时失败的第一件事. (据我所知)

To be clear, MyClass is just another class included into myTargetMex. It just so happens to be the first one, so it naturally is the first thing to fail when the compilation is attempted. (As far as I know)

感谢您的帮助!

推荐答案

其含义如下:

  1. 我需要使用$ CXXFLAGS而不是$ COMPFLAGS(详细输出显示COMPFLAGS从未在这里显示,但是CXXFLAGS了)

  1. I needed to use $CXXFLAGS instead of $COMPFLAGS (the verbose output shows that COMPFLAGS never shows up here, but CXXFLAGS does)

只需执行CXXFLAGS='$CXXFLAGS -std=c++03' 即可将标志添加到现有标志集合中. (Du,但我错过了)默认情况下,现有标志集包括-std=c++11.从技术上讲,最后一个只是覆盖了旧的,但看起来仍然很奇怪.更好的方法是:CXXFLAGS='-fexceptions -fno-omit-frame-pointer -std=c++03',所以我将其包含的所有内容都包括在内,但只是替换了c ++标准部分.您可以对要更改的任何内容执行相同的操作.就像CXXOPTIMFLAGS='-O0 -DNDEBUG'只是将-O替换为-O0

Doing CXXFLAGS='$CXXFLAGS -std=c++03' just adds flags to the existing set of flags. (Duh, but I missed this) The existing set of flags, by default, includes -std=c++11. Technically, the last one just over-writes the old one, but that's still odd-looking. The better way is to do: CXXFLAGS='-fexceptions -fno-omit-frame-pointer -std=c++03', so I include all the stuff it already had, but just replaced the c++ standard part. You can do the same with anything you want to change. Like CXXOPTIMFLAGS='-O0 -DNDEBUG' will just replace -O with -O0

作为旁注:如果您的项目混用了C和CPP,那就太麻烦了.帮个忙,将所有.C文件更改为.CPP.我们已经做到了,当MEX尝试编译.C文件时,它使用GCC而不是G ++,并且有些杂乱无章.最好避免这种情况.提醒一下,旧的mexFunction()入口点在.C文件中完全正常. mexFunction()不必位于.C文件中!

As a sidenote: If your project has mixed C and CPP, that's messy. Just do yourself a favor and change all your .C files to .CPP. We had that, and when MEX tried to compile the .C files, it used GCC instead of G++ and had a hissy fit. Better to avoid that. As a reminder, the old mexFunction() entry point works perfectly fine in .C files. mexFunction() does not need to be in a .C file!

非常感谢 Cris Luengo ,他再次用他的MATLAB和MEX文件知识救了我. :)如果他提出一个解决方案,我会把他的解决方案标记为答案.

Many thanks to Cris Luengo who has once again saved me with his MATLAB and MEX-file knowledge. :) I would've marked his solution as answer if he had put one up.

这篇关于MATLAB R2016b-MEX无法编译C ++代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-22 04:01