【问题标题】:C++03 linker "already defined symbol" doesn't appear on intermediate fileC ++ 03链接器“已定义符号”未出现在中间文件上
【发布时间】:2015-09-01 10:39:19
【问题描述】:

我在 Visual Studio 2005 上的一个大型项目中遇到了问题,我已经没有什么想法了。

我什至不能放一个工作代码 sn-p 因为我不知道有什么关系,但我会尝试:

我需要让我的项目中的每个 .cpp 文件都有自己的 ID 号,并创建一个知道该 ID 的对象的实例(可全局访问)。 我在此线程How to manage file unique IDs in c++ 上遵循了已接受答案的帮助 并使其在沙盒环境中工作。

添加文件,给他们一个独特的#define FILEID (FileId::ID_FileName) 然后访问他们的实例在沙盒上工作正常。

现在麻烦来了—— 我将使文件知道其IDS的代码粘贴到主项目中,并进行了编译。

到目前为止一切顺利。

现在,我添加到项目中现有的 .cpp 文件之一:

#include "ids.h"
#define FILEID File1   // The FileId corresponding to this file
#include "global.h"

仍在编译,链接,一切都很好。

将这些行添加到项目中的(任何)第二个 .cpp 文件中 现在给出链接错误:

其中:

  • name1:第一个文件我添加了行(字母)
  • name2:其他不相关的文件名(也可以是我添加行的第二个文件,但也可能只是其他文件)

错误 in name2.obj : error LNK2005: "public static class Instance & __cdecl Manager<3>::getInstance(void)" (?getInstance@$Manager@$02@@SAAAVInstance@@XZ) already defined in name1.obj

有时错误仅出现在第二个文件中,有时(在没有更改的连续构建之间)错误出现在文件夹中的每个 .cpp 文件中。

查看我添加了这些行的文件的中间文件(预处理器输出)恰好显示了

template <>
Instance &Manager<FILEID>::getInstance()
{
    static Instance theInstance = getTheFactory().getInstance(FILEID);
    return theInstance;
};

使用正确的 FileId::ID_FileName,该名称与其他文件的名称不同。 尽管如此,链接器仍认为在多个文件中使用了相同的 FileId。

在不相关的文件(也给出完全相同的错误)上,根本没有出现getInstance()。显然,链接器应该没有理由在那里大喊大叫。

我检查了,项目中的某处没有 .cpp 文件相互包含。

我完全不知道是什么原因造成的 并希望得到任何帮助。


编辑 1

ids.h

enum FileId{
    ID_file1ID=3,//just to see a non zero number in the debugger, which I do
    ID_file2ID,
    //and so on
    FileIdSize
}

编辑 2

当这些错误开始时,编译器开始出现极其意外的行为。

sdfsdfgasaedfahjk 行添加到任何文件仍然可以编译并通过。

它清楚地说明了该行已添加到编译的文件名。 它清楚地表明它链接到它。 它通过了。

我现在不能相信编译器了。

不知道发生了什么。

【问题讨论】:

  • 你能告诉我们FileId::ID_file1等是如何定义的吗?
  • 编辑显示 ids.h(就像引用的问题一样)

标签: c++ visual-studio templates c-preprocessor c++03


【解决方案1】:

您有 2 个 cpp 文件将 FILEID 定义为相同的值 3

至于 MCVE:

ids.h:

#pragma once

#define File1 3
#define File2 3 //<--same value on purpose

global.h

struct Instance
{

};

struct Factory
{
    Instance getInstance(int FileID) { return Instance(); }
};

template <int ID>
struct Manager
{
    Factory factory;

    Instance& getInstance();
    Factory& getTheFactory() { return factory; }
};

template <>
Instance& Manager<FILEID>::getInstance()
{
    static Instance theInstance = getTheFactory().getInstance(FILEID);
    return theInstance;
};

name1.cpp

#include "ids.h"
#define FILEID File1   // The FileId corresponding to this file
#include "global.h"

name2.cpp

#include "ids.h"
#define FILEID File2   // The FileId corresponding to this file
#include "global.h"

