链接MATLAB Mex库中的错误

Bil*_*ham 3 c++ macos matlab linker-errors mex

我正忙着编写一个MATLAB Mex库 - 特别是来自这个网站的'Correlation Clustering Optimization'代码.

我想在OSX机器上编译,并使用提供的mexall函数.这将运行以下行:

mex -O -largeArrayDims CXXFLAGS="\$CXXFLAGS -Wno-write-strings" QPBO.cpp QPBO_extra.cpp QPBO_maxflow.cpp QPBO_wrapper_mex.cpp QPBO_postprocessing.cpp -output QPBO_wrapper_mex
Run Code Online (Sandbox Code Playgroud)

链接时将错误发生在以下输出到MATLAB命令行:

ld: duplicate symbol QPBO<int>::GetMaxEdgeNum()    in QPBO_extra.o and QPBO.o
collect2: ld returned 1 exit status

    mex: link of ' "QPBO_wrapper_mex.mexmaci64"' failed.
Run Code Online (Sandbox Code Playgroud)

从此判断,功能GetMaxEdgeNum出现在QPBO_extra.oQPBO.o.但是,它实际上只在头文件中定义QPBO.h.因此,我怀疑包含它的两个源文件都将其作为符号包含在其目标文件中,从而导致链接时出现问题.

(更多信息:每个源文件还包含文件#include "instances.inc"末端的文件.instances.inc显然似乎包含模板化的一些特定实例QPBO.)

我在这里犯了一个明显的错误吗?我该怎么做才能增加编译代码的机会?

编辑

这是有问题的GetMaxEdgeNum函数的定义QPBO.h:

template <typename REAL> 
    inline int QPBO<REAL>::GetMaxEdgeNum() 
{
    return (int)(arc_max[0]-arcs[0])/2;
}
Run Code Online (Sandbox Code Playgroud)

编辑2

关于我的机器的更多细节:

  • OSX 10.6.8
  • MATLAB R2012a(也有R2011b)
  • g ++ 4.2或g ++ 4.0(或通过MacPorts的g ++ 4.6)

我在下面的"赏金描述"中添加了一些关于我真正想要的细节.

编辑3

有一些共识instances.inc可能导致麻烦.它包含在每个cpp文件的末尾,它包含以下内容:

#include "QPBO.h"

#ifdef _MSC_VER
#pragma warning(disable: 4661)
#endif

// Instantiations

template class QPBO<int>;
template class QPBO<float>;
template class QPBO<double>;

template <> 
    inline void QPBO<int>::get_type_information(char*& type_name, char*& type_format)
{
    type_name = "int";
    type_format = "d";
}

template <> 
    inline void QPBO<float>::get_type_information(char*& type_name, char*& type_format)
{
    type_name = "float";
    type_format = "f";
}

template <> 
    inline void QPBO<double>::get_type_information(char*& type_name, char*& type_format)
{
    type_name = "double";
    type_format = "Lf";
}
Run Code Online (Sandbox Code Playgroud)

Sha*_*hai 5

似乎问题是某些模板代码在.cpp文件中.

  1. #include instances.inc.cpp文件中删除声明.
  2. 将所有代码QPBO.cpp,QPBO_extra.cpp,QPBO_maxflow.cppQPBO_postprocessing.cpp的头文件qpbo.h.
  3. 您现在将拥有一个.cpp文件qpbo_wrapper_mex.cpp和一个标头qpbo.h
  4. mex文件(在matlab中)使用:

    >> mex -O -largeArrayDims qpbo_wrapper_mex.cpp
    
    Run Code Online (Sandbox Code Playgroud)

应该管用...