不使用模板时如何摆脱警告LNK4006?
我知道这个问题不太具有描述性,但我无法更好地表达它。
我正在尝试编译一个包含多个对象的静态链接库,所有对象都包含以下内容:
#include foo.h
foo.h 是这样的:
#pragma once
template<class T>
class DataT{
private:
T m_v;
public:
DataT(T v) : m_v(v){}
};
typedef DataT<double> Data;
现在,一切正常,但如果我将 DataT 更改为带有 double 的 Data 而不是T,我将在链接每个 .obj 时收到 LNK4006 警告,指出 .ctor 已定义。
编辑1:
#pragma once
class Data{
private:
double m_v;
public:
Data(double v) : m_v(v){}
};
编辑2: 我使用的是MSVC7。 .ctor 实际上包含在这两种情况中,就像
...
public:
Data(double v);
#include foo.inl
...
//foo.inl
Data::Data(double v): m_v(v) {}
我想要完成的那样,不是编译它,而是作为用户可以使用的标头。
I know the question is not very descriptive but I couldn't phrase it better.
I'm trying to compile a static linked library that has several objects, all the objects contain the following:
#include foo.h
foo.h is something along these lines:
#pragma once
template<class T>
class DataT{
private:
T m_v;
public:
DataT(T v) : m_v(v){}
};
typedef DataT<double> Data;
Now, everything works fine, but if I change DataT to be just Data with double instead of T, I will get a LNK4006 warning at linking time for each .obj stating that the .ctor was already defined.
Edit 1:
#pragma once
class Data{
private:
double m_v;
public:
Data(double v) : m_v(v){}
};
Edit 2:
I'm using MSVC7.
The .ctor is actually included in both cases as in
...
public:
Data(double v);
#include foo.inl
...
//foo.inl
Data::Data(double v): m_v(v) {}
What I'm trying to accomplish though, is not to have that compiled but as a header the user can use.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我不确定您在编辑 #2 的示例中想要做什么,但我认为如果 foo.inl 中有以下内容,它可能会有所帮助:
如果 foo.inl 的内容也被包含在
inline
关键字不起作用或不应该起作用的地方,您可以使用预处理器通过使用扩展为空或inline 的宏来处理差异视情况而定。
I'm not sure what you're trying to do in the example for edit #2, but I think it might help if you have the following in foo.inl:
If the contents of foo.inl is also being included in something where the
inline
keyword won't work or shouldn't be, you can probably use the preprocessor to handle the difference by using a macro that expands to nothing orinline
as appropriate.如果将构造函数定义放在
foo.cpp
中而不是foo.h
中,它将不会单独编译到每个翻译单元中。 目前,构造函数的副本被编译到包含 foo.h 的每个对象中。另一种可能的解决方案是让编译器内联构造函数。 您是否在编译器设置中禁用了内联? 构造函数看起来很容易自动内联。
If you put the constructor definition in
foo.cpp
instead offoo.h
it will not be compiled separately into every translation unit. At the moment a copy of the constructor is compiled into every object the contains thefoo.h
include.The other possible solution would be to the get the compiler to inline the constructor. have you disabled inlining in the compiler settings? The constructor looks easy enough to get automatically inlined.