CMake compiling the Mex file in MATLAB under Ubuntu system

Source: Internet
Author: User

The CMake compilation project has unique advantages, especially for complex projects. When using MATLAB to compile the Mex file directly, it is necessary to enumerate all the files when the file data is more engineering, and to sort by the dependencies, these are the disadvantages of using MATALB to compile Mex directly.

The following is a simple example of an addition function that shows how to generate a Mex file using CMake compilation. How to set up the system dynamic library so that MATLAB can call the MEX function normally.

First describes how to generate a Mex file.

This example first builds the add.so dynamic file library with Add.h, add.cpp file compilation, and then uses the Mian.cpp file to invoke the Add.so library file for the addition operation. The structure of this project is as follows:


Where Findmatlab.cmake file is to find the native MATLAB header files, library files and other related path information script, Matalbdef.def file is the specified function interface mode using MATLAB Mex library file form, Bin folder storage Project Mex file, The building folder holds Camke project files. Matalbdef.def and Findmatlab.cmake files can be downloaded in Www.mathworks.com/matlabcentral/fileexchange/45522-mex-cmake.

The CMakeLists.txt file under the project root directory is as follows:

Cmake_minimum_required (VERSION 2.8) Project (ADD) set (Cmake_install_prefix ${cmake_source_dir}) Set (Cmake_module_ PATH ${cmake_source_dir}/cmake) # Add Findmatlab moduleadd_definitions (/dmatlab_mex_file) #define MATLAB macrosadd_ Definitions (/dmx_compat_32) find_package (matlab REQUIRED) IF (matlab_found)    message (STATUS "matlab FOUND, matlab MEX would be compiled. ")    Add_subdirectory (SRC) ELSE (matlab_found)    MESSAGE ("MATLAB not found...nothing would be built.") ENDIF (Matlab_found)

The file implements the configuration of the MATALB-related path information, and the Cmake_install_prefix variable specifies where the Mex file generated after the Make INSTALL command is transferred to, given a root directory, after compilation is completed. The exact location also depends on how the CMakeLists.txt file is written, as in this example, the following command is given:

Install (TARGETS ${cpp_file} DESTINATION./bin)
The final MEX file is transferred under the ${cmake_install_prefix}/bin directory.

src folder under the CMakeLists.txt file, the file gives the library file source files and the main file location, the two order can not change, Mian is dependent on the library file, to compile the library file, after compiling Mian file.

Add_subdirectory (ADD) add_subdirectory (main)

The CMakeLists.txt file under the Add folder is as follows, because the Ubuntu system under the MATLAB only dynamic file library, so here can only generate dynamic library, so that with the MATLAB Mex function to use.

Include_directories (${project_source_dir}/include) aux_source_directory (. Src_files) Set (Library_output_path ${project_source_dir}/lib) add_library (add_lib SHARED ${src_files}) set_target_ Properties (Add_lib Properties Output_name "Add")

The CMakeLists.txt file under the main folder is the following, which implements the ability to encapsulate the dynamic library add.so into a MEX file. The total include_directories instruction is preceded by the Add_library directive in order to specify the location of the Mex-dependent library file beforehand. The final install copies the generated MEX file to the specified location.

# Compile Mex Fileset (cpp_file Main) # set up matlab librariesset (cpp_file_name ${cpp_ File}.cpp) include_directories (${matlab_include_dir} ${project_source_dir}/include) Link_directories (${PROJECT_ Source_dir}/lib) add_library (${cpp_file} SHARED ${cpp_file_name} ${project_source_dir}/matlabdef.def) target_link_ Libraries (${cpp_file} libadd.so ${matlab_libraries}) # 32-bit or 64-bit mexif (WIN32) if (cmake_cl_64) Set_target_prop Erties (${cpp_file} properties SUFFIX. mexw64) Else (cmake_cl_64) set_target_properties (${cpp_file} Properties SUFFIX . Mexw32) endif (cmake_cl_64) Else (WIN32) if (cmake_sizeof_void_p MATCHES "8") Set_target_properties (${cpp_file} prope Rties SUFFIX mexa64 PREFIX "") Else (cmake_sizeof_void_p MATCHES "8") Set_target_properties (${cpp_file} Properties S Uffix. MEXGLX PREFIX "") endif (cmake_sizeof_void_p MATCHES "8") endif (WIN32) # Install To/bin by DefaultInstall (TARGETS $ {Cpp_file} DESTINATION./bin) 

Include folder head file Add.h content is

Float Add (float p1,float p2);
The add.cpp content of the source file under the Add folder is

Float Add (float p1,float p2) {  return p1+p2;}
Mian folder under Main.cpp content is

#include "mex.h" #include "add.h" void mexfunction (int nlhs, Mxarray *plhs[],int nrhs, const Mxarray *prhs[]) {   float A;   A=add (3,5);   mexprintf ("Hello world\n");   mexprintf ("%g\n", a); }

CMake before compiling, you need to include the MATLAB_ROOT environment variable in the system variable, which will be used in the Findmatlab.cmake file, so that the script file can find the MATLAB header and library file information, for native join Matlab_ The root environment variable statement is as follows:

Export matlab_root=/usr/local/matlab/r2014b
The results of the compilation are as follows:


Make results are as follows, the project first compiled the dynamic link library file Libadd.so,ubuntu system, the library file will be the default prefix lib, the prefix can not be removed, or the system will be found not dynamic link situation.


The make install results are as follows:


The Mex file is then generated.

Configure the system dynamic Library, Matalb call the generated main.mexa64 function

The overall step is to save our generated dynamic library file and the corresponding header file somewhere in the system and save the path information to the system as a system variable. Here are the detailed steps

1). Create this folder /usr/local/mylib

In this example, the/usr/localThe Mylib folder is established under the

sudo mkdir/usr/local/mylib

2) Create a mylib.conf file under the /etc/ld.so.conf.d folder and make it record The directory of the Dynamic library.

sudo gedit/etc/ld.so.conf.d/mylib.conf
Add a statement to the file

/usr/local/mylib

3) Create the mypkgconfig folder under the /usr/local/mylib folder, which will save the specific dynamic library information, such as header file, dynamic library file

sudo mkdir/usr/local/mylib/mypkgconfig
The contents of the file are as follows

# Package information for Pkg-config prefix=/usr/local Exec_prefix=${prefix} libdir= includedir=${prefix}/myinclude Name:mylib Description:lib created by myselfversion:1.0 Libs:  ${exec_prefix}/mylib/lib<span style= " Font-family:liberation Serif, Serif; " >add</span>.so-lrt-lpthread-lm-ldlcflags:-i${includedir}


4) Create a myinclude folder under the /usr/local folder to store the dynamic library header files

sudo mkdir/usr/local/myinclude

5) Copy the header file of the created library to the /usr/local/myinclude directory, the following code is the default current directory in the directory where the files are block.h,graph.h

sudo cp <span style= "Font-family:liberation Serif, Serif;" >add</span>.h/usr/local/myinclude

6) Move the established dynamic library file libadd.so to the /usr/local/mylib folder

sudo cp lib<span style= "Font-family:liberation Serif, Serif;" >add</span>.so/usr/local/mylib

7) Refresh Buffers

sudo ldconfig

8) perform configuration at boot-up

sudo gedit/etc/bash.bashrc

Add the following two lines to the end of the file and save it on the last line of the file.

My_pkg_config_path= $MY _pkg_config_path:/usr/local/mylib/mypkgconfig

Exportmy_pkg_config_path


Finally, run the results in MATLAB as shown in.


CMake compiling the Mex file in MATLAB under Ubuntu system

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.