[CMake] using matlab mex compiler with cmake on windows

Eike Kroemer eike-michael.kroemer at atlas-elektronik.com
Tue Oct 4 00:30:28 EDT 2011


Hi Jaka, David,

below I tried to assemble a minimal excerpt from CMake-Files defining a
project that I successfully compile under Windows XP, 32bit, MSVC 10
Express, using the CMake 2.8.5 GUI.

There wasn't anything special about setting up MSVC or CMake, I think I
remember dimly that one had to choose the compiler to use during CMake
install.

Nowhere I have to explicitely define CC or CXX; variables set in the
CMake-GUI are:

'Where ist the source code':   L:/CMAKE/SRC/SONARPERF/.BUILD/MEX
'Where to build the binaries': L:/CMAKE/VCPROJ/SONARPERF/MEX
CMAKE_INSTALL_PREFIX:          <like 'Where to build the libraries'>
MATLAB:                        <PATH:C:/Programme/MATLAB/R2011a>
MEX_EXT:                       mexw32 or mexw64 depending on platform

Actually .BUILD/MEX is the path of the project's main CMakeLists.txt,
not the source, see below.
MATLAB and MEX_EXT are used within the CMakeLists.txts

The project itself is developed under Linux, the only crucial thing
about adaption to Windows was the declspec issue and the need for
explicit specification with target_link_libraries.
The very same source code (barring the mex-specific code, of course) can
be compiled into static executables using another main CMakeLists.txt
not shown below.

Transfer from Linux to Windows only involves copying source and
CMakeLists.txt files, pressing Configure and Generate in the Windows
Cmake GUI and starting/reloading the thus generated project in the MSVC Gui.

The project compiles a series of MEX-Files (100+) which call functions
(500+) from a set of libraries that have been linked dynamically, before
(by the same project). No need for any MSVC button besides 'Build' (and
choosing between Debug/Release).

Some of the flags below are gcc-specific and will be ignored by MSVC.
Some user-defined variables (e.g. _PLATFORM_) are for use in the source
code itself.

Good luck,
   Eike


.BUILD/MEX/CMakeLists.txt: ------------------------>
cmake_minimum_required(VERSION 2.6)

project(mex)

#### define compile switches
set(MATLAB_CFLAGS "-fexceptions -fPIC -fno-omit-frame-pointer -pthread")

set(CMAKE_C_FLAGS_DEBUG    "-g -Wall")
set(CMAKE_CXX_FLAGS_DEBUG  "-g -Wall")
set(CMAKE_C_FLAGS_RELEASE   "-O3 -funroll-all-loops")
set(CMAKE_CXX_FLAGS_RELEASE "-O3 -funroll-all-loops")

#### platform specific defines
if(UNIX)
  set(PLATFORM "_UNIX_")
  set(MEXDEFFILE "")
  set(SYSLIB "m")
endif(UNIX)
if(WIN32)
  set(PLATFORM "_WIN32_")
  set(MEXDEFFILE ../../MATLAB/mexfunction.def)
  set(MEXLIB "libmx;libmex")
endif(WIN32)
if(APPLE)
  set(PLATFORM "_UNIX_")
  set(MEXDEFFILE "")
  set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -undefined
dynamic_lookup")
endif(APPLE)

#### common defines
set(CDEFS "-D${PLATFORM}")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${MATLAB_CFLAGS} ${CDEFS}")
set(CMAKE_C_FLAGS   "${CMAKE_C_FLAGS}   ${MATLAB_CFLAGS} ${CDEFS}")
set(CMAKE_SHARED_LIBRARY_LINK_C_FLAGS "")

#### define target-specific flags
set(BUILD_SHARED_LIBS 1)
if(WIN32)
  add_definitions(-D_MAKEDLL_)
  link_directories("${MATLAB}/extern/lib/win32/microsoft")
  add_definitions(-D_CRT_SECURE_CPP_OVERLOAD_STANDARD_NAMES=1)
  message(STATUS "- MSVC: _CRT_SECURE_CPP_OVERLOAD_STANDARD_NAMES=1")
  add_definitions(-D_CRT_NONSTDC_NO_WARNINGS)
  message(STATUS "- MSVC: _CRT_NONSTDC_NO_WARNINGS")
endif(WIN32)

#### add subdirectories containing include files (C)
[...]
include_directories("../../SVP/INCLUDE")

#### add subdirectories containing include files (MEX)
[...]
include_directories("../../SVP/MEXINTERFACE/INCLUDE")
nclude_directories("${MATLAB}/extern/include")
include_directories("${MATLAB}/simulink/include")

#### add subdirectories containing code and library targets (C)
[...]
add_subdirectory(../../SVP SVP)

#### define set of libraries
set(LIBS svp)
[...]
list(APPEND LIBS "${SYSLIB}")

### add subdirectories containing code and library targets (MEX)
[...]
add_subdirectory(../../SVP/MEXINTERFACE SVP/MEXINTERFACE)
.BUILD/MEX/CMakeLists.txt: ------------------------<


SVP/CMakeLists.txt -------------------------------->
#### define library name and dependencies
set(LIBNAME "svp")
set(DEPLIBS "texttools;numrec;mathtools;errhdl")

#### define sources
set(SRC "svpglobal")
set(SRC "${SRC};read_usersvp")
[...]

#### finally define libraries
add_library(${LIBNAME} ${SRC})
target_link_libraries(${LIBNAME} ${DEPLIBS})

SVP/CMakeLists.txt --------------------------------<


SVP/MEXINTERFACE/CMakeLists.txt ------------------->
#### define library name and dependencies
set(LIBNAME "matlab_svp")
set(DEPLIBS "svp;texttools;numrec;mathtools;errhdl;${MEXLIB}")

#### shared library
set(SRCLIB "conv_usersvp.c")
set(SRCLIB "${SRCLIB};conv_svppar.c")
set(SRCLIB "${SRCLIB};conv_svp.c")
add_library(${LIBNAME} ${SRCLIB})
target_link_libraries(${LIBNAME} ${DEPLIBS})

#### mexfunctions
set(SRCMEX "m_read_usersvp")
set(SRCMEX "${SRCMEX};m_read_svppar")
[...]

add_definitions(-DMATLAB_MEX_FILE)
foreach(SRC ${SRCMEX})
  get_filename_component(MEXNAME ${SRC} NAME)
  add_library(${MEXNAME} SHARED ${SRC} ${MEXDEFFILE})
  target_link_libraries(${MEXNAME} ${LIBS})
  set_target_properties(${MEXNAME} PROPERTIES PREFIX "" SUFFIX
".${MEX_EXT}")
endforeach(SRC)
SVP/MEXINTERFACE/CMakeLists.txt -------------------<

mexfunction.def ----------------------------------->
EXPORTS
        mexFunction
mexfunction.def -----------------------------------<

SVP/INCLUDE/svp.h --------------------------------->
#ifdef __cplusplus
extern "C" {
#endif

[...]

#if defined (_MAKEDLL_)
#  ifdef svp_EXPORTS
#    define DLLEXPORT __declspec(dllexport)
#  else
#    define DLLEXPORT __declspec(dllimport)
#  endif
#else
#  define DLLEXPORT
#endif

DLLEXPORT usersvp_t read_usersvp(string_t filename);

[...]

#undef DLLEXPORT

#ifdef __cplusplus
}
#endif



SVP/INCLUDE/svp.h ---------------------------------<


More information about the CMake mailing list