Makefile: Converting C-code to mex code

8 views (last 30 days)
Dushyant
Dushyant on 13 Feb 2015
Commented: Geoff Hayes on 14 Feb 2015
I have a C-code which works fine using makefile. Now, I am trying to convert it to mex file so that I can run it from Matlab. Here also, I am u sing makefile approach . But, makefile for mex gives me error.
Organization of C-project:
tsnnls_test_DKU.c
Include_4_TSNNLS.c
Include_4_TSNNLS.h
Include_4_TSNNLS.h, .c files have function TestingLibraries() which call 3rd part libraries; while I tried to keep tsnnls_test_DKU.c very simple as:
*Original Code: tsnnls_test_DKU.c*
int TestingLibraries() ;
int main( int argc, char* argv[] )
{
int k = TestingLibraries() ;
return(1);
}
Now, the code was changed:
*Changed code: tsnnls_test_DKU.c:*
#include "mex.h"
#include <math.h>
int TestingLibraries() ;
void mexFunction (int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]) {
int k = TestingLibraries() ;
}
*Original make file (that works*
CXX = gcc
FLAGS =
CFLAGS = "-m64" ## "-m32"
INCLUDE_TSNNLS = -I/home/dkumar/libtsnnls-2.3.3 -I/home/dkumar/libtsnnls-2.3.3/tsnnls -I/home/dkumar/libtsnnls-2.3.3/tsnnls/taucs_basic
#################### ALL TSNNLS lib related ##########################
## this is where all object file (*.o) generated by tsnnls library makefile are located.
tsnnl_PATH = /home/dkumar/libtsnnls-2.3.3/tsnnls/
# Here is a simple Make Macro.
OBJS_tsnnls1 = tsnnls_test_DKU.o
OBJS_ADD = Include_4_TSNNLS.o
# Here is a Make Macro that uses the backslash to extend to multiple lines.
OBJS_tsnnls0 = libtsnnls_la-taucs_malloc.o libtsnnls_la-taucs_ccs_order.o \
libtsnnls_la-taucs_ccs_ops.o libtsnnls_la-taucs_vec_base.o \
libtsnnls_la-taucs_complex.o libtsnnls_la-colamd.o \
libtsnnls_la-amdbar.o libtsnnls_la-amdexa.o \
libtsnnls_la-amdtru.o libtsnnls_la-genmmd.o \
libtsnnls_la-taucs_timer.o libtsnnls_la-taucs_sn_llt.o \
libtsnnls_la-taucs_ccs_base.o libtsnnls_la-tlsqr.o \
libtsnnls_la-tsnnls.o libtsnnls_la-lsqr.o
## adding "$(OBJS_PATH)" to each word in "$(OBJS)"
# which in our case is basically to add the same folder in front of all "*.o" object files.
OBJS_TEMP = $(addprefix $(tsnnl_PATH), $(OBJS_tsnnls0))
# OBJS_LOC is in current working directory,
OBJS_tsnnlsALL = $(OBJS_TEMP) $(OBJS_tsnnls1)
# Libraries for tsnnls
STLIB_tsnnls = /usr/local/lib/taucs_full/lib/linux/libtaucs.a
LIBS_tsnnls = -largtable2 -llapack -lblas -lquadmath -lm
########################################################################
TARGET = tsnnls_test_DKU
REBUILDABLES = $(OBJS_tsnnls1) $(TARGET)
LIBS = $(LIBS_tsnnls) $(STLIB_tsnnls)
INCLUDE = $(INCLUDE_TSNNLS)
all : $(TARGET)
echo All done
clean :
rm -f $(REBUILDABLES)
echo Clean done
$(OBJS_ADD): Include_4_TSNNLS.c
gcc -c -o Include_4_TSNNLS.o Include_4_TSNNLS.c
# Final linking
$(TARGET) : $(OBJS_tsnnlsALL) $(OBJS_ADD) $(LIBS)
$(CXX) -g -o $@ $(INCLUDE) $(CFLAGS) $^
*Changed Makefile that does not run* I made changes at three places marked by string `##CHANGED PART STARTS ##`
###########CHANGED PART STARTS #################################
MEXSUFFIX = mex64
MATLABHOME = /usr/local/MATLAB/R2014b
MEX = mex
CXX = gcc
CFLAGS = -fPIC -ansi -pthread -DMX_COMPAT_32 \
-DMATLAB_MEX_FILE
MEXFLAGS = -cxx CC='$(CXX)' CXX='$(CXX)' LD='$(CXX)'
###########CHANGED PART ENDS #################################
INCLUDE_TSNNLS = -I/home/dkumar/libtsnnls-2.3.3 -I/home/dkumar/libtsnnls-2.3.3/tsnnls -I/home/dkumar/libtsnnls-2.3.3/tsnnls/taucs_basic
#################### ALL TSNNLS lib related ##########################
## this is where all object file (*.o) generated by tsnnls library makefile are located.
tsnnl_PATH = /home/dkumar/libtsnnls-2.3.3/tsnnls/
# Here is a simple Make Macro.
OBJS_tsnnls1 = tsnnls_test_DKU.o
OBJS_ADD = Include_4_TSNNLS.o
# Here is a Make Macro that uses the backslash to extend to multiple lines.
OBJS_tsnnls0 = libtsnnls_la-taucs_malloc.o libtsnnls_la-taucs_ccs_order.o \
libtsnnls_la-taucs_ccs_ops.o libtsnnls_la-taucs_vec_base.o \
libtsnnls_la-taucs_complex.o libtsnnls_la-colamd.o \
libtsnnls_la-amdbar.o libtsnnls_la-amdexa.o \
libtsnnls_la-amdtru.o libtsnnls_la-genmmd.o \
libtsnnls_la-taucs_timer.o libtsnnls_la-taucs_sn_llt.o \
libtsnnls_la-taucs_ccs_base.o libtsnnls_la-tlsqr.o \
libtsnnls_la-tsnnls.o libtsnnls_la-lsqr.o
## adding "$(OBJS_PATH)" to each word in "$(OBJS)"
# which in our case is basically to add the same folder in front of all "*.o" object files.
OBJS_TEMP = $(addprefix $(tsnnl_PATH), $(OBJS_tsnnls0))
# OBJS_LOC is in current working directory,
OBJS_tsnnlsALL = $(OBJS_TEMP) $(OBJS_tsnnls1)
# Libraries for tsnnls
STLIB_tsnnls = /usr/local/lib/taucs_full/lib/linux/libtaucs.a
LIBS_tsnnls = -largtable2 -llapack -lblas -lquadmath -lm
########################################################################
###########CHANGED PART STARTS #################################
TARGET = tsnnls_test_DKU.$(MEXSUFFIX)
INCLUDE = -I$(MATLABHOME)/extern/include $(INCLUDE_TSNNLS)
###########CHANGED PART ENDS #################################
REBUILDABLES = $(OBJS_tsnnls1) $(TARGET)
LIBS = $(LIBS_tsnnls) $(STLIB_tsnnls)
all : $(TARGET)
echo All done
clean :
rm -f $(REBUILDABLES)
echo Clean done
$(OBJS_ADD): Include_4_TSNNLS.c
gcc -c -o Include_4_TSNNLS.o Include_4_TSNNLS.c
###########CHANGED PART STARTS #################################
# Final linking
$(TARGET) : $(OBJS_tsnnlsALL) $(OBJS_ADD) $(LIBS)
$(MEX) -g -o $@ $(INCLUDE) $(CFLAGS) $^
###########CHANGED PART ENDS #################################
*Error:*
Now, I am gettingn error which indicates that I am not able to use "INCLUDE" properly:
fatal error: mex.h: No such file or directory
#include "mex.h"
^
compilation terminated.
make: *** [tsnnls_test_DKU.o] Error 1
Could someone please help me with compiler options?
  1 Comment
Geoff Hayes
Geoff Hayes on 14 Feb 2015
Dushyant - have you verified that
-I$(MATLABHOME)/extern/include
is the valid path to where the mex.h file is?

Sign in to comment.

Answers (0)

Categories

Find more on Write C Functions Callable from MATLAB (MEX Files) in Help Center and File Exchange

Products

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!