【问题标题】:error LNK2005 not getting to solution to solve the problem错误 LNK2005 没有找到解决问题的解决方案
【发布时间】:2020-05-16 10:02:24
【问题描述】:

我是 C++ 新手,需要一些帮助

mainclinet.CPP

#include "client.h"
#include "config.h*"
int main(){
    g_pClientSampleConfig = new ClientSampleConfig;
    g_pUaSession = NULL;
    g_pCallback = NULL;
    g_nsIndex = OpcUa_UInt16_Max;
    g_nsIndex2 = OpcUa_UInt16_Max;

     client();

      }

函数 client() 在 mainclient.CPP 文件中声明

client.h

    OpcUa_UInt16                            g_nsIndex2;
    ClientSampleConfig*                     g_pClientSampleConfig = NULL;
    UaSession*                              m_pSession;
    void Connection_Initialzation();

config.cpp

ClientSampleConfig::ClientSampleConfig()
 {
 //some code
 } 


ClientSampleConfig::~ClientSampleConfig()
 {
 }

config.h

   class ClientSampleConfig
{
public:
ClientSampleConfig();
~ClientSampleConfig(); 
}

问题是我创建了一个名为 clinet_start.cpp 的新文件 其中我定义了client();当我尝试在 clinet_start.cpp 中包含 clinet.h 时返回类型为 void,它给出了链接器错误 2005 *class UaClientSdk::UaSession * g_pUaSession"* redefined 我需要帮助

【问题讨论】:

    标签: c++ visual-studio hyperlink


    【解决方案1】:

    我建议你可以参考文档:Linker Tools Error LNK2005

    如果 mainclinet.CPP 和 clinet_start.cpp 包含 clinet.h ,则两者都使用您对 client() 的定义进行编译,因此两个目标文件都将包含 client() 的代码。当链接器收集所有函数时,他看到client() 定义在两个目标文件中,不知道该使用哪一个。因此,链接器引发了一个 LNK2005。

    有两种解决方案适合您:

    1,如果你想让client()被内联,你可以尝试使用Inline Functions (C++)

    2,如果你不关心内联,你可以尝试使用extern (C++)

    【讨论】:

      猜你喜欢
      • 2016-07-23
      • 1970-01-01
      • 1970-01-01
      • 2022-10-24
      • 1970-01-01
      • 2023-04-10
      • 2020-09-04
      • 1970-01-01
      • 2013-10-23
      相关资源
      最近更新 更多