在Matlab mex文件中编译c ++ 11代码时出错 [英] Error when compiling c++11 code in a Matlab mex file
问题描述
我已下载以下代码:
CVPR 2014交集优化代码
cvpr2014-iou- code-1.0.zip
http://research.microsoft.com/en-us/downloads/e164fe21-ef2b-4e34-98c1-4868968abb06/
我得到的是一个有两个文件夹的文件夹,一个名为 src /
,另一个名为
。我运行Linux Fedora 19,我可以编译代码在 src /
就好了。当我尝试编译 matlab /
中的代码时,会出现问题。
README说:
Matlab编译
----------------- -
在Matlab中,使用mex -setup配置编译器,然后在matlab\compile_windows.m或matlab / compile_linux.m脚本中调整
路径并运行
compile_linux .m或compile_windows.m。
更具体地针对Linux:
Linux
=====
从Matlab运行mex -setup ,然后编辑mexopts.sh文件并修改
的设置
CXXFLAGS =' - std = c ++ 11 -fopenmp -march = native -D_GNU_SOURCE'
CXXLIBS = $ RPATH $ MLIBS -lm -lgomp
CXXOPTIMFLAGS =' - Ofast -DNDEBUG'
这将启用OpenMP和C ++ 11,并且应该从G ++ 4.7开始工作。
从matlab /目录,调用compile_linux来构建所有的mex文件。
所以我打开 matlab
并运行 mex -setup
。我回答是
问题:
> mex -setup
选项文件控制使用哪个编译器,编译器和链接命令
选项以及要链接的运行时库。
使用'mex -setup'命令选择
放在/user/wok/home/.matlab/R2012b中的选项文件,默认情况下用于'mex'。在当前工作目录中或在命令行
上指定的选项
文件覆盖/user/wok/home/.matlab/R2012b中的默认选项文件。
要覆盖默认选项文件,请使用'mex -f'命令
(有关详细信息,请参阅'mex -help')。
mex的选项文件是:
1:/opt/matlab2012b/bin/mexopts.sh:
用于构建gcc MEX文件的模板选项文件
0:不更改退出
输入编译器的编号(0-1):
1
覆盖/user/wok/home/.matlab/R2012b/mexopts.sh([y] / n)?
y
/opt/matlab2012b/bin/mexopts.sh正被复制到
/user/wok/home/.matlab/R2012b/mexopts.sh
******************************************* *******************************
警告:MATLAB C和Fortran API已更改为支持MATLAB
变量具有超过2 ^ 32-1个元素。在不久的将来
你将需要更新你的代码,以利用新的
API。您可以在以下位置找到更多信息:
http://www.mathworks.com/help/techdoc/matlab_external/bsflnue-1.html
使用-largeArrayDims选项构建启用新的API。
********************************************** ****************************
然后我编辑文件 /user/wok/home/.matlab/R2012b/mexopts.sh
,我按照README中的说明:/ p>
CXX ='g ++'
CXXFLAGS =' - ansi -D_GNU_SOURCE'
CXXFLAGS =$ CXXFLAGS -D_FILE_OFFSET_BITS = 64
CXXFLAGS =$ CXXFLAGS -fPIC -pthread
CXXLIBS =$ RPATH $ MLIBS -lm
CXXOPTIMFLAGS =' - O -DNDEBUG '
CXXDEBUGFLAGS =' - g'
CXX ='g ++'
CXXFLAGS =' - std = c ++ 11 -fopenmp -march = native -D_GNU_SOURCE'
CXXLIBS =$ RPATH $ MLIBS -lm -lgomp
CXXOPTIMFLAGS =' - Ofast -DNDEBUG'
CXXDEBUGFLAGS =' - g'
在此步骤后,我打开 matlab
并编辑 compile_linux.m
以将路径添加到Boost:
%你需要安装boost 1.49或更高版本(在
%的软件包中安装libboost-dev)
%
%在Ubuntu 13.10,Matlab R2012b,gcc 4.8.1
%TODO:这假设boost标头在/ usr / include中可用,在
%大多数系统上。如果你手动安装boost,请添加-I / path / to / boost
mex -v -O ...
-output mex_voc_optimize -I ../ src ...
-I /user/wok/home/Softwares/Boost_1.54/include/boost ...
mex_voc_optimize.cpp matlab_helpers.cpp ...
-largeArrayDims
编辑:在Praetorian的第一条评论之后,我添加了 -v
标志。 >
最后,我运行 compile_linux
,只得到一长串错误,我已经缩短和粘贴如下:
>> compile_linux
- > mexopts.sh源自目录(DIR = $ PREF_DIR)
FILE = /user/wok/home/.matlab/R2012b/mexopts.sh
------------- -------------------------------------------------- -
- > matlab = / opt / matlab2012b
- > CC = gcc
- > CC标志:
CFLAGS = -ansi -D_GNU_SOURCE -fexceptions -fPIC -fno-omit-frame-pointer -pthread
CDEBUGFLAGS = -g
COPTIMFLAGS = -O -DNDEBUG
CLIBS = -Wl,-rpath-link,/ opt / matlab2012b / bin / glnxa64 -L / opt / matlab2012b / bin / glnxa64 -lmx -lmex -lmat -lm -lstdc ++
arguments =
- > CXX = g ++
- > CXX标志:
CXXFLAGS = -ansi -D_GNU_SOURCE -fPIC -fno-omit-frame-pointer -pthread
CXXDEBUGFLAGS = -g
CXXOPTIMFLAGS = -O -DNDEBUG
CXXLIBS = - wl,-rpath-link,/ opt / matlab2012b / bin / glnxa64 -L / opt / matlab2012b / bin / glnxa64 -lmx -lmex -lmat -lm
arguments =
- > FC = gfortran
- > FC标志:
FFLAGS = -fexceptions -fbackslash -fPIC -fno-omit-frame-pointer
FDEBUGFLAGS = -g
FOPTIMFLAGS = -O
FLIBS = -Wl,-rpath -link,/ opt / matlab2012b / bin / glnxa64 -L / opt / matlab2012b / bin / glnxa64 -lmx -lmex -lmat -lm
arguments =
- > LD = g ++
- >链接标志:
LDFLAGS = -pthread -shared -Wl, - version-script / opt / matlab2012b / extern / lib / glnxa64 / mexFunction.map -Wl, - no-undefined
LDDEBUGFLAGS = -g
LDOPTIMFLAGS = -O
LDEXTENSION = .mexa64
arguments =
- > LDCXX =
- >链接标志:
LDCXXFLAGS =
LDCXXDEBUGFLAGS =
LDCXXOPTIMFLAGS =
LDCXXEXTENSION =
arguments =
------------- -------------------------------------------------- -
警告:您使用的是gcc版本4.8.2。 MEX当前支持的版本
为4.4.6。
有关当前支持的编译器列表,请参阅:
http://www.mathworks.com/support/compilers/current_release/
- > g ++ -c -I ../ src -I / user / wok / home / Softwares / Boost_1.54 / include / boost -I / opt / matlab2012b / extern / include -I / opt / matlab2012b / simulink / include -DMATLAB_MEX_FILE - ansi -D_GNU_SOURCE -fPIC -fno-omit-frame-pointer -pthread -O -DNDEBUGmex_voc_optimize.cpp
在包含在/usr/include/c++/4.8.2/cstdint:35中的文件中:0,
from mex_voc_optimize.cpp:8:
/usr/include/c++/4.8.2/bits/c++0x_warning.h:32:2:error:#error此文件需要编译器以及ISO C ++ 2011标准的库支持。此支持目前是实验性的,必须使用-std = c ++ 11或-std = gnu ++ 11编译器选项来启用。
#error此文件需要编译器和库支持\
^
从../src/lfp.h:18:0,
中包含的文件/src/voc.h:15,
从mex_voc_optimize.cpp:19:
../src/timing.h:15:2:错误:'chrono'在命名空间'std'不名a type
std :: chrono :: time_point< std :: chrono :: high_resolution_clock>开始时间;
^
../src/timing.h:在成员函数'double timer :: elapsed_this_period()const':
../src/timing.h:20:8:error :'std :: chrono'尚未声明
std :: chrono :: time_point< std :: chrono :: high_resolution_clock> end_time
^
../src/timing.h:20:32:error:'std :: chrono'尚未声明
std :: chrono :: time_point< std :: chrono :: high_resolution_clock> end_time
直到
../ src / voc.h:418:31:note:'P'在这里声明,稍后在翻译单元
alpha [k_best] + = P(j,k_best)
^
../src/voc.h:在实例化'std :: pair< double,double> virtual :: prediction_function< TFloat> :: compute_function< TFloat> :: compute_objective_c(int)const [with TFloat = double]':
../src/voc.h:328:56: :: compute_objective()const [with TFloat = double]'
mex_voc_optimize.cpp:250:39:从这里需要
../src/voc.h:315:17:error:'P'未在此范围中声明
A_c + = P(i,c);
^
../src/voc.h:318:17:错误:'P'未在此范围内声明
D_c + = P(i,c);
^
mex:compile of'mex_voc_optimize.cpp失败。
使用mex时出错(第206行)
无法成功完成。
compile_linux中的错误(行9)
mex -O ...
>>
我已经尝试编辑代码以删除对 chrono
(用于定时器),然后用映射
替换 unordered_map
等, 。我想能够在 matlab /
中编译mex文件,就像我可以在 src /
感谢 Praetorian 的评论,我可以指出问题。我在 mexopts.sh
中编辑了错误的行。事实上, CXXFLAGS
出现在3个不同的地方,我已经编辑了标志在第一个地方,但它是必要的,足够的编辑它在第二个地方
查看例如:
code> $ grep -n CXXLIBS /user/wok/home/.matlab/R2012b/mexopts.sh
74:CXXLIBS =$ RPATH $ MLIBS -lm
129:CXXLIBS = $ RPATH $ MLIBS -lm
199:CXXLIBS =$ MLIBS -lstdc ++
所以我编辑了第129行的行。
CXX ='g ++'
CXXFLAGS =' - ansi -D_GNU_SOURCE'
CXXFLAGS =$ CXXFLAGS -fPIC -fno-omit-frame-pointer -pthread
CXXLIBS =$ RPATH $ MLIBS -lm
b CXXOPTIMFLAGS =' - O -DNDEBUG'
CXXDEBUGFLAGS =' - g'
/ p>
CXX ='g ++'
CXXFLAGS =' - std = c ++ 11 -fopenmp -march = native -D_GNU_SOURCE '
CXXFLAGS =$ CXXFLAGS -fPIC -fno-omit-frame-pointer -pthread
CXXLIBS =$ RPATH $ MLIBS -lm -lgomp
CXXOPTIMFLAGS =' - Ofast -DNDEBUG '
CXXDEBUGFLAGS =' - g'
I have downloaded the following code:
CVPR 2014 intersection over union optimization code
cvpr2014-iou-code-1.0.zip
http://research.microsoft.com/en-us/downloads/e164fe21-ef2b-4e34-98c1-4868968abb06/
What I get is an archive with two folders, one called src/
and another one called matlab/
. I am running Linux Fedora 19 and I can compile the code in src/
just fine. The problem arises when I try to compile the code in matlab/
.
The README says:
Matlab compilation ------------------ From within Matlab, configure the compiler using "mex -setup", then adjust the paths in matlab\compile_windows.m or matlab/compile_linux.m scripts and run either "compile_linux.m" or "compile_windows.m" in the "matlab" directory.
More specifically for Linux:
Linux ===== From within Matlab run "mex -setup", then edit the mexopts.sh file and modify the settings CXXFLAGS='-std=c++11 -fopenmp -march=native -D_GNU_SOURCE' CXXLIBS="$RPATH $MLIBS -lm -lgomp" CXXOPTIMFLAGS='-Ofast -DNDEBUG' This will enable OpenMP and C++11 and should work from G++ 4.7 onwards. From the matlab/ directory, call compile_linux to build all mex files.
So I open matlab
and run mex -setup
. I answer yes
to the question:
>> mex -setup
Options files control which compiler to use, the compiler and link command
options, and the runtime libraries to link against.
Using the 'mex -setup' command selects an options file that is
placed in /user/wok/home/.matlab/R2012b and used by default for 'mex'. An options
file in the current working directory or specified on the command line
overrides the default options file in /user/wok/home/.matlab/R2012b.
To override the default options file, use the 'mex -f' command
(see 'mex -help' for more information).
The options files available for mex are:
1: /opt/matlab2012b/bin/mexopts.sh :
Template Options file for building gcc MEX-files
0: Exit with no changes
Enter the number of the compiler (0-1):
1
Overwrite /user/wok/home/.matlab/R2012b/mexopts.sh ([y]/n)?
y
/opt/matlab2012b/bin/mexopts.sh is being copied to
/user/wok/home/.matlab/R2012b/mexopts.sh
**************************************************************************
Warning: The MATLAB C and Fortran API has changed to support MATLAB
variables with more than 2^32-1 elements. In the near future
you will be required to update your code to utilize the new
API. You can find more information about this at:
http://www.mathworks.com/help/techdoc/matlab_external/bsflnue-1.html
Building with the -largeArrayDims option enables the new API.
**************************************************************************
Then I edit the file /user/wok/home/.matlab/R2012b/mexopts.sh
and I follow the instructions from the README:
CXX='g++' CXXFLAGS='-ansi -D_GNU_SOURCE' CXXFLAGS="$CXXFLAGS -D_FILE_OFFSET_BITS=64" CXXFLAGS="$CXXFLAGS -fPIC -pthread" CXXLIBS="$RPATH $MLIBS -lm" CXXOPTIMFLAGS='-O -DNDEBUG' CXXDEBUGFLAGS='-g'
becomes:
CXX='g++' CXXFLAGS='-std=c++11 -fopenmp -march=native -D_GNU_SOURCE' CXXLIBS="$RPATH $MLIBS -lm -lgomp" CXXOPTIMFLAGS='-Ofast -DNDEBUG' CXXDEBUGFLAGS='-g'
After this step, I open matlab
and edit compile_linux.m
to add the path to Boost:
% You need to have boost 1.49 or higher installed (package libboost-dev on
% most distributions)
%
% Tested on Ubuntu 13.10, Matlab R2012b, gcc 4.8.1
% TODO: this assumes that boost headers are available in /usr/include, as on
% most systems. If you manually installed boost, please add -I/path/to/boost
mex -v -O ...
-output mex_voc_optimize -I../src ...
-I/user/wok/home/Softwares/Boost_1.54/include/boost ...
mex_voc_optimize.cpp matlab_helpers.cpp ...
-largeArrayDims
Edit: Following the first comment from Praetorian, I have added the -v
flag.
Finally I run compile_linux
, only to get a long list of errors, which I have shortened and pasted below:
>> compile_linux
-> mexopts.sh sourced from directory (DIR = $PREF_DIR)
FILE = /user/wok/home/.matlab/R2012b/mexopts.sh
----------------------------------------------------------------
-> MATLAB = /opt/matlab2012b
-> CC = gcc
-> CC flags:
CFLAGS = -ansi -D_GNU_SOURCE -fexceptions -fPIC -fno-omit-frame-pointer -pthread
CDEBUGFLAGS = -g
COPTIMFLAGS = -O -DNDEBUG
CLIBS = -Wl,-rpath-link,/opt/matlab2012b/bin/glnxa64 -L/opt/matlab2012b/bin/glnxa64 -lmx -lmex -lmat -lm -lstdc++
arguments =
-> CXX = g++
-> CXX flags:
CXXFLAGS = -ansi -D_GNU_SOURCE -fPIC -fno-omit-frame-pointer -pthread
CXXDEBUGFLAGS = -g
CXXOPTIMFLAGS = -O -DNDEBUG
CXXLIBS = -Wl,-rpath-link,/opt/matlab2012b/bin/glnxa64 -L/opt/matlab2012b/bin/glnxa64 -lmx -lmex -lmat -lm
arguments =
-> FC = gfortran
-> FC flags:
FFLAGS = -fexceptions -fbackslash -fPIC -fno-omit-frame-pointer
FDEBUGFLAGS = -g
FOPTIMFLAGS = -O
FLIBS = -Wl,-rpath-link,/opt/matlab2012b/bin/glnxa64 -L/opt/matlab2012b/bin/glnxa64 -lmx -lmex -lmat -lm
arguments =
-> LD = g++
-> Link flags:
LDFLAGS = -pthread -shared -Wl,--version-script,/opt/matlab2012b/extern/lib/glnxa64/mexFunction.map -Wl,--no-undefined
LDDEBUGFLAGS = -g
LDOPTIMFLAGS = -O
LDEXTENSION = .mexa64
arguments =
-> LDCXX =
-> Link flags:
LDCXXFLAGS =
LDCXXDEBUGFLAGS =
LDCXXOPTIMFLAGS =
LDCXXEXTENSION =
arguments =
----------------------------------------------------------------
Warning: You are using gcc version "4.8.2". The version
currently supported with MEX is "4.4.6".
For a list of currently supported compilers see:
http://www.mathworks.com/support/compilers/current_release/
-> g++ -c -I../src -I/user/wok/home/Softwares/Boost_1.54/include/boost -I/opt/matlab2012b/extern/include -I/opt/matlab2012b/simulink/include -DMATLAB_MEX_FILE -ansi -D_GNU_SOURCE -fPIC -fno-omit-frame-pointer -pthread -O -DNDEBUG "mex_voc_optimize.cpp"
In file included from /usr/include/c++/4.8.2/cstdint:35:0,
from mex_voc_optimize.cpp:8:
/usr/include/c++/4.8.2/bits/c++0x_warning.h:32:2: error: #error This file requires compiler and library support for the ISO C++ 2011 standard. This support is currently experimental, and must be enabled with the -std=c++11 or -std=gnu++11 compiler options.
#error This file requires compiler and library support for the \
^
In file included from ../src/lfp.h:18:0,
from ../src/voc.h:15,
from mex_voc_optimize.cpp:19:
../src/timing.h:15:2: error: ‘chrono’ in namespace ‘std’ does not name a type
std::chrono::time_point<std::chrono::high_resolution_clock> start_time;
^
../src/timing.h: In member function ‘double timer::elapsed_this_period() const’:
../src/timing.h:20:8: error: ‘std::chrono’ has not been declared
std::chrono::time_point<std::chrono::high_resolution_clock> end_time
^
../src/timing.h:20:32: error: ‘std::chrono’ has not been declared
std::chrono::time_point<std::chrono::high_resolution_clock> end_time
until
../src/voc.h:418:31: note: ‘P’ declared here, later in the translation unit
alpha[k_best] += P(j,k_best);
^
../src/voc.h: In instantiation of ‘std::pair<double, double> voc::prediction_function<TFloat>::compute_objective_c(int) const [with TFloat = double]’:
../src/voc.h:328:56: required from ‘double voc::prediction_function<TFloat>::compute_objective() const [with TFloat = double]’
mex_voc_optimize.cpp:250:39: required from here
../src/voc.h:315:17: error: ‘P’ was not declared in this scope
A_c += P(i,c);
^
../src/voc.h:318:17: error: ‘P’ was not declared in this scope
D_c += P(i,c);
^
mex: compile of ' "mex_voc_optimize.cpp"' failed.
Error using mex (line 206)
Unable to complete successfully.
Error in compile_linux (line 9)
mex -O ...
>>
I have tried editing the code to remove every call to chrono
(for timers), then to replace unordered_map
with map
, etc., but it is a pain. I would like to be able to compile the mex file in matlab/
just like I can compile the c++ code in src/
Thanks to Praetorian's comment, I could pinpoint the problem. I had edited the wrong lines in mexopts.sh
. Indeed, the CXXFLAGS
appears at 3 different places, I had edited the flags at the first place, but it was necessary and sufficient to edit it at the second place (the first place was a decoy).
See for instance:
$ grep -n CXXLIBS /user/wok/home/.matlab/R2012b/mexopts.sh 74: CXXLIBS="$RPATH $MLIBS -lm" 129: CXXLIBS="$RPATH $MLIBS -lm" 199: CXXLIBS="$MLIBS -lstdc++"
So I edited the lines around line 129. The following lines
CXX='g++'
CXXFLAGS='-ansi -D_GNU_SOURCE'
CXXFLAGS="$CXXFLAGS -fPIC -fno-omit-frame-pointer -pthread"
CXXLIBS="$RPATH $MLIBS -lm"
CXXOPTIMFLAGS='-O -DNDEBUG'
CXXDEBUGFLAGS='-g'
become
CXX='g++'
CXXFLAGS='-std=c++11 -fopenmp -march=native -D_GNU_SOURCE'
CXXFLAGS="$CXXFLAGS -fPIC -fno-omit-frame-pointer -pthread"
CXXLIBS="$RPATH $MLIBS -lm -lgomp"
CXXOPTIMFLAGS='-Ofast -DNDEBUG'
CXXDEBUGFLAGS='-g'
这篇关于在Matlab mex文件中编译c ++ 11代码时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!