包含两个具有相同函数名称的库时出现链接器警告 "second definition ignored"

Linker warning "second definition ignored" when including two libraries with same function names

上下文

我正在从事一个旨在向设备发送特定命令的项目。每个设备都可以与一个 dll 接口(例如 deviceADll.h、deviceBDll.h),并且 Dll 不是我编写的,我也不能以任何方式修改它们。我负责将 DeviceB 集成到项目中,对项目的结构进行最少的更改。我知道结构可能不是最优的 and/or 设计得很好,所以我愿意将有关此事的建议作为最后的解决方案。

由于设备非常相似,因此所有 Dll 函数都具有相同的名称,并且通常具有相同的原型。

也是因为这个,我做了一个父类class(Device_ts.h),DeviceA_ts.h和DeviceB_ts.h继承自父类(我也有一个工厂class 对于设备,但我认为这与我的问题无关)。

问题

当我尝试包含两个 Dll 时出现问题:项目编译,但我得到一个

Warning 60 warning LNK4006: Connect@12 already defined in DeviceA.lib(DeviceA.dll); second definition ignored C:\project_path\DeviceB.lib(DeviceB.dll) Project_Name

后跟

Warning 61 warning LNK4006: __imp__Connect@12 already defined in DeviceA.lib(DeviceA.dll); second definition ignored C:\project_path\DeviceB.lib(DeviceB.dll) Project_Name

还有一个

Warning 62 warning LNK4221: This object file does not define any previously undefined public symbols, so it will not be used by any link operation that consumes this library C:\project_path\DeviceB.lib(DeviceB.dll) Project_Name

有没有人遇到过类似的情况?我应该忽略这些警告,还是我无法调用 DeviceB.h 函数,因为它们的定义被忽略了?

我用的是Visual Studio 2010,我写的Device_ts.h库是一个静态库,项目的所有参数(例如/MD,include directories,dependencies,MFC等)都设置好了根据我在研究这个问题时发现的内容。

代码

include 和代码如下所示(我将只显示导致警告的函数之一,因为我在 50 个函数上遇到相同的错误):

DeviceADll.h

#ifndef DEVICEA_H__
#define DEVICEA_H__

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000

namespace DeviceA
{
// some struct definition that don't cause the linker warnings
//...

// function definitions
extern "C" HANDLE PASCAL EXPORT Connect( HANDLE h_devA, const char *ip);
// ...
} // namespace DeviceA

DeviceBDll.h

#ifndef DEVICEB_H__
#define DEVICEB_H__

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000

namespace DeviceB
{
// some struct definition that don't cause the linker warnings
//...

// function definitions
extern "C" HANDLE PASCAL EXPORT Connect( HANDLE h_devB, const char *ip);
// ...
} // namespace DeviceB

Device_ts.h

#ifndef DEVICE_FCT_H_
#define DEVICE_FCT_H_
#ifndef EXPORT
#define EXPORT
#endif

#if _MSC_VER > 1000
#pragma once
#endif

#include "DeviceADll.h"
#include "DeviceBDll.h"

class CDevice {
public:
    virtual BOOL Connect(char *ip_addr) = 0;
};
#endif DEVICE_FCT_H_

这是手动加载 DLL 的一个很好的用例,使用 LoadLibrary()GetProcAddress()

您必须为以这种方式查找的每个函数管理一个函数指针,这有点麻烦,但绕过 OS 的 dll 加载为您提供了很大的灵活性。

另请注意,使用此方法时不需要link针对DLL,dll绑定是100%运行时,根本不涉及linker。

这是一个例子:

typedef void (*connect_fn)(HANDLE, const char*);

connect_fn connect_a;
connect_fn connect_b;

int main()
{
  HINSTANCE dll_a = LoadLibrary("path_to_dll_a.dll");
  HINSTANCE dll_b = LoadLibrary("path_to_dll_b.dll");

  if (!dll_a || !dll_b) {
    return 1;
  }

  connect_a = (connect_fn)GetProcAddress(dll_a , "Connect");
  connect_b = (connect_fn)GetProcAddress(dll_b , "Connect");

  // connect_a and connect_b can now be used.
  return 0;
}

编辑: 基本上,我建议您将设备 DLL 视为插件,而不是动态库。