c++ - Linker warning "second definition ignored" when including two libraries with same function names -
context
im working on project designed send commands device. each device can interfaced dll (e.g. deviceadll.h, devicebdll.h) , dll's not programmed me, nor can modify them in way. in charge of integrating deviceb project, minimal changes structure of project. know structure may not optimal and/or designed, willing take suggestion concerning matter last resort solution.
since devices similar, dll functions have same name, , same prototype.
also because of this, made parent class (device_ts.h), devicea_ts.h , deviceb_ts.h inherit (i have factory class devices, don't think it's relevant problem).
problem
the problem occurs when try include both dlls: project compiles,
warning 60 warning lnk4006: connect@12 defined in devicea.lib(devicea.dll); second definition ignored c:\project_path\deviceb.lib(deviceb.dll) project_name
followed
warning 61 warning lnk4006: __imp__connect@12 defined in devicea.lib(devicea.dll); second definition ignored c:\project_path\deviceb.lib(deviceb.dll) project_name
and a
warning 62 warning lnk4221: object file not define undefined public symbols, not used link operation consumes library c:\project_path\deviceb.lib(deviceb.dll) project_name
has experienced similar situation? should ignore warning or not able call deviceb.h functions since definitions ignored?
i using visual studio 2010, device_ts.h library writing static library , project's parameters (e.g. /md, include directories, dependencies, mfc, etc) set found in research problem.
code
the include , code looks (i show 1 of functions cause warning since same error on 50 functions):
deviceadll.h
#ifndef devicea_h__ #define devicea_h__ #if _msc_ver > 1000 #pragma once #endif // _msc_ver > 1000 namespace devicea { // struct definition don't cause 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 { // struct definition don't cause 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_
this use-case manual dll loading, using loadlibrary() , getprocaddress().
you'll have manage function pointer each function looked way, bit of pain, bypassing os's dll loading gives lot of flexibility.
also note not need link against dll when using method, dll binding 100% runtime, , linker not involved @ all.
here's example:
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 , connect_b can used. return 0; } edit: basically, suggest treat device dlls plugins, rather dynamic libraries.
Comments
Post a Comment