在编译时,为Manager&lt;3&gt;::getInstance(void) 创建了一个特殊的实现name1.cppname2.cpp

您不能在 2 个不同的编译单元中为 FILEID 使用相同的值。


编辑:编译时检查值

需要预处理器定义__BASE_FILE__="%(Filename)%(Extension)"

(配置属性 -> C/C++ -> 预处理器 -> 预处理器定义)

template <>
Instance& Manager<FILEID>::getInstance()
{
    #define _STR(x) #x
    #define STR(x) _STR(x)
    #define CHECK_ID() __pragma(message("Initializing \"Instance& Manager<FILEID>::getInstance()\" with FILEID="STR(FILEID)" in "STR(__BASE_FILE__)))
    CHECK_ID()
    static Instance theInstance = getTheFactory().getInstance(FILEID);
    return theInstance;
};

示例输出:

1>------Build started : Project : Test_Call, Configuration : Debug Win32------
1>  name1.cpp
1>  Initializing "Instance& Manager<FILEID>::getInstance()" with FILEID = FileId::ID_file1ID in "name1.cpp"
1>  name2.cpp
1>  Initializing "Instance& Manager<FILEID>::getInstance()" with FILEID = FileId::ID_file2ID in "name2.cpp"
1>  Test_Call.vcxproj-><Project>\Debug\Test_Call.exe
== == == == == Build: 1 succeeded, 0 failed, 0 up - to - date, 0 skipped == == == == ==

编辑:使用 FileId 值作为模板参数 (MSVE)

id.h

#pragma once

enum FileId {
    ID_file1ID = 3,//just to see a non zero number in the debugger, which I do
    ID_file2ID,
    //and so on
    FileIdSize
};

global.h

#pragma once

#include "ids.h"

struct Instance
{

};

struct Factory
{
    Instance getInstance(int FileID) { return Instance(); }  
};

template <FileId ID>
struct Manager
{
    static const FileId manager_id = ID;        
    static Factory& getTheFactory() { return m_factory; }
    static Instance& getInstance()
    {
        static Instance theInstance = getTheFactory().getInstance(manager_id);
        return theInstance;
    }

private:
    static Factory m_factory;
};

global.cpp

#include "global.h"

Factory Manager<FileId::ID_file1ID>::m_factory;
Factory Manager<FileId::ID_file2ID>::m_factory;

name1.cpp

#include "global.h"

void test1()
{
    Instance& a = Manager<FileId::ID_file1ID>::getInstance();
}

name2.cpp

#include "global.h"

void test2()
{
    Instance& a = Manager<FileId::ID_file2ID>::getInstance();
}

test.cpp

#include <iostream>
#include "global.h"

using namespace std;


int main(int argc, char** argv)
{
    Instance& a = Manager<FileId::ID_file1ID>::getInstance();
    Instance& b = Manager<FileId::ID_file2ID>::getInstance();
    Instance& c = Manager<FileId::ID_file1ID>::getInstance();

    Instance* aptr = &a;
    Instance* bptr = &b;
    Instance* cptr = &c;

    printf("aptr==bptr -> %s\n", (aptr == bptr) ? "true" : "false"); //->false
    printf("aptr==cptr -> %s\n", (aptr == cptr) ? "true" : "false"); //->true (both use the instance from ID_file1ID
    printf("bptr==cptr -> %s\n", (bptr == cptr) ? "true" : "false"); //->false

}

【讨论】:

  • 不对.. 我确实不能使用相同的值3,但我不这样做!我使用的是不同的值,它由 ids.h 中的枚举生成。一个文件中的值应该不同(并且在它确实显示的中间文件中):FileId::ID_file1 和另一个 FileId::ID_file2
  • 您的错误消息显示有 2 个 template &lt;&gt; Instance&amp; Manager&lt;FILEID&gt;::getInstance() 声明,其中 FILEID? 设置为 3。因此,您要么使用相同的 ID 两次,要么包含 Instance&amp; Manager&lt;FILEID&gt;::getInstance() 所在的标头定义了两次。
  • 就像我说的,中间文件显示不同的FileIds,(即使使用硬编码的34 我仍然得到相同的错误)。中间文件只显示正确的数字,没有别的。一定有别的东西
  • 您是否可能包含其中一个 cpp 文件?
  • 正如我在原始问题中所述,不包含任何 .cpp 文件。你如何看待通过使用codeproject.com/Articles/48575/…codeproject.com/Articles/48575/… 的方法 1 来绕过这个问题?
