【问题标题】:C++ - issue while porting C++ code from Visual Studio to Linux Eclipse IDEC++ - 将 C++ 代码从 Visual Studio 移植到 Linux Eclipse IDE 时出现问题
【发布时间】:2014-03-21 10:31:46
【问题描述】:

我有一个在 Microsoft Visual Studio 中构建的项目,我想使用 Eclipse IDE 将它移植到 Linux(OpenSuse 12.2) 中。

该项目正在使用 OpenCV,我已设法在 Linux 中构建 OpenCV。它不是那么大,它包含 3 个 .cpp 文件和 4 个 .h 文件,它们实际上定义了项目中使用的类,其中一个 .cpp 文件包含main() 函数。

但是,还有一个额外的instances.inc 文件,其内容如下:

#include "graph.h"

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

// Instantiations: <captype, tcaptype, flowtype>
// IMPORTANT:
//    flowtype should be 'larger' than tcaptype
//    tcaptype should be 'larger' than captype

template class Graph<int,int,int>;
template class Graph<short,int,int>;
template class Graph<float,float,float>;
template class Graph<double,double,double>;

其中graph.h 包含Graph 类的声明。

我将instances.inc 文件的扩展名更改为instances.h 文件。但是,当我尝试构建项目时,我收到以下错误:

../src/BranchAndMincut.cpp:246: undefined reference to `Graph<int, int, int>::maxflow(bool, Block<int>*)'

我猜它与.inc 文件有关。你知道如何解决这个问题吗?

编辑:

我还想提一下instanes.inc 文件包含在graph.cpp 文件的末尾。

【问题讨论】:

    标签: c++ linux eclipse visual-studio


    【解决方案1】:

    您正在使用模板类的实例化。首先,您可以删除包含此模板类Graph 的实现的.cpp 文件,并将方法等的实现放入Graph.h 文件中——此操作将使实例化变得不必要。 接下来您可以尝试将instances.h/inc 重命名为instances.cpp 并编译它(将其放在makefile 中或您使用什么)。

    我找到了一个很好的链接:

    http://www.cplusplus.com/forum/articles/10627/

    http://www.cplusplus.com/forum/articles/14272/

    class template instantiation

    Why can templates only be implemented in the header file?

    【讨论】:

    • 这一步之后includes.inc的内容就不需要了吗?我可以删除Graph 类的实例吗?
    • 将所有实现部分放在头文件中后——是的,在此之后您将不需要实例化。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-09-28
    • 1970-01-01
    • 1970-01-01
    • 2013-10-13
    • 2011-01-02
    • 2021-05-26
    • 1970-01-01
    相关资源
    最近更新 更多