Last active
March 5, 2023 05:40
-
-
Save scivision/f592e9560bf9cca4fe7f884676cba97d to your computer and use it in GitHub Desktop.
CMake matlab_add_mex() example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
cmake_minimum_required(VERSION 3.19) | |
project(mexDemo LANGUAGES C) | |
enable_testing() | |
find_package(Matlab REQUIRED COMPONENTS MAIN_PROGRAM) | |
matlab_get_release_name_from_version(${Matlab_VERSION_STRING} release) | |
message(STATUS "Matlab release: ${release}") | |
find_file(matmul_src | |
NAMES matrixMultiply.c | |
NO_DEFAULT_PATH | |
PATHS ${Matlab_ROOT_DIR}/extern/examples/refbook | |
REQUIRED | |
) | |
find_library(Matlab_MEX_BLAS | |
NAMES libmwblas mwblas | |
NO_DEFAULT_PATH | |
PATHS ${Matlab_EXTERN_LIBRARY_DIR} ${Matlab_BINARIES_DIR} | |
PATH_SUFFIXES ${_matlab_libdir_suffix} | |
REQUIRED | |
) | |
message(STATUS "Matlab BLAS library: ${Matlab_MEX_BLAS}") | |
matlab_add_mex(NAME matrixMultiply SHARED SRC ${matmul_src} LINK_TO ${Matlab_MEX_BLAS}) | |
add_test(NAME C_MEX_BLAS | |
COMMAND ${Matlab_MAIN_PROGRAM} -batch | |
"addpath('$<TARGET_FILE_DIR:matrixMultiply>'), r=runtests('${CMAKE_CURRENT_SOURCE_DIR}', Name='TestMex/test_blas'); assertSuccess(r)" | |
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
classdef TestMex < matlab.unittest.TestCase | |
methods(Test) | |
function test_blas(tc) | |
tc.assumeEqual(exist('matrixMultiply'), 3, 'C matrixMultiply MEX not compiled') %#ok<*EXIST> | |
a = eye(3); | |
b = magic(3); | |
x = matrixMultiply(a, b); | |
tc.verifyEqual(x, a*b) | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment