makefile文件与MEX-项目:创建共享目标时`.RODATA'不能用 [英] makefile with mex-project: `.rodata' can not be used when making a shared object

查看:864
本文介绍了makefile文件与MEX-项目:创建共享目标时`.RODATA'不能用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个C- code,我可以编译生成文件使用和运行。现在,我想将它转化成MEX文件,我想用相应的makefile编译。然而,我对MEX code makefile文件不能正常工作。我想AP preciate,如果有人能指导我。

I have a c-code which I can compile using makefile and run. Now, I want to convert it to mex file which I would like to compile using corresponding makefile. However, my makefile for mex code is not working. I would appreciate if someone can guide me.

我工作的 64位系统。

这两个C- codeS和MEX codeS结构:

Structure of both C-codes and mex codes:

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

生成文件(C-项目)的作品:

Makefile (C-project) 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 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) $^

生成文件(MEX项目)不工作:

Makefile (mex project) DOES NOT work:

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

I think that I am already compiling with -fPIC. My understanding is that $(OBJS_tsnnls) may not properly included in my mex project.

OR,
问题可能出在32位和64位平台<一个href=\"http://stackoverflow.com/questions/9637551/relocation-r-x86-64-32s-against-rodata-while-compiling-on-64-bit-platform\">Stackoveflow-link.我不知道如何解决这个问题。

OR, issue could be with 32-bit vs 64 bit platform Stackoveflow-link. I have no idea how to fix this.

任何帮助将是那么非常AP preciated。

Any help would be so very much appreciated.

推荐答案

再看看另一个错误信息 - 与调用部分 MEX

Have another look at the error messages - the part with the call to mex:

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

当你已经修改了 CFLAGS 的Makefile变量,包括 -fPIC ,你不能保证它被传递给 MEX (最底部在Makefile命令)。你可以修改 MEXFLAGS 。这应涵盖所有基地:

While you have modified the CFLAGS Makefile variable to include -fPIC, you didn't ensure it was being passed to mex (the very bottom command in your Makefile). You could modify MEXFLAGS. This should cover all bases:

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

您不必都 CC CXX (可能只是 CXX ,因为它的编译为C ++)。此外,添加了 -v 标记,看看有什么失败的G ++命令了。

You don't need both CC and CXX (probably just CXX since it's compiling as C++). Also, add the -v flag to see what the failing g++ command was.

有趣的是,使用 MEX 只是链接对象文件,而不是也在做编译。如果我了解这里的目标,这是链接编译为C的对象文件和方法是指导 MEX 编译为C ++,但设置 CXX = GCC ,迫使它使用C前端。应工作我猜,但它似乎相当扭曲。

It is interesting to use mex just to link object files instead of also do the compiling. If I understand the goal here, it's to link object files compiled as C, and the approach was to instruct mex to compile as C++, but setting CXX=gcc, forcing it to use the C frontend. Should work I guess, but it seems rather contorted.

这篇关于makefile文件与MEX-项目:创建共享目标时`.RODATA'不能用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