Use the CMake project in Qt Creator to debug the mex file, cmakemex
You can find many articles on the Internet that use Visual Studio to compile and compile the mex file and debug it with MATLAB. However, this is only limited to the Win platform. Many source code files on the Internet are. mexa64 files. how are their authors debugging them? Here I will introduce my methods.
Use the Qt IDE To build a CMake project to implement one-time Encoding and reuse on multiple platforms. Moreover, CMake can automatically set the Include and Lib directories of Matlab to save a lot of trouble. Furthermore, ide gui makes debugging more intuitive and easier than debugging mex directly using gdb.
I. Implementation platform:
Operating System: Linux Mint 17.3, Ubuntu 14.04, Linux Mint 18, and Ubuntu 16.04.
Programming tools: CMake 3.7, built-in gcc, built-in gdb, Matlab 2016b, Qt 5.7
2. Create a CMake project:
Create a CMake project in Qt. For the structure, see here.
Http://cn.mathworks.com/matlabcentral/fileexchange/45522-mex-cmake
Here is a Chinese description of it.
Http://blog.csdn.net/kxuehen/article/details/45851651
Notes:
1) the content of the file is
EXPORTS mexFunction
2) The FindMatlab. cmake file is used to find the Include and Lib paths of the local Matlab. CMake adds the CMAKE_MODULE_PATH variable to implement this search function. Make sure to set the environment variable maid as the Matlab root directory. For example ~ /Add under bashrc
export MATLAB_ROOT=/usr/local/MATLAB/R2016b
3) You can specify the location of the compiled mex file (that is, the generated location of the mex file after make is not the installation location behind make install. you can set the LIBRARY_OUTPUT_PATH variable in cmakelist.txt for the file to be generated. For example, I add
set(LIBRARY_OUTPUT_PATH ${PROJECT_BINARY_DIR}/lib)
You can generate the mex file to the lib folder of the build directory (usually the build directory.
3. Compile the CMake project:
To compile the CMake project with Qt Creator, you must install CMake 3.3.2 or later.
Qt Creator needs to introduce bash environment variables. For example, in the above mentioned variables, the method is to change the Exec in the Qt Creator shortcut (that is, the. desktop file, generally ~ /. Local/share/applications/DigiaQtOpenSource-qtcreator.desktop or/usr/share/applications/DigiaQtOpenSource-qtcreator.desktop ). For example, if I add bash-I-c
Exec=bash -i -c /opt/Qt5.7.0/Tools/QtCreator/bin/qtcreator
Also, set the CMake generator in Qt Creator to CodeBlocks-Unix Makefiles (this setting is used by default), as shown in
If no source code is displayed in Edit, do not forget to open the Project on the left; then choose Build> Run CMake from the menu, or right-click the Project name, and right-click Run CMake from the menu,
For the CMake project, for detailed Qt Creator settings, refer
Http://doc.qt.io/qtcreator/creator-project-cmake.html
Iv. Joint debugging with Matlab:
After compilation, you can enable Matlab to enable Qt Creator to use gdb for joint debugging. Set the breakpoint in Qt Creator, and then choose Debug> Start Debuging> Attach to Running Application...
However, I encountered two errors during joint debugging:
1) The first possible error, as shown in
You can use the following command to temporarily solve the problem,
echo 0 | sudo tee /proc/sys/kernel/yama/ptrace_scop
To completely solve this problem, modify the/etc/sysctl. d/10-ptrace.conf file
kernel.yama.ptrace_scope = 1
Change
kernel.yama.ptrace_scope = 0
2) The second possible error, as shown in
Here, solib-search-path is gdb's dynamic link library file (or shared library file, that is, in Linux. in the so file or Win. dll file) directory, of course, gdb will also search in the current directory where it is working. So this error is actually because gdb cannot find our mex file, so I will directly set the Working directory during debugging to the directory where mex is located, so that gdb can directly find the mex file, setting Method
Note: Here is a tip. I like to store the Matlab code and C ++ separately. To call the newly generated mex file in Matlab, you can use the addpath command in Matlab, include the folder where the mex file is located, as shown in figure
addpath('~/mex/build/lib');