c++:头函数未正确从库链接到 exe
我在库中有一个头文件(alibrary.lib)。 该库是一个静态库 (.lib),它正确链接到 exe。
现在,我有一个类:Vector3d。
class Vector3d
{
void amethod()
{
blah
}
};
Vector3d cross(const Vector3d &v0, const Vector3d &v1)
{
float x,y,z;
x = v0.y*v1.z-v0.z*v1.y;
y = v0.z*v1.x-v0.x*v1.z;
z = v0.x*v1.y-v0.y*v1.x;
return Vector3d(x,y,z);
Vector3d
在头文件 (Vector3d .h) 中声明和定义。 在类声明之后,我使用了 cross 函数。
lib 编译是文件,但是当它链接到单元测试 exe 时,我收到此错误:
flywindow.obj :error LNK2005: "class Vector3d __cdecl cross(class Vector3d const &,class Vector3d const &)" (?cross@@YA?AVVector3d@@ABV1@0@Z) already defined in fly.obj
有什么想法吗?
谢谢
I have a header file in a library (alibrary.lib). The library is a static library (.lib) and it links properly to exe.
Now, I have a class: Vector3d.
class Vector3d
{
void amethod()
{
blah
}
};
Vector3d cross(const Vector3d &v0, const Vector3d &v1)
{
float x,y,z;
x = v0.y*v1.z-v0.z*v1.y;
y = v0.z*v1.x-v0.x*v1.z;
z = v0.x*v1.y-v0.y*v1.x;
return Vector3d(x,y,z);
}
Vector3d is declared and defined in a header file (Vector3d .h). After the class declaration, I the cross function.
The lib compile is file, but when it links to the unit test exe I get this error:
flywindow.obj :error LNK2005: "class Vector3d __cdecl cross(class Vector3d const &,class Vector3d const &)" (?cross@@YA?AVVector3d@@ABV1@0@Z) already defined in fly.obj
Any ideas?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果定义一个自由(不是类的成员)函数,则必须在单独编译的 .cpp 文件中定义它,或者在标头中定义它并标记为内联。 因此,在您的情况下,您可以通过以下方式进行编译:
导致错误的原因是您在标头中有函数的定义,但没有将其标记为内联。 如果您现在将该标头包含到两个单独编译的文件中,则链接器在尝试链接已编译的目标文件时将抛出错误,因为它会看到交叉函数被定义了两次。
它无需显式地内联类的成员函数即可工作,因为在类定义中定义的成员函数是隐式内联的。
然而,通常在标头中进行函数定义并不是一个好主意。 如果您的函数依赖于其他类型而不仅仅是向量(在您的情况下,恕我直言,这很好,但当然这是有争议的 - 有些人不喜欢它),那么您将需要包含这些类型的标头。 这会不必要地使标头间接包含的代码变得臃肿。 相反,在这些情况下,您只需将函数声明放入标头中:
但在单独编译的 .cpp 文件中定义它。 当然,内联应该被删除。
让我添加一小部分定义和声明,只是为了帮助清楚地了解声明和定义对于函数和类的含义。 请注意,每个定义也是一个声明,但反之则不然:
If you define a free (not a member of a class) function, it has to be defined in a .cpp file separately compiled, or in a header and marked inline. So in your case, you can get away making it compile by this:
The error is caused because you have the definition of the function in the header, but haven't marked it inline. If you now include that header into two files that are compiled separately, the linker, when trying to link the compiled object files, will throw out an error, because it then sees a cross function being defined twice.
It works without exlicitly putting inline for member functions of a class, because member functions that are defined inside the class definition are implicitly inline.
It is however, not a good idea generally to make function definitions in the header. If your function would depend on other types than just the vector (in your case it's fine IMHO, but it's debatable of course - some people don't like it), then you would be required to include the headers for those types. That will unnecessarily bloat the code that's indirectly included by your header. Instead, in those cases you would solely put only a declaration of your function inside the header:
But define it within the .cpp file that's compiled separately. The inline, of course, should then be dropped.
Let me add a small list of definitions and declarations, just to help in keeping things clear about what declaration and definition means for functions and classes. Note that every definition is also a declaration, but not the other way around:
最可能的解释是,您的包含文件中有代码(特别是交叉的定义),并且您的包含文件被两个源文件包含,因此存在双重定义。
头文件中应该包含声明,而不是定义。 声明(表示某些东西存在)是指 typedef、类声明、枚举等。
定义(赋予存在的事物以意义)是诸如函数、变量定义等之类的东西。
您的交叉函数应该在头文件中声明:
但在单独的源文件中定义。
The most likely explanation is that you've got code (specifically the definition of cross) in your include file and your include file is being included by two source files, hence the double definition.
Header files should have declarations in them, not definitions. Declarations (syaing that something exists) are things like typedef's, class declarations, enum's and so on.
Definitions (giving meaning to thos things that exist) are things like functions, varialble definitions and so on.
Your cross function should be declared in the header file:
but defined in a separate source file.