函数模板的显式实例化失败 (g++)

发布于 2024-10-06 18:19:00 字数 758 浏览 0 评论 0原文

我在函数模板的显式实例化方面遇到一些问题(即链接错误)。在Visual Studio下项目链接正常,仅在g++/Unix下,使用Eclipse-CDT,链接产生错误。

在一个大项目中,函数调用是静态库的一部分,它与动态库链接。 该函数的架构如下:

  • 在我的 MathUtils.h 文件中的命名空间内声明(但未实现)的函数模板。函数参数之一本身就是一个结构模板,它在此 h 文件中声明并实现(在同一命名空间下)。
  • 函数实现和实例化位于MathUtils.cpp中。
  • 函数调用位于 someFile.cpp 中(当然是 #include "MathUtils.h")作为静态库的一部分链接。

让我(几乎)发疯的是,构建错误不能完全重现,我怀疑应该归咎于 Eclipse(也许跳过了一些步骤,尽管我在每次构建之前都使用了clean project) )。

在大约一个小时的时间里,调试配置构建没有错误,但发布失败,并出现未定义的引用...链接错误。 然后,在接下来的一个小时里,两种配置都失败了。然后我做了一个小项目,只有上面提到的 3 个文件,并从命令行和 Eclipse 编译它 - 没有任何错误。现在两种配置似乎都链接正常。

有人在使用 Eclipse-CDT 时遇到过类似的问题吗?有什么建议吗?

编辑:由于问题不容易(或根本)重现,我想很难得到答案。如果我有任何新的见解,我会更新。

I am experiencing some problems (i.e, linkage errors) with explicit instantiation of a function template. Under Visual Studio the project links ok, only under g++/Unix, using Eclipse-CDT, the linkage produce errors.

The function call is a part of a static library, which is linked with a dynamic library, in a big project.
The architecture of the function is as follows:

  • function template declared (but not implemented) inside a namespace in my MathUtils.h file. One of the function arguments is itself a struct template, which is declared and implemented in this h file (under the same namespace).
  • function implementation and instantiation is in MathUtils.cpp.
  • function call is in someFile.cpp (which of course #include "MathUtils.h") which is compiled & linked as a part of a static library.

The thing that drives me (almost) crazy, is that the build errors are not fully reproducible, and I suspect the Eclipse is to be blamed (maybe skipping some steps, although I use clean project before each build).

For about an hour, the Debug configuration built w/o errors but the Release failed with undefined reference to... linkage error.
Then, for the next hour, both configurations failed. Then I made a small project, with only the 3 files mentioned above, and compiled it both from the command line and from Eclipse - no errors at all. Now Both configurations seem to link ok.

Did anyone experienced similar issues using the Eclipse-CDT? Any suggestions?

EDIT: since the problem is not easily (or at all) reproducible, I guess it will be hard to get an answer. I will update if I have any new insights.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

兰花执着 2024-10-13 18:19:00

我有类似的问题。通过将 .cpp 中的实例化移动到实现与类实现一起解决了这个问题。

myclass.hpp:

template <class T>
class MyClass
{
public:
    MyClass();
    // other declarations
};

myclass.cpp:

#include "myclass.hpp"

template <class T>
MyClass<T>::MyClass()
{
}

template class MyClass<int>;
template class MyClass<bool>;

I had a similar problem. Solved it by moving instantiation after implementation in the .cpp with the class implementation.

myclass.hpp:

template <class T>
class MyClass
{
public:
    MyClass();
    // other declarations
};

myclass.cpp:

#include "myclass.hpp"

template <class T>
MyClass<T>::MyClass()
{
}

template class MyClass<int>;
template class MyClass<bool>;
酸甜透明夹心 2024-10-13 18:19:00

引用自 www.cplusplus.com

因为模板是在需要时编译的,所以这个强制限制多文件
项目:模板类或函数的实现(定义)必须在同一个项目中
文件作为其声明。这意味着我们不能将接口放在单独的标头中
文件,并且我们必须在使用模板的任何文件中包含接口和实现。

Quoting from www.cplusplus.com

Because templates are compiled when required, this forces a restriction for multi-file
projects: the implementation (definition) of a template class or function must be in the same
file as its declaration. That means that we cannot separate the interface in a separate header
file, and that we must include both interface and implementation in any file that uses the templates.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文