问题
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.
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
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)
Thanks for any assistance!
回答1:
Here's what it came down to:
I needed to use $CXXFLAGS instead of $COMPFLAGS (the verbose output shows that COMPFLAGS never shows up here, but CXXFLAGS does)
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. LikeCXXOPTIMFLAGS='-O0 -DNDEBUG'
will just replace-O
with-O0
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!
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.
来源:https://stackoverflow.com/questions/57579194/matlab-r2016b-mex-fails-to-compile-c-code