C++ - 将 C++ 代码从 Visual Studio 移植到 Linux Eclipse IDE 时出现问题

标签 c++ linux eclipse visual-studio

我在 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 文件的末尾。

最佳答案

您正在使用模板类的实例化。首先,您可以删除包含此模板类 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?

关于C++ - 将 C++ 代码从 Visual Studio 移植到 Linux Eclipse IDE 时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22556271/

相关文章:

C++读取txt数据和char到二维数组

c++ - 获取由 vector 的 vector 表示的矩阵的第一列

python - 如何在 shell 中将字典键/值作为命令执行?

linux - 从 Ansible Playbook 创建用户密码

PHP-ODBC 驱动程序安装问题

java - 如何关闭 eclipse 中某些文件夹(如 node_modules)的 javascript 验证(包括 tern/lint/jshint)?

java - apache tomcat 服务器的问题总是运行一个程序,它显示端口 8080 已被使用..?

c++ - 如何优化 unordered_map 查找?

java - 使用 Eclipse 创建 Java 应用程序启动器

c++ - 指向数据成员地址的指针