使用mex-project的makefile:在创建共享对象时不能使用`.rodata'

时间:2015-02-18 12:09:36

标签: c matlab makefile mex

我有一个c代码,我可以使用makefile编译并运行。现在,我想将它转换为mex文件,我想使用相应的makefile进行编译。但是,我的mex代码的makefile不起作用。如果有人能指导我,我将不胜感激。

我正在使用 64位系统。

C代码和mex代码的结构:

C-codes                                Mex Code
Test_tsnnls.c <--Main defined         Test_tsnnls_MEX.c <--mex defined here
Include_4_TSNNLS.h                    Include_4_TSNNLS.h
Include_4_TSNNLS.c                    Include_4_TSNNLS.c

Makefile(C-project)有效:

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 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_tsnnls = $(addprefix $(tsnnl_PATH), $(OBJS_tsnnls0)) 

# Libraries for tsnnls
STLIB_tsnnls    = /usr/local/lib/taucs_full/lib/linux/libtaucs.a 
LIBS_tsnnls     = -largtable2 -llapack -lblas -lquadmath -lm

########################################################################
TARGET_WO_EXTN =   Test_tsnnls
TARGET         =   $(TARGET_WO_EXTN)
LIBS           =   $(LIBS_tsnnls)   $(STLIB_tsnnls)
INCLUDE        =   $(INCLUDE_TSNNLS)
REBUILDABLES   =   *.o          $(TARGET) 

all : $(TARGET)
    echo All done

clean : 
    rm -f $(REBUILDABLES)   
    echo Clean done

Include_4_TSNNLS.o: Include_4_TSNNLS.c
    $(CXX) -c -o Include_4_TSNNLS.o Include_4_TSNNLS.c

$(TARGET_WO_EXTN).o: $(TARGET_WO_EXTN).c
    $(CXX) -c -o $(TARGET_WO_EXTN).o  $(TARGET_WO_EXTN).c

# Final linking
$(TARGET) : $(OBJS_tsnnls)   $(TARGET_WO_EXTN).o   Include_4_TSNNLS.o $(LIBS)
    $(CXX) -g -o $@ $(INCLUDE) $(CFLAGS) $^

Makefile(mex项目)不起作用:

MEXSUFFIX  = mexa64
MATLABHOME = /usr/local/MATLAB/R2014b
MEX        = mex
CXX        = gcc
CFLAGS     = -fPIC -pthread -DMX_COMPAT_32 \
               -DMATLAB_MEX_FILE

INCLUDE_Matlab  = -I$(MATLABHOME)/extern/include
MEXFLAGS    = -cxx CC='$(CXX)' CXX='$(CXX)' LD='$(CXX)'

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 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_tsnnls = $(addprefix $(tsnnl_PATH), $(OBJS_tsnnls0)) 

# Libraries for tsnnls
STLIB_tsnnls    = /usr/local/lib/taucs_full/lib/linux/libtaucs.a 
LIBS_tsnnls     = -largtable2 -llapack -lblas -lquadmath -lm

########################################################################
TARGET_WO_EXTN =  Test_tsnnls_Mex
TARGET         = $(TARGET_WO_EXTN).$(MEXSUFFIX)
LIBS           = $(LIBS_tsnnls)     $(STLIB_tsnnls)
INCLUDE        = $(INCLUDE_Matlab)  $(INCLUDE_TSNNLS)
REBUILDABLES   = *.o            $(TARGET)  

all : $(TARGET)
    echo All done

clean : 
    rm -f $(REBUILDABLES)   
    echo Clean done

Include_4_TSNNLS.o:  Include_4_TSNNLS.c
    $(CXX) $(CFLAGS) $(INCLUDE) -c $^
##  $(CXX) -c -o Include_4_TSNNLS.o Include_4_TSNNLS.c

$(TARGET_WO_EXTN).o: $(TARGET_WO_EXTN).c
    $(CXX) $(CFLAGS) $(INCLUDE) -c $^

# Final linking
$(TARGET): $(OBJS_tsnnls)   $(TARGET_WO_EXTN).o   Include_4_TSNNLS.o $(LIBS)
    $(MEX) $(MEXFLAGS) $(LIBS) -output $(TARGET_WO_EXTN) $^

我得到的错误

dkumar@dkumar-Precision-WorkStation-T7500 ~/Mex_Codes_DKU/Using_tsnnls_DKU_copy_MEX/Pure_Mex $ make
cc -fPIC -pthread -DMX_COMPAT_32 -DMATLAB_MEX_FILE   -c -o Test_tsnnls_Mex.o Test_tsnnls_Mex.c
gcc -c -o Include_4_TSNNLS.o Include_4_TSNNLS.c
In file included from Include_4_TSNNLS.c:1:0:
Include_4_TSNNLS.h:72:15: warning: useless storage class specifier in empty declaration [enabled by default]
   extern enum STYPE;
               ^
mex -cxx CC='gcc' CXX='gcc' LD='gcc' -largtable2 -llapack -lblas -lquadmath -lm     /usr/local/lib/taucs_full/lib/linux/libtaucs.a  -output Test_tsnnls_Mex /home/dkumar/libtsnnls-2.3.3/tsnnls/libtsnnls_la-taucs_malloc.o /home/dkumar/libtsnnls-2.3.3/tsnnls/libtsnnls_la-taucs_ccs_order.o /home/dkumar/libtsnnls-2.3.3/tsnnls/libtsnnls_la-taucs_ccs_ops.o /home/dkumar/libtsnnls-2.3.3/tsnnls/libtsnnls_la-taucs_vec_base.o /home/dkumar/libtsnnls-2.3.3/tsnnls/libtsnnls_la-taucs_complex.o /home/dkumar/libtsnnls-2.3.3/tsnnls/libtsnnls_la-colamd.o /home/dkumar/libtsnnls-2.3.3/tsnnls/libtsnnls_la-amdbar.o /home/dkumar/libtsnnls-2.3.3/tsnnls/libtsnnls_la-amdexa.o /home/dkumar/libtsnnls-2.3.3/tsnnls/libtsnnls_la-amdtru.o /home/dkumar/libtsnnls-2.3.3/tsnnls/libtsnnls_la-genmmd.o /home/dkumar/libtsnnls-2.3.3/tsnnls/libtsnnls_la-taucs_timer.o /home/dkumar/libtsnnls-2.3.3/tsnnls/libtsnnls_la-taucs_sn_llt.o /home/dkumar/libtsnnls-2.3.3/tsnnls/libtsnnls_la-taucs_ccs_base.o /home/dkumar/libtsnnls-2.3.3/tsnnls/libtsnnls_la-tlsqr.o /home/dkumar/libtsnnls-2.3.3/tsnnls/libtsnnls_la-tsnnls.o /home/dkumar/libtsnnls-2.3.3/tsnnls/libtsnnls_la-lsqr.o Test_tsnnls_Mex.o Include_4_TSNNLS.o /usr/local/lib/libargtable2.so /usr/lib/liblapack.so /usr/lib/libblas.so /usr/lib/libquadmath.so /usr/lib/x86_64-linux-gnu/libm.so /usr/local/lib/taucs_full/lib/linux/libtaucs.a
Building with 'g++'.
Warning: You are using gcc version '4.8.2'. The version of gcc is not supported. The version currently supported with MEX is '4.7.x'. For a list of currently supported compilers see: http://www.mathworks.com/support/compilers/current_release.
Warning: You are using gcc version '4.8.2-19ubuntu1)'. The version of gcc is not supported. The version currently supported with MEX is '4.7.x'. For a list of currently supported compilers see: http://www.mathworks.com/support/compilers/current_release.
/usr/bin/ld: /home/dkumar/libtsnnls-2.3.3/tsnnls/libtsnnls_la-taucs_ccs_order.o: relocation R_X86_64_32 against `.rodata' can not be used when making a shared object; recompile with -fPIC
/home/dkumar/libtsnnls-2.3.3/tsnnls/libtsnnls_la-taucs_ccs_order.o: error adding symbols: Bad value
collect2: error: ld returned 1 exit status

