Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / C++

How to build an image (DLL/EXE) when two of its included libraries have the same symbol (say function/variable) using VC++

5.00/5 (1 vote)
24 Jan 2012CPOL 9.4K  
If the .lib files you're linking to represent some .dll files somewhere else, you can do the following:void main(){ FunB(); // from external_1.lib FunC(); // from external_2.lib // FunA(); // from external_1.lib / external_2.lib ???? typedef void (WINAPI *...
If the .lib files you're linking to represent some .dll files somewhere else, you can do the following:

C++
void main()
{
   FunB(); // from external_1.lib
   FunC();  // from external_2.lib
  
  // FunA();  // from external_1.lib / external_2.lib ????
   typedef void (WINAPI * funa_ptr)(void);
   funa_ptr pfunA = ::GetProcAddress(::GetModuleHandle(_T("external_1.dll")), _T("FunA"));
   if (NULL == pfunA)
   {
     // TODO: Handle the error.
   }
   pfunA();
}



  • The linker should not complain, since the conflicting functions are not statically linked.
  • ::GetModuleHandle should work, since both DLLs were statically linked (and loaded before the .exe where main() is defined)


If the .lib files are static libraries, you can encapsulate one of them within a DLL of your own, replacing the functions by renamed versions:

C++
// In MyDll.cpp: link to external_1.lib
void MyFunA()
{
   FunA();
}
void MyFunB()
{
   FunB();
}

You may link your executable to external_2.lib, and your DLL to external_1.lib, thus avoiding name clashes.
Hope this helps.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)