函数模板的显式实例化失败(g ++) [英] explicit instantiation of function template fails (g++)
问题描述
我遇到一些问题(即链接错误)与显式实例化的功能模板。在Visual Studio下的项目链接确定,只有在g ++ / Unix下,使用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:
- 函数模板在我的
MathUtils.h
文件。其中一个函数参数本身是一个struct模板,它被声明为和在h
文件(在同一命名空间下)中实现。 - 函数实现和实例化在
MathUtils.cpp
。 - 函数调用位于
someFile.cpp
(当然#includeMathUtils.h
作为静态库的一部分链接。
- 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 thish
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.
驱动我(几乎)疯了的事情是,构建错误不完全可以重现,我怀疑Eclipse被指责(也许跳过一些步骤,虽然我在每个构建之前使用 clean project
)。
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).
大约一个小时,Debug配置创建了w / o错误,但是发布失败,未定义的引用...
链接错误。
然后,对于下一个小时,两个配置失败。然后我做了一个小项目,只有上面提到的3个文件,并从命令行和从Eclipse编译它 - 没有错误。现在这两个配置似乎链接确定。
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.
有没有人使用Eclipse-CDT遇到类似的问题?任何建议?
Did anyone experienced similar issues using the Eclipse-CDT? Any suggestions?
编辑:因为问题不容易(或在所有)可重现,我想这将很难得到答案。如果我有任何新的见解,我会更新。
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.
推荐答案
我有一个类似的问题。通过在 .cpp
中使用类实现移动实例化之后实现
I had a similar problem. Solved it by moving instantiation after implementation in the .cpp
with the class implementation.
myclass.hpp:
myclass.hpp:
template <class T>
class MyClass
{
public:
MyClass();
// other declarations
};
myclass.cpp:
myclass.cpp:
#include "myclass.hpp"
template <class T>
MyClass<T>::MyClass()
{
}
template class MyClass<int>;
template class MyClass<bool>;
这篇关于函数模板的显式实例化失败(g ++)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!