【解决方案2】:

这不是答案,但可能有助于找出问题所在。

  • 以下代码与原始答案基本相同,但所有复杂性都被剥离了,代价是在各个地方需要样板代码。

    idmanager.h

    struct Instance {/*...*/};
    
    Instance &getFile1Instance();
    Instance &getFile2Instance();
    // etc...
    

    idmanager.cpp

    Instance &getFile1Instance()
    {
        static Instance file1instance;
        return file1instance;
    }
    
    Instance &getFile2Instance()
    {
        static Instance file2instance;
        return file2instance;
    }
    
    // etc...
    

    在每个文件中,放在开头

    #include "idmanager.h"
    

    您可以通过显而易见的方式获取任何文件的静态Instance

    这很简单,因此将其复制到您的项目中根本不会造成问题。

  • 如果上述示例有效,请尝试使其更接近原始答案:将getFileXInstance 函数的定义移动到文件本身,并删除idmanager.cpp

    idmanager.h

    struct Instance {/*...*/};
    
    Instance &getFile1Instance();
    Instance &getFile2Instance();
    // etc...
    

    file1.cpp

    #include "idmanager.h"
    
    Instance &getFile1Instance()
    {
        static Instance file1instance;
        return file1instance;
    }
    

    file2.cpp

    // etc...
    

    显然这只是在不同的 .obj 文件之间移动代码,所以应该仍然可以工作。

  • 现在将每个 getFileXInstance 函数替换为 struct 和单个静态成员函数 getInstance,如下所示:

    idmanager.h

    struct Instance {/*...*/};
    
    struct Manager1
    {
        static Instance &getInstance(); // defined in file1.cpp
    };
    
    struct Manager2
    {
        static Instance &getInstance(); // defined in file2.cpp
    };
    
    // etc...
    

    file1.cpp

    #include "idmanager.h"
    
    Instance &Manager1::getInstance()
    {
        static Instance file1instance;
        return file1instance;
    }
    

    file2.cpp

    // etc...
    
  • 上一步允许我们使用模板减少样板代码的数量:

    idmanager.h

    struct Instance {/*...*/};
    
    template <int id>
    struct Manager
    {
        static Instance &getInstance(); // each instantiation has its definition in a different cpp file
    };
    

    file1.cpp

    #include "idmanager.h"
    
    template <>
    Instance &Manager<1>::getInstance()
    {
        static Instance file1instance;
        return file1instance;
    }
    

    这是链接器错误最有可能再次出现的地方,如果真的出现的话。

  • 也可以通过将公共代码放在共享标头globals.h 中并将预处理器常量FILEID 传递给它来消除更多重复。

    idmanager.h

    struct Instance {/*...*/};
    
    template <int id>
    struct Manager
    {
        static Instance &getInstance(); // each instantiation has its definition in a different cpp file
    };
    

    file1.cpp

    #include "idmanager.h"
    #define FILEID 1
    #include "globals.h"
    

    globals.h

    template <>
    Instance &Manager<FILEID>::getInstance()
    {
        static Instance theInstance;
        return theInstance;
    }
    

最后一个示例现在与原始答案相同,但有一些与链接器错误无关的差异(没有工厂、没有枚举、没有 getThisFileInstance())。因此(假设第一个示例有效)您可以确定哪个更改破坏了程序,这应该有助于诊断真正的问题。

(注意:虽然您的错误正是如果多个文件共享相同的 id 时会出现的错误,但从 cmets 来看,我认为情况并非如此。)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-03-16
    • 2015-09-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-09-17
    • 1970-01-01
    相关资源
    最近更新 更多