错误LNK2005无法解决问题的解决方案

error LNK2005 not getting to solution to solve the problem

本文关键字:解决方案 解决问题 LNK2005 错误      更新时间:2023-10-16

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

主斜网。.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

客户端.h

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

配置.cpp

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

ClientSampleConfig::~ClientSampleConfig()
{
}

配置.h

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

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

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

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

有两种解决方案供您选择:

1,如果你想client()是内联的,你可以尝试使用内联函数(C++(

2,如果你不在乎内联,你可以尝试使用extern(C++(