make: *** [Test_tsnnls_Mex.mexa64] Error 255

我认为我已经使用-fPIC进行编译了。 我的理解是$(OBJS_tsnnls)可能没有正确地包含在我的mex项目中。

OR, 问题可能是32位与64位平台Stackoveflow-link。我不知道如何解决这个问题。

非常感谢任何帮助。

1 个答案:

答案 0 :(得分:0)

再看一下错误消息 - 调用mex的部分:

mex -cxx CC='gcc' CXX='gcc' LD='gcc' -largtable2 -llapack -lblas -lquadmath -lm  

虽然您已将CFLAGS Makefile变量修改为包含-fPIC,但您无法确保将其传递给mex(Makefile中最底层的命令)。您可以修改MEXFLAGS。这应涵盖所有基础:

MEXFLAGS = -v -cxx CC='$(CXX)' CXX='$(CXX)' CFLAGS='$(CFLAGS)' CXXFLAGS='$(CFLAGS)' LD='$(CXX)'

您不需要CCCXX(可能只是CXX,因为它正在编译为C ++)。另外,添加-v标志以查看失败的g ++命令是什么。

使用mex仅链接目标文件而不是编译也很有趣。如果我理解这里的目标,那就是链接编译为C的目标文件,方法是指示mex编译为C ++,但设置CXX=gcc,强制它使用C前端。应该工作,我猜,但它似乎相当扭曲。