2014-09-03 77 views
3

我已經下載the following code錯誤編譯C++時在Matlab的MEX 11代碼文件

CVPR 2014交集過工會優化代碼

cvpr2014-iou-code-1.0.zip

http://research.microsoft.com/en-us/downloads/e164fe21-ef2b-4e34-98c1-4868968abb06/

我得到的是兩個文件夾歸檔,一個叫src/和另一個稱爲matlab/。我運行的是Linux Fedora 19,我可以在src/中編譯代碼。當我嘗試在matlab/中編譯代碼時出現問題。

自述說:

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. 

更特別版:

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. 

於是我打開matlab和運行mex -setup。我回答yes的問題:

>> 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. 
************************************************************************** 

然後我編輯的文件/user/wok/home/.matlab/R2012b/mexopts.sh,我遵循的自述文件的說明:

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添加的路徑來提高:

% 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 

編輯:繼從禁衛軍第一條評論,我已經添加了-v標誌。

最後我跑compile_linux,只得到錯誤的一個長長的清單,我已經縮短,粘貼下面:

>> 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 

直到

../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 ... 

>> 

我曾嘗試編輯代碼以刪除每致電chrono(定時器),然後用map等代替unordered_map,但這是一種痛苦。我想能夠編譯MEX文件中matlab/就像我在src/

+1

添加'-v'到mex命令行來調用它詳細模式,你應該能夠看到mex傳遞給gcc的文件和選項。 – Praetorian 2014-09-03 16:16:18

+0

感謝您的建議,我編輯了這個問題。由於某些原因,雖然我編輯了正確的文件,但CXX標誌看起來不對。 – Wok 2014-09-03 16:20:25

+0

看來CXX標誌在'mexopts.sh'文件中多次出現,並且我沒有編輯過所有這些標誌。 – Wok 2014-09-03 16:23:09

回答

5

由於編譯C++代碼Praetorian的評論,我可以查明問題。我在mexopts.sh中編輯了錯誤的行。實際上,CXXFLAGS出現在3個不同的地方,我在第一個地方編輯過標誌,但是在第二個地方編輯它是必要和足夠的(第一個地方是誘餌)。

見例如:

$ 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" 
     CXXOPTIMFLAGS='-O -DNDEBUG' 
     CXXDEBUGFLAGS='-g' 

成爲

 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'