静态库多定义链接错误 [英] Static Lib Multiple Definition Link Error

查看:110
本文介绍了静态库多定义链接错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我尝试在VC ++ 8.0上构建一个小型3D引擎.我有一个MathLib静态库和一个由TestBed exe链接的Render静态库.现在,Render具有两个类:Color和DXManager3D.颜色包括来自MathLib的Vector.h,一切正常.

So I'm trying to build a small 3D engine as an exercise on VC++ 8.0. I have a MathLib static lib and a Render static lib that is being linked by my TestBed exe. Right now Render has two classes: Color and DXManager3D. Color includes my Vector.h from MathLib just fine, no problems.

第二个尝试将Vector.h包含在DXManager3D中,这让我大吃一惊,说该符号被定义了两次,第二个定义被忽略了(从lib发出警告).我以为可能包括两次导致了这个问题,所以我从Color.h中删除了Vector.h并将其留在DXManager3D.h中进行了测试-同样的问题.我已进行了三重检查,以确保将所有内容包裹在ifndef中以防止这种情况,所以我被抓挠了.

The second I try to include Vector.h in DXManager3D it blows up on me, saying the symbol is defined twice, and the second definition is ignored (warning from lib). I thought maybe including it twice was causing this so as a test I removed Vector.h from Color.h and left it in DXManager3D.h - same problem. I have triple checked to make sure I have everything wrapped in ifndef to protect from this, so I am left scratching my head.

DXManager3D.obj:警告LNK4006:"public:__thiscall Math :: Vector :: Vector(void)"(?? 0Vector @ Math @@ QAE @ XZ)已在Render.obj中定义;第二个定义被忽略

DXManager3D.obj : warning LNK4006: "public: __thiscall Math::Vector::Vector(void)" (??0Vector@Math@@QAE@XZ) already defined in Render.obj; second definition ignored

真正让我感到困惑的是,当我与TestBed分开构建Render.lib时,它不应该链接任何东西,因为它是静态库,对吗?我仍然收到多个符号定义警告.如果我在主实例化DXManager3D,我的警告就会变成错误.

What really confuses me is that when I build the Render.lib separate from TestBed, which should not be linking anything as it is a static lib, right? I still get the multiple symbol definition warnings. If I instantiate a DXManager3D in main my warnings become errors.

Render.lib(DXManager3D.obj):错误LNK2005:公共:__thiscall Math :: Vector :: Vector(void)"(?? 0Vector @ Math @@ QAE @ XZ)已在WinMain.obj中定义

是的,我有F1的LNK4006和LNK2005,并且MSDN中的解决方案对我不起作用. 抱歉,如果您之前曾问过这个问题,我找不到任何可以帮助我使用搜索功能的东西.

Yes, I have F1'd LNK4006 and LNK2005 and the solutions in the MSDN aren't working for me. Sorry if this question has been asked before, I couldn't find anything solid to help me out using the search feature.

谢谢!

推荐答案

您的Vector ctor是在类定义之外的标头中定义的吗?使其内联然后进行更改

Is your Vector ctor defined in the header outside the class definition? Make it inline then i.e. change

class Vector {
  public:
    Vector();
  // ...
}; 

Vector::Vector() {
   // ...
}

class Vector {
  public:
  Vector() {}
  // ...
}; 

或使用明确的inline限定条件:

or use an explicit inline qualification:

class Vector {
   public:
  Vector();
  // ...
}; 

inline Vector::Vector() {
   // ...
}

这篇关于静态库多定义链接错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