任何人都知道如何修复编译错误:LNK2005?(内有源代码) [英] Anyone knows how to fix compile error: LNK2005? (Source Code inside)

查看:30
本文介绍了任何人都知道如何修复编译错误:LNK2005?(内有源代码)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 stdafx.h 中有以下代码.

I have the below code in stdafx.h.

using namespace std;

typedef struct {
    DWORD   address;
    DWORD   size;
    char    file[64];
    DWORD   line;
} ALLOC_INFO;

typedef list<ALLOC_INFO*> AllocList;
//AllocList *allocList;

没有注释代码(最后一行),它编译得很好.但是当我添加注释代码时,我收到以下错误.

Without the commented code (last line), it compiles just fine. But when I add the commented code, Im getting the following error.

错误 LNK2005:类 std::list >* allocList" (?allocList@@3PAV?$list@PAUALLOC_INFO@@V?$allocator@PAUALLOC_INFO@@@std@@@std@@A)已经在 test.obj 中定义

error LNK2005: "class std::list > * allocList" (?allocList@@3PAV?$list@PAUALLOC_INFO@@V?$allocator@PAUALLOC_INFO@@@std@@@std@@A) already defined in test.obj

我使用的是 Visual Studio .NET 2003.有人知道那是什么以及如何解决吗?

Im using Visual Studio .NET 2003. Anyone has any idea what that is and how to solve it?

推荐答案

不要将定义放在头文件中,只需声明.声明指定存在的东西,而定义实际上定义了它们(通过分配空间).例如typedefextern和函数原型都是声明,而structint和函数体都是声明定义.

Don't put definitions in header files, just declarations. Declarations specify that something exists while definitions actually define them (by allocating space). For example typedef, extern and function prototypes are all declarations, while things like struct, int and function bodies are definitions.

发生的情况是您很可能在多个编译单元(C++ 源文件)中包含 stdafx.h,并且每个生成的目标文件都获得了自己的 allocList 副本.

What's happening is that you're most likely including stdafx.h in multiple compilation units (C++ source files) and each of the resulting object files is getting its own copy of allocList.

然后,当您将对象链接在一起时,有两个(或多个)称为 allocList 的东西,因此会出现链接错误.

Then when you link the objects together, there's two (or more) things called allocList, hence the link error.

你最好声明变量:

extern AllocList *allocList;

在您的头文件中并在 C++ 源文件(例如 main.cpp)中的某处定义:

in your header file and defining it somewhere in a C++ source file (such as a main.cpp):

AllocList *allocList;

这样,每个包含 stdafx.h 的编译单元都会知道外部变量,但它只在一个编译单元中定义.

That way, every compilation unit that includes stdafx.h will know about the external variable, but it's only defined in one compilation unit.

根据您的进一步信息:

我试图关注 http://www.flipcode.com/archives/How_To_Find_Memory_Leaks.shtml,我假设所有这些代码都应该放在 stdafx.h 中.各位,还有其他选择吗?

I was trying to follow http://www.flipcode.com/archives/How_To_Find_Memory_Leaks.shtml, I assume that all those code are meant to be placed in the stdafx.h. Any other alternatives, pax?

我的回答如下.

我自己不会把它们放在 stdafx.h 中,因为我认为这对预编译头文件使用了一些 MS 魔法.

I wouldn't put them in stdafx.h myself since I think that uses some MS magic for pre-compiled headers.

制作一个单独的头文件 mymemory.h 并将你的函数原型放入其中,例如(注意这里没有 body):

Make a separate header file mymemory.h and put your function prototypes in it, for example (note that this has no body):

inline void * __cdecl operator new(
    unsigned int size,
    const char *file,
    int line);

同样在那个头文件中,放入 AddTrack()DumpUnfreed() 等的其他原型,以及 #definetypedefextern 语句:

Also in that header, put the other prototypes for AddTrack(), DumpUnfreed(), etc., and the #define, typedef and extern statements:

extern AllocList *allocList;

然后,在一个新文件mymemory.cpp(其中还包含#include "mymemory.h")中,放入allocList的实际定义code> 连同所有实际功能(不仅仅是原型)并将该文件添加到您的项目中.

Then, in a new file mymemory.cpp (which also contains #include "mymemory.h"), put the actual definition of allocList along with all the real functions (not just the prototypes) and add that file to your project.

然后,#include "mymemory.h" 在您需要跟踪内存的每个源文件中(可能是所有源文件).因为头文件中没有定义,所以在链接过程中你不会得到重复,而且因为声明在那里,你也不会得到未定义的引用.

Then, #include "mymemory.h" in every source file in which you need to track memory (probably all of them). Because there are no definitions in the header file, you won't get duplicates during the link and because the declarations are there, you won't get undefined references either.

请记住,这不会跟踪您未编译的代码(例如第三方库)中的内存泄漏,但它应该让您了解自己的问题.

Keep in mind that this won't track memory leaks in code that you don't compile (e.g., third-party libraries) but it should let you know about your own problems.

这篇关于任何人都知道如何修复编译错误:LNK2005?(内有源代码)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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